ES6 AKA Ecmascript 2015
Enhanced object literals
computed property keys
const keyName = 'name'
const product = {
[keyName] : "samsung galaxy",
price : 100,
}
console.log(product);
Method defination shorthand :-
const product = {
buy(){
console.log("brought");
}
}
product.buy()
Property shorthand
const accessToken = 'dhfhsdfkhdkfh';
const refreshToken = 'hsdifhisdhfi';
const user = {
// accessToken : accessToken,
// refreshToken : refreshToken
// enhancement in es6
accessToken,
refreshToken,
}
console.log(user);
DESTRUCTURING
const user = {
name : 'John Doe',
age : 13
}
const {name : fullName ,age} = user;
console.log(fullName,age);
///////
const data = ["Rakesh","30","Engineer"];
const [name,age,profession] = data
console.log(profession)
SPREAD (...) spread used with object and arrays
let languages = ['c','c++','JavaScript'];
let newLanguages = ['TypeScript',...languages];
console.log(newLanguages);
const settings = {
vol : 10,
brightness : 80
}
const newSetting = {...settings, contrast: 50}
console.log(newSetting);
REST (...) Rest use with funcions
const sum = (...arguments) => {
let result = 0;
for(let i=0; i<arguments.length; i++){
result += arguments[i];
}
console.log(result);
}
sum(1,2,3)
For Of Loops :-
array,string,object ,set,map
Array
const numbers = [2,3,5,6,7,8];
for(let num of numbers){
console.log(num);
}
String
const languages = "javascript";
for (let char of languages){
console.log(char);
}
Object
const person ={
name : "Himanshu",
city : "Ranchi",
brand : "Apple"
}
for (let [key,values] of Object.entries(person)){
console.log(key,values);
}
Promises (we aready read this )
SET :-
const uniqueNum = new Set();
uniqueNum.add(3);
uniqueNum.add(5);
uniqueNum.add(6);
uniqueNum.add(3);
console.log(uniqueNum.size);
const num = [4,5,6,7,4,7,6];
const uniqueNumbers = new Set(num);
console.log(uniqueNumbers);
MAP MEHTOD
const urls = new Map();
urls.set('devlopement' , 'dev.example.com');
urls.set('production','products.example.com')
console.log(urls.get('devlopement'));
more about map we already see
Classes :-
Old
function Person(name) {
this.name = name;
}
const himanshu = new Person('Himanshu');
const john = new Person('John');
console.log(john);
new
class Person{
name;
constructor(name){
this.name = name
}
greet(){
console.log("Hello");
}
}
class GreatPerson extends Person {
attitude = "COOL";
// It overrides
greet(){
console.log("Good evening");
}
}
// const himanshu = new Person('Himanshu');
// const john = new Person("John")
// console.log(himanshu.greet());
const john = new GreatPerson("John");
console.log(john.greet);
ES6 Modules