- Variable:
- var (ES5): Variable scope is available within the function in which it is declared.
- let (ES6):
- let scope works only within the block in which it is declared like {}. be it function(){}, if(){} or just {}, etc.
- Does not allow redeclaring variable within the scope (Reduces unnecessary redeclaration of the same variable).
- const (ES6)
- Another way to declare block-scoped variables and value must be assigned to the variable.
- Throws JS error if const variable’s value is changed or if no value is set immediately during declaration.
- But object property or array member can be changed if constant is an object or array that can add/remove property/member or can change the value.
- Parameters:
- Template Literals:
- Arrow Functions:
- Array:
- Some new static class methods, as well as new methods on the Array prototype, are introduced like find(), findIndex(), fill() etc ( http://exploringjs.com/es6/ch_arrays.html )
- Some new static class methods, as well as new methods on the Array prototype, are introduced like find(), findIndex(), fill() etc ( http://exploringjs.com/es6/ch_arrays.html )
- String:
- Maps:
- Class Constructor and Methods:
- Math:
- New methods have been added to the Math object.
- Math.sign: returns sign of a number as 1, -1 or 0.
Math.sign(5); // 1
Math.sign(-9); // -1 - Math.trunc: returns passed number without fractional digits.
Math.trunc(5.9); // 5
Math.trunc(5.123); // 5 - Math.cbrt: returns cube root of a number.
Math.cbrt(64); // 4
- Math.sign: returns sign of a number as 1, -1 or 0.
- New methods have been added to the Math object.
- Spread Operator:
- Destructuring:
- Provides a convenient way to extract data from objects or arrays.
- Values can be assigned to multiple variables in one shot. and with this, you can also easily swap variable values.
- It also works with objects (need to make sure to have matching keys)
- This mechanism can also be used to change variable names.
- Can be used for multiple return values.
- Can be used to assign default values to argument objects.
- Modules:
- It includes both a new syntax and a loader mechanism for modules.
// lib/math.js (create a math.js file in lib folder to export variable and methods.export function sum(x, y) {
return x + y;
}
export var pi = 3.141593;// app.js (create your app.js file to import methods/variable from module created above.
import { sum, pi } from “lib/math”;
console.log(‘2π = ‘ + sum(pi, pi));
- It includes both a new syntax and a loader mechanism for modules.
- Symbols:
- Symbols are a new primitive data type, like Number and String.
- It can be used to create unique identifiers for object properties or can create unique constants.
const MY_CONSTANT = Symbol();
I hope this post gives you some clear and simple idea of what is there in ES6. More can be read from below reference links:
References: