- let and const
- Arrow functions
- Default Parameters
- Template Literals
- Array and Object Destructuring
- Imports and Export
let and const
ES6 introduces two new keywords, let and const, to declare variables. Both of them declare block-scoped variables i.e., the variable is only accessible in the block it is defined.
- let is used to declare block-scoped variables that can be mutated i.e., reassigned.
- const is used to declare block-scoped variables that cannot be mutated. It points to the same data or object and cannot be changed.
Arrow Functions allows you to write functions in a different way which makes you code more readable and more structured. You can use arrow functions with map, filter or reduce. Also, using arrow functions inside classes (which will be explained later), automatically binds it to this object.
Default Parameters are parameters that are given as default values while declaring a function. It allows getting a default value if the argument is not passed to the function.
Template literals allow you to embed any variable or expression inside a string. Now you don’t have to use + to concatenate string variables. You can also create multi-line strings using template literals. Template literals are declared using backticks.
Array and Object Destructuring
Destructuring makes the assignment of the values of an array or object to the new variable easier. In ES6, we can just use variable names inside curly braces(for objects) or brackets (for arrays) to get any property out of an object or array. You can also rename the assignment variables and give default values.
Imports and Exports
Importing and exporting modules in ES6 is one of the useful features you will use and will most often see in modern front-end libraries like React, Vue, or Angular.
Exports are used in modules/files to explicitly exporting some variables, functions or classes to be used in other modules/files. Imports are used to import variables, functions or classes from other modules/files.
ES6 introduces a new feature called Promises to deal with asynchronous code and save us from callback hell. It can be used to perform async tasks like fetching data from an API. You can also create your own promises and use them elsewhere in the code. Promises either resolve i.e., returns data, or reject i.e., returns an error.