DEV Community

Margia Sultana
Margia Sultana

Posted on

Feature of ES6

JavaScript ES6 is also known as ECMAScript 2015 or ECMAScript 6.ECMAScript 2015 or ES2015 is an update to the JavaScript programming language. I provide a brief summary of some features of ES6 to start quickly in ES6.

JavaScript var:
When we declare a variable using the var keyword the scope of the variable is either global or local. If we declare a variable outside of a function, the scope of the variable is global. When we declare a variable inside a function, the scope of the variable is local. For example:
var variable_name;
Here variable_name is a global variable that is accessible by any function.

function increase() {
var variable_name= 10;
}

In this example, the variable_name variable is local to the increase() function. It cannot be accessible outside of the function.

JavaScript let:
ES6 provides a new way of declaring a variable by using the let keyword. The let keyword is similar to the var keyword, except that these variables are blocked-scope. This means they are only accessible within a particular block. For example:
let variable_name;

JavaScript const:
ES6 provides a new way of declaring a constant by using the const keyword. In ES6 const will have block scoping just like the let keyword. The value of a constant cannot change through re-assignment, and it can’t be redeclared. A constant cannot share its name with a function or a variable in the same scope. For example:
const CONSTANT_NAME = value;

JavaScript Classes:
JavaScript class is used to create an object. Keyword class is used to create a class. Class is similar to a constructor function.For example:
class Person {
constructor(name) {
this.name = name;
}
}
JavaScript Arrow Function:
ES6 arrow functions provide us the way to write a shorter syntax compared to the function expression.For example,

// function expression
let x = function(x, y) {
return x * y;
}
// function expression using arrow function
let x = (x, y) => x * y;
Default perimeter value:
When we do not provide a parameter in a function then it became an undefined value that’s why we specify default parameters to set a default value in the parameters of a function. In the ES6 version, you can pass default values in the function parameters. For example,
function say(message='Hi') {
console.log(message);
}

say(); // 'Hi'
say('Hello') // 'Hello'

JavaScript Destructuring:
The destructuring syntax makes it easier to assign values to a new variable. For example,
// before you would do something like this
const person = {
name: 'Marjia',
age: 23,
gender: 'female'

}

let name = person.name;
let age = person.age;
let gender = person.gender;

//now we do something like this
const person = {
name: 'Marjia',
age: 23,
gender: 'female'

}

let { name, age, gender } = person;

Top comments (0)