Level Up Your JavaScript with ES6!
What is the DOM ?
DOM Manipulation
DOM Elements
DOM Events
# What is the DOM ?
ES6, also known as ECMAScript 2015, introduced a plethora of new features to JavaScript, making the language more powerful and developer-friendly. It addressed common issues, streamlined syntax, and brought JavaScript closer to other modern programming languages.
# What is HTML?
# Essential Extensions
The New Guards of Variables!
# VSC Interface Overview
let age = 25; // Can be updated
const PI = 3.14; // Cannot be re-assigned
// Block scope demonstration
if (true) {
let x = 10;
const y = 20;
console.log(x, y); // Works inside the block
}
// console.log(x, y); // Error: x and y are not defined outside the block
# Attributes
# VSC Interface Overview
const name = "Alice";
const age = 30;
const greeting = `Hello, my name is ${name} and I am ${age} years old.`;
console.log(greeting);
// Multi-line strings
const multiLine = `
This is a string
spanning multiple
lines.`;
console.log(multiLine);
Template literals allow embedding expressions and multi-line strings, using backticks (`) instead of quotes.
# VSC Interface Overview
condition ? expressionIfTrue : expressionIfFalse;
The ternary operator is a concise alternative to if-else statements.
Syntax
const age = 20;
const canVote = age >= 18 ? "Yes, you can vote." : "No, you cannot vote.";
console.log(canVote);
Exemple
# VSC Interface Overview
const add = (a, b) => a + b; // Implicit return
const greet = name => `Hello, ${name}!`; // Single argument doesn't need parentheses
const sayHi = () => console.log("Hi!"); // No arguments
Arrow functions provide a shorthand way to write functions.
Syntax
const numbers = [1, 2, 3];
const squared = numbers.map(num => num ** 2);
console.log(squared); // [1, 4, 9]
Exemple
# VSC Interface Overview
ES6 brought several new methods for array manipulation:
map
: Transforms each element.filter
: Filters elements based on a condition.reduce
: Reduces the array to a single value.find
: Returns the first element matching a condition.const numbers = [1, 2, 3, 4, 5];
const evens = numbers.filter(num => num % 2 === 0);
const squares = numbers.map(num => num ** 2);
const sum = numbers.reduce((acc, num) => acc + num, 0);
console.log(evens); // [2, 4]
console.log(squares); // [1, 4, 9, 16, 25]
console.log(sum); // 15
# VSC Interface Overview
Destructuring is an ES6 feature that simplifies the process of extracting values from arrays or objects into distinct variables. It makes the code more concise and readable, especially when dealing with complex data structures.
# VSC Interface Overview
You can extract values from an array based on their positions.
const fruits = ["apple", "banana", "cherry"];
// Destructure into variables
const [first, second, third] = fruits;
console.log(first); // Output: "apple"
console.log(second); // Output: "banana"
console.log(third); // Output: "cherry"
Array Destructuring:
# VSC Interface Overview
You can skip unwanted elements by leaving placeholders.
const numbers = [10, 20, 30, 40];
const [, second, , fourth] = numbers;
console.log(second); // Output: 20
console.log(fourth); // Output: 40
Skipping Elements:
# VSC Interface Overview
Provide default values in case the array doesn’t have enough elements.
const colors = ["red"];
const [primary, secondary = "blue"] = colors;
console.log(primary); // Output: "red"
console.log(secondary); // Output: "blue"
Default Values:
# VSC Interface Overview
Destructuring can swap variable values without a temporary variable.
let a = 1;
let b = 2;
[a, b] = [b, a];
console.log(a); // Output: 2
console.log(b); // Output: 1
Swapping Variables:
# VSC Interface Overview
Extract properties from objects and assign them to variables.
const user = { name: "Alice", age: 25 };
// Destructure into variables
const { name, age } = user;
console.log(name); // Output: "Alice"
console.log(age); // Output: 25
Basics:
# VSC Interface Overview
You can rename variables while destructuring.
const user = { name: "Bob", age: 30 };
const { name: userName, age: userAge } = user;
console.log(userName); // Output: "Bob"
console.log(userAge); // Output: 30
Renaming Variables:
# VSC Interface Overview
Provide defaults for missing properties.
const product = { title: "Laptop" };
const { title, price = 1000 } = product;
console.log(title); // Output: "Laptop"
console.log(price); // Output: 1000
Default Values:
# VSC Interface Overview
Extract values from nested objects.
const user = {
name: "Charlie",
address: {
city: "New York",
zip: 10001
}
};
const { name, address: { city, zip } } = user;
console.log(name); // Output: "Charlie"
console.log(city); // Output: "New York"
console.log(zip); // Output: 10001
Nested Destructuring:
# VSC Interface Overview
Combine destructuring for arrays and objects.
const data = {
title: "Post Title",
tags: ["JavaScript", "ES6", "Coding"]
};
const { title, tags: [firstTag, secondTag] } = data;
console.log(title); // Output: "Post Title"
console.log(firstTag); // Output: "JavaScript"
console.log(secondTag); // Output: "ES6"
# VSC Interface Overview
Destructure directly in function parameters to make code concise.
const printUser = ({ name, age }) => {
console.log(`Name: ${name}, Age: ${age}`);
};
const user = { name: "Diana", age: 28 };
printUser(user);
// Output: Name: Diana, Age: 28
# VSC Interface Overview
With Default Values:
const greet = ({ name = "Guest", age = "unknown" }) => {
console.log(`Hello, ${name}. You are ${age} years old.`);
};
greet({ name: "Eve" });
// Output: Hello, Eve. You are unknown years old.
Provide defaults in function parameters.
# VSC Interface Overview
The spread
and rest
operators in ES6 use the same syntax: three dots (...
). However, their purpose and behavior are different.
# VSC Interface Overview
What It Does:
# VSC Interface Overview
1.1 Expanding Arrays:
const numbers = [1, 2, 3];
const newNumbers = [...numbers, 4, 5]; // Adds new elements
console.log(newNumbers); // Output: [1, 2, 3, 4, 5]
Use Cases:
1.2 Combining Arrays:
const arr1 = [1, 2];
const arr2 = [3, 4];
const combined = [...arr1, ...arr2];
console.log(combined); // Output: [1, 2, 3, 4]
# VSC Interface Overview
1.3 Copying Arrays (Immutable):
const original = [10, 20, 30];
const copy = [...original];
copy.push(40);
console.log(original); // Output: [10, 20, 30] (Unchanged)
console.log(copy); // Output: [10, 20, 30, 40]
Use Cases:
1.4 Expanding Objects:
const user = { name: "Alice", age: 30 };
const updatedUser = { ...user, location: "Paris" }; // Add a new property
console.log(updatedUser);
// Output: { name: "Alice", age: 30, location: "Paris" }
# VSC Interface Overview
1.5 Function Arguments:
const numbers = [5, 10, 15];
console.log(Math.max(...numbers)); // Output: 15
Use Cases:
# VSC Interface Overview
What It Does:
# VSC Interface Overview
2.1 Function Parameters:
function sum(...numbers) {
return numbers.reduce((total, num) => total + num, 0);
}
console.log(sum(1, 2, 3, 4)); // Output: 10
Use Cases:
2.2 Collecting Remaining Array Items:
const [first, second, ...rest] = [10, 20, 30, 40, 50];
console.log(first); // Output: 10
console.log(second); // Output: 20
console.log(rest); // Output: [30, 40, 50]
# VSC Interface Overview
2.3 Collecting Remaining Object Properties:
const user = { name: "Alice", age: 30, location: "Paris" };
const { name, ...details } = user;
console.log(name); // Output: "Alice"
console.log(details); // Output: { age: 30, location: "Paris" }
Use Cases:
# VSC Interface Overview
const numbers = [1, 2, 3, 4, 5];
const [first, ...rest] = numbers; // Rest collects remaining items
console.log(first); // Output: 1
console.log(rest); // Output: [2, 3, 4, 5]
const newNumbers = [...rest, 6, 7]; // Spread expands elements
console.log(newNumbers); // Output: [2, 3, 4, 5, 6, 7]
Combining Both Operators:
# VSC Interface Overview
const user = { name: "Alice", age: 30, location: "Paris" };
const { location, ...personalDetails } = user; // Rest collects properties
console.log(personalDetails); // Output: { name: "Alice", age: 30 }
const updatedUser = { ...personalDetails, location: "London" }; // Spread expands properties
console.log(updatedUser); // Output: { name: "Alice", age: 30, location: "London" }
Mixing Objects:
Pro Tip: Use Spread for Expansion and Rest for Gathering