Answer the question
In order to leave comments, you need to log in
Why is 16 output in this javascript operation?
Hello, I'm a green newbie in javascript, I started reading David Flanagan's book (specially bought for 840 rubles), but I just can't understand why 16 is displayed in this operation? I don't seem to understand the javascript logic.
var x = 2, y = 3;
function plus1(x) {
return x+1;
}
plus1(y)
var square = function(x) {
return x*x;
};
square(plus1(y))
var a = 2, b = 3;
function plus1(x) {
return x+1;
}
plus1(y)
var square = function(x) {
return x*x;
};
square(plus1(y))
Answer the question
In order to leave comments, you need to log in
Here you need to understand that in the local scope of the plus1 and square functions, x is the value of the dedicated argument, and not the global variable x .
var x = 2, y = 3;
function plus1(x) {
return x + 1; // тут x это значение которое вы передали при вызове аргументом
}
plus1(y)
var square = function(x) {
return x * x; // тут тоже
};
square(plus1(y))
function plus1(3) {
return 3 + 1;
}
function(4) {
return 4 * 4;
};
in the 1st example, everything is simple, you just need to understand how the functions in Js work. Use the debugger to visually study the work of the js interpreter.
In the 2nd you will not have any result, because. variables y, x are not defined.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question