JavaScript Interview Questions
JavaScript
Web DevelopmentFrontendBackendQuestion 9
What are some new features in ES6?
Answer:
ES6, also known as ECMAScript 2015, introduced several new features to JavaScript that enhanced its capabilities and improved developer productivity. Some of the notable features include:
- Arrow Functions: Provide a concise syntax for writing functions.
- Classes: Offer a more straightforward and clearer syntax to create objects and deal with inheritance.
- Template Literals: Allow embedding expressions in strings and support multi-line strings.
- Default Parameters: Enable setting default values for function parameters.
- Destructuring Assignment: Allows unpacking values from arrays or properties from objects into distinct variables.
- Spread Operator: Expands elements of an array or object.
- let and const Declarations: Provide block-scoped variables.
Here are some examples of these features:
// Arrow Function
const add = (a, b) => a + b;
console.log(add(2, 3)); // 5
// Class
class Person {
constructor(name) {
this.name = name;
}
greet() {
console.log(`Hello, ${this.name}`);
}
}
const john = new Person('John');
john.greet(); // Hello, John
// Template Literal
const greeting = `Hello,
World!`;
console.log(greeting);
// Default Parameter
function multiply(a, b = 2) {
return a * b;
}
console.log(multiply(3)); // 6
// Destructuring Assignment
const [x, y] = [1, 2];
console.log(x, y); // 1 2
// Spread Operator
const arr1 = [1, 2];
const arr2 = [...arr1, 3, 4];
console.log(arr2); // [1, 2, 3, 4]
// let and const
let a = 10;
const b = 20;
console.log(a, b); // 10 20