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

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.

Related

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

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();

Javascript - Defining a local variable after assigning a new value in a already declared global variable is overriding the global variable. Why? [duplicate]

This question already has answers here:
Javascript function scoping and hoisting
(18 answers)
Closed 1 year ago.
I am learning javascript global variable and local variable. When I did experiment with it (see jsfiddle below) then I am confuse that why after executing myFunction1() the value of variable a became 4 instead of 5. What do you think happened inside the javascript memory which made its value 4 instead of 5 ?
var a = 4;
myFunction1();
function myFunction1() {
a= 5;
var a= 6;
}
alert(a); //a = 4
myFunction2();
function myFunction2() {
a= 5;
}
alert(a); //a = 5
You've just discovered hoisting.
By declaring var a inside the function, it is available to the whole function context (even before its declaration), so when you write a = 5 the actual a is not the one you expect, it's the function-scoped a.
In the second case, a is the one you expect (outside the function)

Declaring a variable that isn't var, const or let [duplicate]

This question already has answers here:
Is using 'var' to declare variables optional? [duplicate]
(14 answers)
What is the scope of variables in JavaScript?
(27 answers)
Closed 3 years ago.
I'm refreshing my JS knowledge by doing a JS course. There's something about variables that have left me a little confused.
var variableVar = 'This is a variable pre ES6'; // var
let variableLet = 'This is a variable that can be changed'; // let
const variableConst = 'This variable cannot be changed'; // con
alsoWorks = 'This also works'; // What's this?
console.log(variableVar);
console.log(variableLet);
console.log(variableConst);
console.log(alsoWorks);
I understand var, const and let. I also noticed I don't need to declare any of these for a variable to work e.g. alsoWorks = 'This also works';. Why shouldn't I write a variable like this?

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.

Access local variable from the global context? [duplicate]

This question already has answers here:
What is a 'Closure'?
(22 answers)
How do JavaScript closures work?
(86 answers)
Closed 5 years ago.
function wrapValue(n) {
var localVariable = n;
return function() { return localVariable; };
}
var wrap1 = wrapValue(1);
var wrap2 = wrapValue(2);
console.log(wrap1());
// → 1
console.log(wrap2());
// → 2
In this example how is it allowed to access localVariable, which is local to wrapValue() function, from the global environment?
The book, included this example, stated the reason as follows:
"multiple instances of the variable can be alive at the same time".
But I didn't understand.
Your function sets a private value (localVariable) when called and returns it immediately so you can assign it to a another variable without change the private var inside function.

Categories

Resources