-
Notifications
You must be signed in to change notification settings - Fork 0
/
es6.js
108 lines (75 loc) · 2.35 KB
/
es6.js
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
101
102
103
104
105
106
107
108
// es6 features
console.log("hello world")
//let and const for variable Declaration
let city= 'New York'
city='Los Angeles'
const County = 'USA'
// COUNTY='Canada' //Error: Cant Reassign a const
// allow function
// provide shorter syntax for writing a Function
// normal way
function calculate(){
}
// allow function
const add = ()=>{}
// Template Literals
//Template literals use backticks (`) and allow embedding variables and expressions with ${}. They also support multiline strings directly.
const name ='alice'
const messeage=`hello, ${name}` //hello alice
// Destructuring assignment
//Destructuring allows you to extract values from arrays and objects into distinct variables
// array Destrucuting
const colors = ['red','green','blue']
const [first,second]=colors;
// object Destucturing
const user={
nam:"simon",
course:"computer science",
year:"3"
}
const{nam,course,year}=user
console.log(nam) //simon
// Speard and Rest operators (...)
//Spread (...) expands elements in arrays or properties in objects.
//Rest (...) collects multiple arguments or array elements into a single array.
// spread array
const arr1 = [1,2,3];
const arr2=[...arr1,4,5]; //[1,2,3,4,5]
// spraed in Objects
const user1={
name:'simon',
age:'30'
}
const userwithcity={
...user,
city:'Newyork'
}
// rest Parameter
function sum(...numbers){
return numbers.reduce((acc,num)=>acc+num,0);
}
console.log(sum(1,2,3,4));//10
// modules
export const PI =3.14159;
export function add(a,b){
return a+b;
}
// importing
import {pi,add} from './math.js'
console.log(add(s,3)); //5
// promise
//Promises allow you to handle asynchronous operations, providing a more readable way to handle success and failure cases compared to callbacks
const fetchData = new Promise((resolve, reject) => {
setTimeout(() => resolve("Data fetched!"), 1000);
});
fetchData
.then(result => console.log(result)) // "Data fetched!"
.catch(error => console.error(error));
// New Array Methods
//find: Returns the first element that satisfies a condition.
//findIndex: Returns the index of the first element that satisfies a condition.
//includes: Checks if an array contains a certain value.
const numbers = [10, 20, 30];
const found = numbers.find(num => num > 15); // 20
const index = numbers.findIndex(num => num > 15); // 1
const hasValue = numbers.includes(20); // true