ES6, let, var, const & hoisting in simple language
Let's get started without wasting any time
What features have been introduced in the ES6 version?
Let and const keywords: These keywords are used to declare variables. The
letkeyword creates a block-scoped variable, which means that the variable can only be accessed within the block in which it is declared. The
constkeyword creates a constant variable, which means that the value of the variable cannot be changed.
Arrow functions: Arrow functions are a new way of writing functions. They are shorter and more concise than traditional functions.
Template literals: Template literals are a new way of creating strings. They allow you to embed expressions and variables inside of strings.
Default parameters: Default parameters allow you to specify a default value for a function parameter. This makes it easier to write functions that can be used with different sets of arguments.
Destructuring assignment: Destructuring assignment allows you to extract the values from an object or array into variables. This can make your code more concise and easier to read.
Promises: Promises are a new way of handling asynchronous code. They allow you to write code that can be executed in the background and then get notified when the code is finished.
What is the main difference between var, const, and let?
The main difference between
let is their scope and mutability.
Scope refers to the area of the program where a variable is accessible. A
varvariable is function-scoped, which means that it is accessible within the function in which it is declared. A
letvariable is block-scoped, which means that it is accessible within the block in which it is declared. A
constvariable is also block-scoped, but it cannot be redeclared within the block.
Mutability refers to whether the value of a variable can be changed. A
varvariable is mutable, which means that its value can be changed. A
letvariable is also mutable, but it cannot be redeclared within the block. A
constvariable is immutable, which means that its value cannot be changed.
What is hoisting?
For example, the following code will print
var a = 'abc';
Even though the
console.log() statement will try to print the value of the
a variable before it has been assigned a value.
Here are some things to keep in mind about hoisting:
Hoisting only applies to variable declarations. Function declarations are not hoisted.
Hoisting only applies to the top of the scope. This means that variables declared within a block are not hoisted to the top of the function.
Hoisting does not apply to the initialization of variables. The value of a variable is still undefined until it is assigned a value.