JavaScript: The React Parts

Block Scope

This section we will dive into new features in JavaScript.

One thing that many JavaScript developer didn't realize is that the default scope of variable in JavaScript is function:

js

This is in contrast with most programming languages like Java or C in which the scope of the variable is block (the closest curly braces pair {}).

This behavior applies for for loop as well:

js

Now JavaScript has two new keywords, let and const, which will scoped within the block.

js

const behaves like let, but it cannot be reassigned.

js

Today, most JavaScript developers use const by default, and switch to let when they need to reassign the variable. Very few developer use var anymore.

Note that const cannot be reassigned doesn't mean it cannot be changed. If the variable is not a primitive value, i.e. boolean, string, or number, it could be modified.

js
Comments

There is no comment on this post yet.

Issue on this page? Report here