output of the javascript snippet? javascript variable declarations [duplicate] - javascript

This question already has answers here:
How JS hoisting works within functions?
(4 answers)
Why a variable defined global is undefined? [duplicate]
(3 answers)
Closed last year.
My first guess is that the following code has the output of 10 and 100. However, after running the code I get undefined and 100 and I don't understand why the function doesn't see the first declaration of x..
var x = 10;
function f() {
console.log(x);
var x = 100;
console.log(x);
}
f();

Related

Extra parameters in a self executing function? [duplicate]

This question already has answers here:
What does the comma operator do in JavaScript?
(5 answers)
Closed 1 year ago.
I saw this piece of code in a webpack compiled file
(1,2, function(x) {
console.log(x);
return 4;
})(5);
This seems to execute correctly. I am aware the 5 is a parameter to function(x)
I don't understand what the 1,2 are? How is this valid?
Well, it's because the brackets only returns the function
(thing1,thing2,thing3,thing4....,thingN) would show the last thing on the list(in this case, thingN)
Example
var bracketTest1=(1,3,5,7,9,null)
console.log(bracketTest1) //null
var bracketTest2=(null,1,2,3,4)
console.log(bracketTest2) //4
var bracketTest3=(null,1,2,4,5,function(x){return x})
console.log(bracketTest3) //function(x)
console.log(bracketTest3(Object)) //function Object()
console.log((1,2,3,4,null,undefined)) //prints undefined

Variable value not gets updated if function has same name as variable (JavaScript) [duplicate]

This question already has answers here:
Why can variable declarations always overwrite function declarations?
(3 answers)
javascript hoisting: what would be hoisted first — variable or function?
(2 answers)
Closed 2 years ago.
I expected the answer to be "function text" as output, why the answer is 5?
var alpha = 5;
function alpha(){}
console.log(alpha);

Javascript - Function Not Changing Global Variable [duplicate]

This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
Closed 4 years ago.
My Code (Javascript):
var num = 0;
function change(){
var num = 10;
}
change();
document.write(num);
The result should be 10 but it is showing 0. Why?
If the code is wrong, what is the correct way to do this?
You are setting another local variable called num.
Just change the inner var num to num.

Why is this variable changed inside the if block? [duplicate]

This question already has answers here:
Redeclaring a javascript variable
(8 answers)
Closed 5 years ago.
Why is the global variable some_var changed inside the if block in this example?
<script>
var some_var = 0;
var i = 5;
if (i>2)
{
var some_var = 2;
}
else
{}
console.log(some_var);
</script>
I'm guessing that if you remove the 'var' inside the if block it works properly. Must be the environment checking for initializations before running the code.

ReferenceError does not occurs? [duplicate]

This question already has answers here:
Why is no ReferenceError being thrown if a variable is used before it’s declared?
(3 answers)
does this variable get hoisted no matter what?
(1 answer)
Closed 5 years ago.
I have a question about following code blocks
var x = "123" + y;
in this code block I am getting
Uncaught ReferenceError: y is not defined
but when I try something like this
var x = "123" + y;
var y = "456";
It works fine, so why? What is the difference?

Categories

Resources