Variable Declarations with Same Name

LP496

If I have this code below:

var greeting = 'Hola';

(function(spanishGreeting, name){
    var spanishGreeting = 'Como estas!';
    spanishGreeting = 'HOLA!'
    console.log(spanishGreeting);
}(greeting,'John'));

console.log(greeting);

Can you explain to if my understanding of the above code is correct? So first inside the IIFE:

var spanishGreeting = 'HOLA!!'; 

That line of code creates a whole new variable with the same name as the parameter that is passed into the IIFE (this new variable declaration has nothing to do with that parameter, spanishGreeting, passed into the IIFE correct?).

Also:

spanishGreeting = 'Como estas!'

will look for the spanishGreeting variable in the current Execution Context of the IIFE. Now the problem is there are two spanishGreeting variables in the current Execution stack. The one in the IIFE parameter and the one I just created:

var spanishGreeting = 'HOLA!!';

How does the JS engine know which one to use?

Bergi

That line of code creates a whole new variable with the same name as the parameter

No, in fact it doesn't. There is only one scope (the function scope) with one variable of the name spanishGreeting (and a second variable that goes by name).

The var is ignored, it doesn't create a second variable after the parameter did already introduce it.

When you call the function, the variable is first filled with the argument ('Hola'), then it is overwritten with the value 'Como estas!', and then with the value 'HOLA!'.


However, there is a situation where you can have two different variables that go by the same name - when they are in different scopes:

var greeting = "Hello outer!";
function greet() {
    var greeting = "Hello inner!";
    console.log(greeting);
}
greet();

How does the JS engine know which one to use?

Here, the JS engine just uses the local variable (which could be declared via var or as a parameter, doesn't make a difference) in the current scope. The inner variable is said to shadow the outer variable.


Btw, if you use the ES6 let keyword to declare your variables, the engine will throw an error if you attempt to re-declare an already defined variable (in the same scope).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Variable with same name in subclass and superclass

Same variable, different name

Why was the name 'let' chosen for block-scoped variable declarations in JavaScript?

Local variable and instance variable has the same name

Placement of instance variable declarations

'filenames are used to distinguish private declarations of the same name' error

Algorithms new variable with same name

Swift, variable with same as a method name

C: Global variable and local variable of same name

Local variable in Java with the same name as a global variable

Same variable name in Data types

creating a variable with name as same as the name of items in list

Access global variable with same name as instance variable

Understanding declarations in the same scope

Class have a variable name same as function name

Check if a string is same as a variable name

Pointer and regular variable with the same name

Squash variable declarations that are declared by the same function

Inherit same variable but different name

Use a class name to call variable of the same name

Can class name and variable name be same in java?

Explicit package name and masking earlier declarations in the same statement

R: variable and function of same name

Merging declarations of namespace and variable

variable and function with same name clobbering

Is it OK to name a variable with the same name as a structure tag?

Multiple Declarations of [variable name]

why are the following declarations not the same?

check if variable name is the same as string