JS let and const
In this tutorial, you will learn about JavaScript variable declaration using let and const keyword.
JS let keyword
The let keyword was introduced in ES6 (2015).
Variables defined with let cannot be Redeclared.
Variables defined with let must be Declared before use.
Variables defined with let have Block Scope.
Cannot be Redeclared
Variables defined with let cannot be redeclared.
With let you can not do this:
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:
JS Const keyword
The const keyword was introduced in ES6 (2015).
Variables defined with const cannot be Redeclared.
Variables defined with const cannot be Reassigned.
Variables defined with const have Block Scope as let have.
Cannot be Reassigned
Run codeWhen to use JavaScript const?
Always declare a variable with const when you know that the value should not be changed.
Use const when you declare:A new Array
A new Object
A new Function
A new RegExp
Now that you know how to declare variables using var, let and const, let's learn how we can use document methods.
Share this page on :
© 2022 AnalyzeCode.com All rights reserved.