You’ll learn :
about constant and const keyword and variables declared with let keyword and the differences between these types of variables.
constant :
we all know about variable and it is container that hold specific value during program’s execution, and this value can be changed whenever to whatever the programmer want.
constants are the same thing but the only difference is the const value can not be changed during the code execution, the initiated value when we declare the constant will remain the same the whole time.
Constants are block-scoped, much like variables declared using the let keyword. The value of a constant can't be changed through reassignment (i.e. by using the assignment operator), and it can't be redeclared (i.e. through a variable declaration). However, if a constant is an object or array its properties or items can be updated or removed. MDN.
The const keyword was introduced in ES6 (2015), Variables defined with const cannot be Redeclared, Variables defined with const cannot be Reassigned. w3Schools.
Syntax :
const identifier = anyValue;
const x = 10 ;
const keyword , identifier, assignment operator, any value
variable declared by const keyword, its value can not be changed (object’s properties and array’s elements still can be updated or removed), it can not be redeclared, and last thing its scope will be Blocked-Scope.
you’ll learn about scopes in later tutorial
Let keyword :
The let statement declares a block-scoped local variable, optionally initializing it to a value. MDN
variables declared with let keyword and variables declared with var do the same thing , they are variables and its rules pretty much the same, the key difference is their scope limitation, var is function-scope and let is blocked scope.
if you want to read and understand what scope is.
Rules for let variables :
- 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. w3schools
Syntax :
let identifier = anyValue;
let x ;
let keyword, identifier, assignment operator and a given value.
you can drop the initiated value when declaring let variables and can assign it later, the default value will be undefined when no value assigned to the let variable. expression.
Let vs Var vs Const :
let and var variables can be reassigned, const can’t
var variables can be redeclared, let and const can’t
let and const are blocked-scope, while var is a function-scope
let and var can be declared without initial value, const can’t
let and var default values without initialization values will be undefined
More :
The Difference of “var” vs “let” vs “const” in Javascript | by Megan Lo | The Startup | Medium
Megan Lo ・ ・
Medium
References and useful links :
The Difference of “var” vs “let” vs “const” in Javascript | by Megan Lo | The Startup | Medium
Megan Lo ・ ・
Medium
Thanks for reading, feel free to ask any question about javascript or about this series, I appreciate any feedback or advises to improve My content.
find me on twitter, github and my portfolio.
Top comments (0)