Create two variables using let and const. The let variable should be named age and set to 30. The const variable should be named name and set to "Alice". Try to reassign the name variable and observe what happens.
ES6 introduced let and const for declaring variables. let is used for variables that can change, while const is for variables that should remain constant.
let age = 30; const name = "Alice"; name = "Bob";
Convert the following function into an arrow function.
function add(a, b) {
return a + b; }
Arrow functions provide a concise syntax and lexically bind the this value.
const add = (a, b) => a + b;
Use a template literal to print "Hello, Alice! Your age is 30." using the variables name and age.
Template literals allow embedded expressions and multi-line strings.
console.log(Hello, ${name}! Your age is ${age}.
);
Given an object { firstName: "Alice", lastName: "Johnson" }, use object destructuring to extract firstName and lastName into variables.
Destructuring allows you to unpack values from arrays or properties from objects.
const person = { firstName: "Alice", lastName: "Johnson" };
const { firstName, lastName } = person;
Given an array [1, 2, 3, 4, 5], use array destructuring to create variables first and second for the first two elements.
Array destructuring works similarly to object destructuring but with array elements.
const numbers = [1, 2, 3, 4, 5];
const [first, second] = numbers;
Combine two arrays [1, 2, 3] and [4, 5, 6] into a new array using the spread operator.
The spread operator allows an iterable (like an array) to be expanded in places where zero or more arguments or elements are expected.
const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const combined = [...arr1, ...arr2];
Write a function that takes multiple arguments and returns their sum using rest parameters.
Rest parameters allow us to represent an indefinite number of arguments as an array.
const sum = (...numbers) => numbers.reduce((acc, current) => acc + current, 0);
Create a function greet(name, greeting = "Hello") that greets a person. If no greeting is provided, it should default to "Hello".
Default function parameters allow named parameters to be initialized with default values if no value or undefined is passed.
const greet = (name, greeting = "Hello") => ${greeting}, ${name}!;
Create a class Animal with a constructor setting the name property. Then, create a subclass Dog that extends Animal and adds a bark method.
ES6 classes are syntactical sugar over JavaScript's existing prototype-based inheritance. The extends keyword is used for class inheritance.
class Animal {
constructor(name) {
this.name = name;
}
}
class Dog extends Animal {
bark() {
return Woof! My name is ${this.name};
}
}