I put a simple code in JS, in CodePen. JavaSript ignores the function. Output of the console.log is always 2 when it should be 3. Sometimes, there is no output at all.
Why is this happening?
Many thanks in advance.
My code
var x=2;
function test(x) {
return x=x+1;
}
console.log(x);
//output should be 3. However, it is 2
I don’t see anywhere that this syntax would make sense.
If you define a function as
then you should call it as
test(x)
Though, I don’t think that it will do the assignment in the way that you think it will. The function argument x shadows the global variable x, so you will be incrementing the variable x inside of the function test() rather than the global variable x.