ES6 Features In Javascript
Here are some ES6 Feature that you can use to develop your web application.
1 . Arrow function
It provides a concise way to write functions in JavaScript.
The function keyword, the this keyword, the return keyword, and the curly brackets are not needed in this function.
Example :
const sum = (a, b) => a + b
console.log(sum(3, 2) //return 5
2 . Let keyword
It allow you to declare variable within specified scope. Outside scope it will not work.
Example :
let a = 2
if(a==2){
a=5
console.log(a)// return 5
}
console.log(a) // return 2
3 . Const keyword
It allow you to declare variable as constant.
Example :
const a = 2
console.log(a)// return 2
a = 22
console.log(a) // return error
4 . Template String
Template string use for replace variable value inside the statements.
Example:
const a = 5
const data = `There are ${a} pens.`
console.log(data) // There are 5 pens.
5 . Spread Operator
Spread operator use for connecting array, add new element in array, reassign the existing array to new.
Example:
const a = [1,2,3]
const b = [4,5,6]
const c = […a, …b]
console.log(c) //return [1,2,3,4,5,6]
6 . String Includes
Includes method use to check string content specified value. It return true if string content that value.
Example:
const data = “abcdefg”
console.log(data.include(“cd”) // return true
7 . String startsWith
startsWith method use to check string starts with specified value. It return true if string starts with that value.
Example:
const data = “abcdefg”
console.log(data.startsWith(“ab”) // return true
8 . String endsWith
endsWith method use to check string ends with specified value. It return true if string end with that value.
Example:
const data = “abcdefg”
console.log(data.endsWith(“fg”) // return true
9 . Destructuring Object
It allow you to destruct object and access variable of an object directly.
Example:
let obj = { a:10, b:20}
let {a, b} obj
console.log(a, b) //return 10,20
10 . Multi-line Strings in ES6
Using the back-ticks you can define multiple string.
Example:
const line=`line1
line2
line3`
console.log(line)
//line1
line2
line3
11 . Object Literals
Object literals make it easy to quickly create objects with properties inside the curly braces instead of defining source and destination variable.
Example:
function test(a,b){
return{
a,b
}
}
console.log(test(5,1))
//{a:5,b:1}
12 . Default Parameter
It allow to pass default parameter into the function itself.
Example:
let data=function test(a=5,b=1){
return a + b
}
console.log(data)
//6
Thank You for reading this article. Hope this article will help you.