Arrow Functions:
Arrow functions were introduced in ES6. Arrow functions allow us to write shorter function syntax. Arrow function .this represents the owner of the function.
If the function has only one statement, and the statement returns a value, you can remove the brackets and the return keyword.
[var helloStr = function(){If you have a single parameter and return value then no need braces, and if you have a more then one parameter then you need braces.
return "Hello";
}
// using arrow function
var helloStr = () => "Hello";]
[// single parameter
var discount = amount => amount *0.10;
// Multiple parameters
var sum = ( a, b) => a+b;
// Multiple parameters with multiple lines of code, need return keywork
var tax = (basic, deductions, others) => {
var sum = basic*0.10;
return sum +deductions * others;
}]
Rest parameter(...):
The rest parameter allows us to represent an indefinite number of arguments as an array. The rest parameter have to be the last argument, as its job is to collect all the remaining arguments into an array.[var finalAmount = (discount, ...products) => {
let sum = 0;
for(let i of products){
sum+=i;
}
return sum * discount;
}]
Spread Operator:
It's similar to the Rest operator. It will take an array and pass to a list of parameters to a function.[var x = [1,2,3];
var y = [...x]; // op: 1,2,3
var z = [...x,...[4,5]]; // op: 1,2,3,4,5
var j = [..."123"]; // op: "1","2","3"]
Object Literals:
Object literal is a comma-separated list of name-value pairs wrapped in object. Object literals encapsulate data, enclosing it in a tidy package.[var price = 10;one more example.
var quantity = 5;
var bill = {price, quantity, "total amount" : () => this.price*this.quantity};
bill.price; // op: 10
bill["total amount"](); // OP: 50]
[var name = 'Apple';
var price = 10;
var details = {[name]:price};
details; // op: {Apple: 10}
details.Apple; // op: 10 ]
For..Of loop:
Loops through the elements/values of an iterable object.[var fruits = ['Apple', 'Banana','Kiwi'];one more example.
for(var item of fruits){
console.log(item);
}
// Op: Apple
// Banana
// Kiwi]
[var helloStr = 'Hello';yes, it can also iterate strings.
for(var item of helloStr){
console.log(item);
}
// OP: H
// e
// l(2)
// 0]
Template literals:
Template literals are introduced in ES6. Javascript Template literals are string literals that allow embedded expression inside it.
[var helloStr = 'Hello';one more example.
console.log(`This ${"is "+ helloStr} world!`);
// OP: this isHello world!]
[var x = `A
B
C`;
console.log(x);
// OP: A
//B
//C ]
yes, whitespaces(Tab, New lines) are maintained.
COMMENTS