-
Notifications
You must be signed in to change notification settings - Fork 1
/
ES6
100 lines (80 loc) · 4.15 KB
/
ES6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
var, let and const keywords-------------------------------------------------
var lets you override its values
it can be declared globally or within a function locally
let does not lets you override its code and throws error on trying to do so
it has local scope
const keyword lets you declare a variable and has 'read-only' property.
they cannot be reassigned
"use strict"----------------------------------------------------------------
enables strict mode and watched for common mistakes
array declaration using const-----------------------------------------------
array declared using const are mutable. example:-\
const s = [2, 3, 4];
s[1] = 5; //s = [2, 5, 4]
freezing objects to prevent mutation----------------------------------------
to prevent any kind of mutations we use
Object.freeze(objectname);
example:-
const things = {apple, cat, ball};
Object.freeze(things); //no mutation is possible in things
creating inline functions / concise anonymous functions---------------------
to create a concise function we use the following syntax:-
const afun = function(){
const aval = "value";
return aconst;
}
OR
const afun= () => {
const aval = "value";
return aconst;
}
using the spread operator to compute values--------------------------------
the spread operator is helpful where a number of argu,ents are expected.
Eg:- for calculating the maximum number in an array. using var declaration with apply, math can be tedious and thus spread operator can be useful
const arr = [ 1, 23, 65, 78];
const maximum = Math.max(...arr);
using destructive assignment to assign values from objects directly--------
we can shorten a piece of code to assign values directly from the objects like :-
const jhon = { age: 30, title: sikle};
const age = jhon.age;
const title = jhon.title;
USING DESTRUCTIVE ASSIGNMENT FOR ASSIGNING VALUE
const jhon = { age: 30, title: sikle};
const {age, title} = jhon; //does the same as the above code
TO ASSIGN A DIFFERENT VARIABLE NAME
const jhon = { age: 30, title: sikle};
const {age: user_age, title: user_title} = jhon;
TO ASSIGN VALUE FROM A NESTED OBJECT
const people = { jhon { age:21, title:doe }};
const {jhon :{ age, title} = people;
using destructive assignment to access the elements of an array-------------
for assigning values to array directly we can use the variable name
Eg:-
const [a, b , , , c] = [1, 2, 3, 4, 5, 6, 7];
console.log(a, b, c); //1, 2, 6
//for swapping values
a = 3;
b = 6;
[a,b] = [b,a];
using destructive assignment with rest parameter---------------------------
we can use rest parameter and destructive assignment together too
Eg:-
const [a,b, ...arr] = [1, 2, 3, 4, 5, 6, 7];
console.log(a,b); //1, 2
console.log(...arr); //3, 4, 5, 6, 7
writing functions directly using the destructive assignment-----------------
we can directly assign vales in a function using destructive assigment:-
Eg:-
const temp = { min: 10, max: 40, day: 32 };
const avg_temp = ({ min, max }) => (min + max) / 2.0;
creating strings with template litrals--------------------------------------
template litral helps in making complex strings easier
The string should start and end with ` and not '. the assignement is done by ${..}
Eg:-
const person = { name: abc, age: 50};
const greetings = ` Hi everyone , my name is ${person.name}. I am ${person.age} years old.`;
writing a concise object declaration with ES6-------------------------------
rather than writing {x:x, y:y} we can directly assign by writing ({x, y})
Eg:-
person( "abc", 28, "female");
const person = (name, age, gender) => ({ name, age gender})