JavaScript ES6 Basics
Modern JavaScript fundamentals: let/const, arrow functions, template literals, and destructuring.
Let & Const
# Block-scoped variables
let x = 10; # Can be reassigned
const y = 20; # Cannot be reassigned
# Const with objects
const obj = {name: "John"};
obj.name = "Jane"; # OK - modifying property
obj = {}; # ERROR - cannot reassign
Arrow Functions
# Traditional function
function add(a, b) {
return a + b;
}
# Arrow function
const add = (a, b) => a + b;
# With multiple statements
const greet = (name) => {
const msg = `Hello, ${name}!`;
return msg;
};
Template Literals
# Template strings
const name = "Alice";
const greeting = `Hello, ${name}!`;
# Multi-line strings
const html = `
Title
`;
# Expression evaluation
`Sum: ${2 + 2}` # "Sum: 4"
Destructuring
# Array destructuring
const [a, b, c] = [1, 2, 3];
const [first, ...rest] = [1, 2, 3, 4];
# Object destructuring
const {name, age} = {name: "Bob", age: 30};
const {name: userName} = user; # Rename
const {name = "Guest"} = user; # Default value