Introduction:
Introduce ES6 as a significant update to JavaScript, bringing a plethora of new features and syntactic sugar to enhance code readability and maintainability.
1. let and const Declarations:
Explain the difference between let
, and const
, and how let
and const
provide block scoping. Include examples illustrating their usage.
- let:
- Variables declared with
let
can be reassigned values. - They are block-scoped, meaning they exist only within the block they are defined in, such as loops or conditional statements.
- Variables declared with
let
are hoisted to the top of their block, but they are not initialized until the line of code where they are declared is executed. - Example:
let x = 10;
if (true) {
let y = 20;
console.log(x); // Output: 10
console.log(y); // Output: 20
}
console.log(x); // Output: 10
console.log(y); // ReferenceError: y is not defined
2. const
- Variables declared with
const
are constants and cannot be reassigned a new value once initialized. - Like
let
,const
is also block-scoped. - Constants must be initialized when declared.
- If a
const
variable holds an object or array, the properties or elements of that object or array can be modified, but the variable itself cannot be reassigned. - Example:
const PI = 3.14;
PI = 3; // Error: Assignment to constant variable
const person = {
name: 'John',
age: 30
};
person.age = 31; // Valid
person = { name: 'Jane', age: 25 }; // Error: Assignment to constant variable
In general, it’s recommended to use const
by default and only use let
when you need to reassign a variable. This helps in writing safer and more predictable code.
2. Arrow Functions:
Discuss the concise syntax of arrow functions and their lexical scoping behavior. Provide examples comparing traditional functions with arrow functions.
// Traditional function
function add(a, b) {
return a + b;
}
// Arrow function
const addArrow = (a, b) => a + b;
console.log(add(5, 3)); // Output: 8
console.log(addArrow(5, 3)); // Output: 8
3. Template Literals:
Highlight the usefulness of template literals in creating strings with embedded expressions. Show examples of template literals in action.
const name = 'John';
const greeting = `Hello, ${name}!`;
console.log(greeting); // Output: Hello, John!
4. Enhanced Object Literals:
Explain shorthand property syntax, computed property names, and method definitions within object literals. Provide examples demonstrating each feature.
const x = 10, y = 20;
// Shorthand property syntax
const obj = { x, y };
// Computed property names
const propName = 'z';
const objWithComputedProp = { [propName]: 30 };
console.log(obj); // Output: { x: 10, y: 20 }
console.log(objWithComputedProp); // Output: { z: 30 }
5. Destructuring Assignment:
Illustrate how destructuring assignment simplifies extracting values from arrays and objects. Show examples of array and object destructuring.
const numbers = [1, 2, 3, 4, 5];
// Array destructuring
const [first, second, ...rest] = numbers;
console.log(first); // Output: 1
console.log(second); // Output: 2
console.log(rest); // Output: [3, 4, 5]
6. Spread and Rest Operators:
Discuss the versatility of spread and rest operators in manipulating arrays and function arguments. Include examples showcasing their usage.