Let and Const for Block Scope
Tiffany White,
Up until ES6, you couldn’t achieve true block scoping in JavaScript. With the var keyword, the scope in a code block could be accessed in the global scope, as variable declarations are hoisted to the top of scope and the assignments are kept where they are.
For instance, take this code:
<span class=""c1"">// some code</span>
<span class=""kd"">function</span> <span class=""nx"">bar</span><span class=""p"">()</span> <span class=""p"">{</span>
<span class=""c1"">// ...</span>
<span class=""p"">}</span>
<span class=""c1"">// more code</span>
<span class=""kd"">var</span> <span class=""nx"">c</span> <span class=""o"">=</span> <span class=""mi"">3</span><span class=""p"">;</span>
}
You can make a const value immutable by adding object.freeze() method to it.
Up until ES6, you couldn’t achieve true block scoping in JavaScript. With the var keyword, the scope in a code block could be accessed in the global scope, as variable declarations are hoisted to the top of scope and the assignments are kept where they are.
For instance, take this code:
<span class=""c1"">// some code</span>
<span class=""kd"">function</span> <span class=""nx"">bar</span><span class=""p"">()</span> <span class=""p"">{</span>
<span class=""c1"">// ...</span>
<span class=""p"">}</span>
<span class=""c1"">// more code</span>
<span class=""kd"">var</span> <span class=""nx"">c</span> <span class=""o"">=</span> <span class=""mi"">3</span><span class=""p"">;</span>
}
You can make a const value immutable by adding object.freeze() method to it.
© tiff.RSS