JavaScript · 1030 bytes Raw Blame History
1 // Modern JavaScript example
2 class Calculator {
3 constructor() {
4 this.result = 0;
5 }
6
7 add(a, b) {
8 this.result = a + b;
9 return this.result;
10 }
11
12 multiply(a, b) {
13 return a * b;
14 }
15 }
16
17 // Arrow function
18 const fibonacci = (n) => {
19 if (n <= 1) return n;
20 return fibonacci(n - 1) + fibonacci(n - 2);
21 };
22
23 // Async/await example
24 async function fetchData(url) {
25 try {
26 const response = await fetch(url);
27 const data = await response.json();
28 return data;
29 } catch (error) {
30 console.error("Error:", error);
31 }
32 }
33
34 // Main execution
35 const calc = new Calculator();
36 console.log(`5 + 3 = ${calc.add(5, 3)}`);
37
38 // Array methods
39 const numbers = [1, 2, 3, 4, 5];
40 const doubled = numbers.map(x => x * 2);
41 const evens = numbers.filter(x => x % 2 === 0);
42
43 // Object destructuring
44 const { result } = calc;
45 console.log("Result:", result);
46
47 // Template literals and promises
48 Promise.resolve("Hello")
49 .then(msg => `${msg}, World!`)
50 .then(console.log);