Repository navigation
Expand file tree
/
Copy pathecmastand.js
More file actions
94 lines (79 loc) · 2.59 KB
/
Copy pathecmastand.js
File metadata and controls
94 lines (79 loc) · 2.59 KB
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
/**
* ECMAScript Standards Overview
* -----------------------------
* ECMAScript (often abbreviated as ES) is the standardized specification for JavaScript
* maintained by ECMA International in the ECMA-262 standard.
*
* Major ECMAScript versions:
* - ES1-3: Early versions (1997-1999)
* - ES5: Released in 2009, added strict mode, JSON support, etc.
* - ES6/ES2015: Major update with classes, arrow functions, promises, etc.
* - ES2016-Present: Yearly release cycle with incremental updates
*/
// ES5 Features
"use strict"; // Strict mode (ES5)
var es5Array = [1, 2, 3, 4, 5];
es5Array.forEach(function(item) {
console.log("ES5 forEach:", item);
});
// ES6/ES2015 Features
const es6Feature = () => {
// Arrow functions
const numbers = [1, 2, 3, 4, 5];
// Array methods
const doubled = numbers.map(n => n * 2);
console.log("ES6 map:", doubled);
// Template literals
const name = "JavaScript";
console.log(`ES6 template literals: Hello, ${name}!`);
// Destructuring
const person = { firstName: "John", lastName: "Doe", age: 30 };
const { firstName, lastName } = person;
console.log(`ES6 destructuring: ${firstName} ${lastName}`);
// Classes
class User {
constructor(name) {
this.name = name;
}
sayHello() {
return `Hello, my name is ${this.name}`;
}
}
const user = new User("Alice");
console.log("ES6 classes:", user.sayHello());
// Promises
const simplePromise = new Promise((resolve) => {
setTimeout(() => resolve("ES6 Promise resolved"), 100);
});
simplePromise.then(console.log);
};
// ES2017+ Features
const modernFeatures = async () => {
// Async/await (ES2017)
try {
const result = await new Promise(resolve =>
setTimeout(() => resolve("ES2017 async/await"), 100)
);
console.log(result);
// Object rest/spread (ES2018)
const obj1 = { a: 1, b: 2 };
const obj2 = { ...obj1, c: 3 };
console.log("ES2018 spread:", obj2);
// Optional chaining (ES2020)
const user = {
profile: {
address: null
}
};
console.log("ES2020 optional chaining:", user?.profile?.address?.street);
// Nullish coalescing (ES2020)
const value = null;
const defaultValue = value ?? "default";
console.log("ES2020 nullish coalescing:", defaultValue);
} catch (error) {
console.error(error);
}
};
// Execute examples
es6Feature();
modernFeatures();