ES6 features in javascript:

Priti Jha
3 min readJul 25, 2024

--

Here are some essential ES6 features you can discuss in an interview:

1. Let and Const

  • let: Allows you to declare block-scoped variables, which are limited to the block, statement, or expression where they are used.
  • const: Allows you to declare variables that are block-scoped and cannot be reassigned.

2Arrow Function

  • Arrow functions provide a concise syntax for writing function expressions and lexically bind the this value.

Example:

const add = (a, b) => a + b;
console.log(add(2, 3)); // 5

3. Template Literals

  • Template literals allow for multi-line strings and expression interpolation.

Example:

const name = 'John';
const greeting = `Hello, ${name}!`;
console.log(greeting); // Hello, John!

4. Default Parameters

  • Default parameters allow you to set default values for function parameters.

Example:

function greet(name = 'Guest') {
return `Hello, ${name}`;
}
console.log(greet()); // Hello, Guest

5. Destructuring Assignment

  • Destructuring allows you to unpack values from arrays or properties from objects into distinct variables.

Example:

// Array destructuring
const [a, b] = [1, 2];
console.log(a); // 1
console.log(b); // 2
// Object destructuring
const { name, age } = { name: 'Alice', age: 25 };
console.log(name); // Alice
console.log(age); // 25

6. Rest and Spread Operators

  • The rest operator (...) allows you to represent an indefinite number of arguments as an array.
  • The spread operator (...) allows you to expand an array or object into individual elements.

Example:

// Rest operator
function sum(...numbers) {
return numbers.reduce((total, num) => total + num, 0);
}
console.log(sum(1, 2, 3)); // 6
// Spread operator
const arr1 = [1, 2, 3];
const arr2 = [...arr1, 4, 5, 6];
console.log(arr2); // [1, 2, 3, 4, 5, 6]

7. Classes

  • ES6 introduces classes, which are syntactical sugar over JavaScript’s existing prototype-based inheritance.

Example:

class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
  greet() {
return `Hello, my name is ${this.name} and I am ${this.age} years old.`;
}
}
const john = new Person('John', 30);
console.log(john.greet()); // Hello, my name is John and I am 30 years old.

8. Promises

  • Promises provide a way to handle asynchronous operations.

Example:

const fetchData = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('Data received');
}, 1000);
});
};
fetchData().then(data => console.log(data)); // Data received

9. Modules

  • ES6 introduces the module system, which allows you to export and import functions, objects, or primitives from one module to another.

Example: // module.js

export const add = (a, b) => a + b;

// main.js

import { add } from './module.js';
console.log(add(2, 3)); // 5

10. Enhanced Object Literals

  • ES6 enhances the object literal syntax, making it more concise and expressive.

Example:

const name = 'John';
const age = 30;
const person = {
name,
age,
greet() {
return `Hello, my name is ${this.name}`;
}
};
console.log(person.greet()); // Hello, my name is John

Conclusion

Highlighting these features during an interview demonstrates your knowledge of modern JavaScript and your ability to write clean, efficient, and maintainable code.

--

--

Priti Jha

Senior front-end developer writing about Angular 8 , Ionic Framework ,Javascript, HTML, Css and all front end technology.