let
- No variable hoisting, first declare variable then use. In var you can use variable hoisting.
let
is scoping at block level.
const
- const is used for constant declarations.
- For primitive types the value can't change.
- For array and objects as the reference is stored in const, the value can change.
- The fat arrow function keeps the context where it is defined.
- If normal function is called from event listener the
this
will refer to the html element object. - In case of fat arrow the
this
will still refer to window object. - No need for bind and call on fat arrow function.
- rest is used in arguments where you pass multiple values.
- spread is used to spread an array into individual values.
let numbers = [1,2,3];
let [a,b] = numbers;
let [x = 'def', ,y] = numbers;
[b,a] = [a,b];
let [c, ...d] = numbers;
let obj = {name: 'Asif', age: 21};
let {name, age} = obj;
other.js
export let as = 'cfv';
export const ds = 12;
export default let ab = 'def';
index.js
import {as,ds} from 'other';
import ab from 'other';
class Parent {
constructor(name){
this._name = name;
}
get name(){
return this._name;
}
set name(name){
this._name = name;
}
greet(){
console.log('welcome ' + this._name);
}
}
class Child extends Parent {
constructor(age){
super();
this.age = age;
}
greet(){
console.log('welome ' + this._name + 'you are '+ this.age);
}
static fruit(fruit){
console.log('my fav fruit is '+ fruit);
}
}
let parent = new Parent('Asif');
let child = new Child(23);
parent.greet();
child.greet();
Child.fruit('Mango');