|
| 1 | +// 📌 JavaScript Intermediate - ES6+ Features |
| 2 | + |
| 3 | +// Welcome to the fourth section of the JavaScript Intermediate tutorial! |
| 4 | +// Here, you'll learn about modern JavaScript features introduced in ES6 and beyond. |
| 5 | + |
| 6 | +// let and const (Block-Scoped Variables) |
| 7 | +let city = "New York"; |
| 8 | +const country = "USA"; |
| 9 | +console.log(city, country); |
| 10 | + |
| 11 | +// Template Literals |
| 12 | +const name = "Alice"; |
| 13 | +console.log(`Hello, ${name}!`); |
| 14 | + |
| 15 | +// Arrow Functions |
| 16 | +const add = (a, b) => a + b; |
| 17 | +console.log("Sum:", add(5, 3)); |
| 18 | + |
| 19 | +// Destructuring Arrays and Objects |
| 20 | +const person = { firstName: "John", lastName: "Doe" }; |
| 21 | +const { firstName, lastName } = person; |
| 22 | +console.log(firstName, lastName); |
| 23 | + |
| 24 | +// Spread and Rest Operators |
| 25 | +const numbers = [1, 2, 3]; |
| 26 | +const newNumbers = [...numbers, 4, 5]; |
| 27 | +console.log("Spread:", newNumbers); |
| 28 | + |
| 29 | +const sumAll = (...nums) => nums.reduce((total, num) => total + num, 0); |
| 30 | +console.log("Rest:", sumAll(1, 2, 3, 4)); |
| 31 | + |
| 32 | +// Default Parameters |
| 33 | +const greet = (name = "Guest") => console.log(`Hello, ${name}!`); |
| 34 | +greet(); |
| 35 | + |
| 36 | +// Promises and Async/Await |
| 37 | +const fetchData = () => { |
| 38 | + return new Promise((resolve) => { |
| 39 | + setTimeout(() => resolve("Data Loaded"), 2000); |
| 40 | + }); |
| 41 | +}; |
| 42 | + |
| 43 | +async function loadData() { |
| 44 | + const data = await fetchData(); |
| 45 | + console.log(data); |
| 46 | +} |
| 47 | +loadData(); |
| 48 | + |
| 49 | +// 💡 Practice using ES6+ features to write cleaner and more efficient code! |
0 commit comments