-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathES6_all_syntax.js
More file actions
77 lines (76 loc) · 2.27 KB
/
ES6_all_syntax.js
File metadata and controls
77 lines (76 loc) · 2.27 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
// 1. Variables (let, const)
// let: Declares block-scoped variables that can be reassigned.
let x = 10;
// const: Declares block-scoped constants whose values cannot be re-assigned.
const pi = 3.14;
// 2. Arrow Functions
// Shorter syntax for writing functions, with implicit return for single expressions.
// Traditional function
function add(a, b) {
return a + b;
}
// Arrow function
const add = (a, b) => a + b;
// 3. Template Literals
// Enhanced string literals allowing embedded expressions.
const name = 'Alice';
const greeting = `Hello, ${name}!`;
// 4. Destructuring Assignment
// Extracts values from arrays or objects into distinct variables.
// Array destructuring
const [first, second] = [1, 2, 3];
// Object destructuring
const { firstName, lastName } = { firstName: 'John', lastName: 'Doe' };
// 5. Default Parameters
// Provides default values for function parameters.
function greet(name = 'Guest') {
return `Hello, ${name}!`;
}
// 6. Rest Parameters
// Represents an indefinite number of arguments as an array.
function sum(...numbers) {
return numbers.reduce((acc, curr) => acc + curr, 0);
}
// 7. Spread Operator
// Allows an iterable to be expanded into individual elements.
const numbers = [1, 2, 3];
console.log(...numbers); // 1 2 3
// 8. Classes
// Syntactical sugar over JavaScript's prototype-based inheritance.
class Animal {
constructor(name) {
this.name = name;
}
speak() {
return `${this.name} makes a noise.`;
}
}
// 9. Modules
// Allows JavaScript code to be split into reusable modules.
// Exporting module
export const PI = 3.14;
// Importing module
import { PI } from './math';
// 10. Promises
// Provides a cleaner alternative to callbacks for asynchronous programming.
function fetchData() {
return new Promise((resolve, reject) => {
// Asynchronous operation
if (success) {
resolve(data);
} else {
reject(error);
}
});
}
// 11. Async/Await
// Syntactic sugar for working with asynchronous code, built on top of Promises.
async function fetchData() {
try {
let response = await fetch('https://api.example.com/data');
let data = await response.json();
return data;
} catch (error) {
console.error('Error fetching data:', error);
}
}