Popular posts from this blog
Variable Scope in javascript
Scope determines the accessibility (visibility) of variables. JavaScript has 3 types of scope: Block scope Function scope Global scope 1. Block Scope : Before ES6 (2015), JavaScript had only Global Scope and Function Scope . ES6 introduced two important new JavaScript keywords: let and const . These two keywords provide Block Scope in JavaScript. --------------------------------------------------------------------------------------------- Variables declared inside a { } block cannot be accessed from outside the block: Example: { let x = 2 ; } // x can NOT be used here ---------------------------------------------------------------------------------------------- Variables declared with the var keyword can NOT have block scope. Variables declared inside a { } block can be accessed from outside the block. Example: { var x = 2 ; } // x CAN be used here note...
Shadowing
What is shadowing in javascript ? Shadowing: Now, when a variable is declared in a certain scope having the same name defined on its outer scope and when we call the variable from the inner scope, the value assigned to the variable in the inner scope is the value that will be stored in the variable in the memory space. This is known as Shadowing or Variable Shadowing . What is illegal shadowing ? Illegal Shadowing: Now, while shadowing a variable, it should not cross the boundary of the scope, i.e. we can shadow var variable by let variable but cannot do the opposite. So, if we try to shadow let variable by var variable, it is known as Illegal Shadowing and it will give the error as “variable is already defined.”


Comments
Post a Comment