About ES6 syntax 1 (let and const)
This article introduced ES6 syntax (let and const)
This post is to summarize ES6 syntax, which I recently revisited in preparation for a coding test.
ES6 stands for ECMAScript, which was released in 2015, and is a standardized version of JavaScript.
About variable declarations (let, const)
These are the keywords used to declare block-scoped variables.
- let: No duplicate declarations are allowed, so it cannot be declared multiple times with the same name. The value can be reassigned after the variable is declared.
- const: The declared variable is a constant, so the value cannot be changed once assigned.
Example of a let declaration
let count = 0;
count = 5; // can be reassigned
if (true) {
let count = 10; // Declare a new block scope variable
console.log(count); // output: 10
}
console.log(count); // output: 5 (variables outside the block are not affected)
Example of a const declaration
const PI = 3.14159;
PI = 3.14; // Error - constants cannot be reassigned
if (true) {
const age = 30; // Declare a new block scope constant
console.log(age); // output: 30
}
console.log(age); // Error - constant cannot be accessed from outside the block
What is a block-scoped variable?
A variable that is valid only within the block in which it is declared.