-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathES6.js
More file actions
54 lines (44 loc) · 1.11 KB
/
Copy pathES6.js
File metadata and controls
54 lines (44 loc) · 1.11 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
// 1. Arrow Functions
const add = (a, b) => a + b;
const square = x => x * x;
// Example usage
console.log(add(2, 3)); // 5
console.log(square(4)); // 16
// 2. Template Literals
const name = "John";
const greeting = `Hello, ${name}!`;
// Example usage
console.log(greeting); // Hello, John!
// 3. Destructuring Assignment
const person = { name: "Alice", age: 30 };
const { name: personName, age } = person;
// Example usage
console.log(personName); // Alice
console.log(age); // 30
// 4. Classes
class Animal {
constructor(name) {
this.name = name;
}
speak() {
console.log(`${this.name} makes a noise.`);
}
}
class Dog extends Animal {
speak() {
console.log(`${this.name} barks.`);
}
}
// Example usage
const dog = new Dog('Rex');
dog.speak(); // Rex barks.
// 5. Modules (import/export)
// Note: This part will be commented out as it requires a module system to run.
export const pi = 3.14;
export function add(a, b) {
return a + b;
}
// Example usage (in another file):
// import { pi, add } from './es6_examples.js';
// console.log(pi); // 3.14
// console.log(add(2, 3)); // 5