You are on page 1of 18

"Top 25 JavaScript Interview Questions and Answers for 5 Years of Experience (ES6 and Above) | Medium 6/23/23, 6:39 PM

Search Medium Write Sign up Sign In

Member-only story

20 Javascript interview questions


with code answers.
FullStackTips · Follow
6 min read · Jan 31

-- 4

https://12ft.io/api/proxy?ref=&q=https%3A%2F%2Fmedium.com%2F%40fu…20-javascript-interview-questions-with-code-answers-dd9fb28f3f5a Page 1 of 18
"Top 25 JavaScript Interview Questions and Answers for 5 Years of Experience (ES6 and Above) | Medium 6/23/23, 6:39 PM

Photo by JESHOOTS.COM on Unsplash

I am going to start a series for the JavaScript interview questions which may
help junior to mid level developers.

Below are 20 common JavaScript ES6+ interview questions with examples.

1. What is let and const in JavaScript?

let and const are block-scoped declarations in JavaScript, used to declare


variables. let allows you to reassign the value of the variable, while const

creates a read-only reference to a value.

https://12ft.io/api/proxy?ref=&q=https%3A%2F%2Fmedium.com%2F%40f…20-javascript-interview-questions-with-code-answers-dd9fb28f3f5a Page 2 of 18
"Top 25 JavaScript Interview Questions and Answers for 5 Years of Experience (ES6 and Above) | Medium 6/23/23, 6:39 PM

Example:

let message = "Hello, World!";


message = "Hello, JavaScript!";
console.log(message); // Output: "Hello, JavaScript!"
const PI = 3.14;
PI = 3.14159; // TypeError: Assignment to constant variable.

2. What is arrow function in JavaScript?

Arrow functions are a shorthand for anonymous functions in JavaScript.


They are also known as “fat arrow” functions.

Example:

let add = (a, b) => a + b;


console.log(add(1, 2)); // Output: 3
let numbers = [1, 2, 3, 4, 5];
let doubledNumbers = numbers.map(num => num * 2);
console.log(doubledNumbers); // Output: [2, 4, 6, 8, 10]

3. What is default parameter in JavaScript?

Default parameters allow function parameters to be initialized with default


values if no values are passed or if the values are undefined.

https://12ft.io/api/proxy?ref=&q=https%3A%2F%2Fmedium.com%2F%40f…20-javascript-interview-questions-with-code-answers-dd9fb28f3f5a Page 3 of 18
"Top 25 JavaScript Interview Questions and Answers for 5 Years of Experience (ES6 and Above) | Medium 6/23/23, 6:39 PM

Example:

function greet(name = "John") {


return `Hello, ${name}`;
}
console.log(greet()); // Output: "Hello, John"
console.log(greet("Jane")); // Output: "Hello, Jane"

4. What is spread operator in JavaScript?

The spread operator allows an iterable to be expanded into its elements.

Example:

let numbers1 = [1, 2, 3];


let numbers2 = [4, 5, 6];
let numbers3 = [...numbers1, ...numbers2];
console.log(numbers3); // Output: [1, 2, 3, 4, 5, 6]
let person = { name: "John", age: 30 };
let newPerson = { ...person, city: "New York" };
console.log(newPerson); // Output: { name: "John", age: 30, city: "New York" }

5. What is destructuring in JavaScript?

Destructuring is a convenient way to extract values from arrays or objects


into variables.

https://12ft.io/api/proxy?ref=&q=https%3A%2F%2Fmedium.com%2F%40f…20-javascript-interview-questions-with-code-answers-dd9fb28f3f5a Page 4 of 18
"Top 25 JavaScript Interview Questions and Answers for 5 Years of Experience (ES6 and Above) | Medium 6/23/23, 6:39 PM

Example:

let numbers = [1, 2, 3];


let [a, b, c] = numbers;
console.log(a, b, c); // Output: 1 2 3
let person = { name: "John", age: 30 };
let { name, age } = person;
console.log(name, age); // Output: John 30

6. What is a rest parameter in JavaScript?

A rest parameter is a way to represent an indefinite number of arguments


as an array. It allows a function to take a variable number of arguments.
The rest parameter is represented by three dots (…).

Example:

function sum(...args) {
return args.reduce((a, b) => a + b);
}
console.log(sum(1, 2, 3, 4, 5));
// Output: 15

7. What is the difference between var, let, and const in JavaScript?

var is function scoped and its value can be changed, while let and const

https://12ft.io/api/proxy?ref=&q=https%3A%2F%2Fmedium.com%2F%40f…20-javascript-interview-questions-with-code-answers-dd9fb28f3f5a Page 5 of 18
"Top 25 JavaScript Interview Questions and Answers for 5 Years of Experience (ES6 and Above) | Medium 6/23/23, 6:39 PM

are block scoped and their values cannot be changed once they are assigned
with const .

Example:

var name = "John";


var name = "Jane";
console.log(name); // Output: "Jane"
let age = 30;
// age = 31;
console.log(age); // Output: 30
const city = "New York";
// city = "London";
console.log(city); // Output: "New York"

8. What is closure in JavaScript?

A closure is a function that has access to variables in its outer scope, even
after the outer function has returned.

Example:

function outerFunction(counter) {
return function innerFunction() {
return ++counter;
};
}

let counter = 0;

https://12ft.io/api/proxy?ref=&q=https%3A%2F%2Fmedium.com%2F%40f…20-javascript-interview-questions-with-code-answers-dd9fb28f3f5a Page 6 of 18
"Top 25 JavaScript Interview Questions and Answers for 5 Years of Experience (ES6 and Above) | Medium 6/23/23, 6:39 PM

let increment = outerFunction(counter);


console.log(increment()); // Output: 1
console.log(increment()); // Output: 2

9. What is a promise in JavaScript?

A promise is an object representing the eventual completion or failure of an


asynchronous operation.

Example:

let promise = new Promise((resolve, reject) => {


setTimeout(() => {
resolve("Hello, World!");
}, 2000);
});

promise.then(result => {
console.log(result); // Output: "Hello, World!"
});

10. What is async/await in JavaScript?

async/await is a way to write asynchronous code that looks and behaves like
synchronous code.

Example:
https://12ft.io/api/proxy?ref=&q=https%3A%2F%2Fmedium.com%2F%40fu…20-javascript-interview-questions-with-code-answers-dd9fb28f3f5a Page 7 of 18
"Top 25 JavaScript Interview Questions and Answers for 5 Years of Experience (ES6 and Above) | Medium 6/23/23, 6:39 PM

async function getData() {


let response = await fetch("https://jsonplaceholder.typicode.com/posts");
let data = await response.json();
return data;
}
getData().then(data => {
console.log(data);
});

11. What is a template literal in JavaScript?

A template literal is a string that is delimited by backticks (`) instead of


single or double quotes.

Example:

let name = "John";


let message = `Hello, ${name}`;
console.log(message); // Output: "Hello, John"

12. What is a class in JavaScript?

A class is a blueprint for creating objects that defines a set of properties and
methods.

Example:

https://12ft.io/api/proxy?ref=&q=https%3A%2F%2Fmedium.com%2F%40f…20-javascript-interview-questions-with-code-answers-dd9fb28f3f5a Page 8 of 18
"Top 25 JavaScript Interview Questions and Answers for 5 Years of Experience (ES6 and Above) | Medium 6/23/23, 6:39 PM

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.`;
}
}
let person = new Person("John", 30);
console.log(person.greet()); // Output: "Hello, my name is John and I am 30 years old

13. What is a constructor in JavaScript?

A constructor is a special method that is called when an object is created


from a class. It is used to initialize the object’s properties.

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.`;
}
}
let person = new Person("John", 30);
console.log(person.greet()); // Output: "Hello, my name is John and I am 30 years old

https://12ft.io/api/proxy?ref=&q=https%3A%2F%2Fmedium.com%2F%40f…20-javascript-interview-questions-with-code-answers-dd9fb28f3f5a Page 9 of 18
"Top 25 JavaScript Interview Questions and Answers for 5 Years of Experience (ES6 and Above) | Medium 6/23/23, 6:39 PM

14. What is the difference between == and === in JavaScript?

== performs type coercion, meaning it converts the operands to the same


type before making the comparison, while === does not perform type
coercion and returns false if the operands have different types.

Example:

console.log(1 == "1"); // Output: true


console.log(1 === "1"); // Output: false

15. What is the difference between null and undefined in JavaScript?

undefined means a variable has been declared but has not been assigned a
value, while null is explicitly set to represent no value.

Example:

let name;
console.log(name === undefined); // Output: true
let city = null;
console.log(city === null); // Output: true

16. What is the difference between let and const when declaring a constant?

https://12ft.io/api/proxy?ref=&q=https%3A%2F%2Fmedium.com%2F%40f…20-javascript-interview-questions-with-code-answers-dd9fb28f3f5a Page 10 of 18
"Top 25 JavaScript Interview Questions and Answers for 5 Years of Experience (ES6 and Above) | Medium 6/23/23, 6:39 PM

Both let and const can be used to declare a constant, but the value of a
const cannot be changed after it is declared, while the value of a let can be
changed.

Example:

const PI = 3.14;
// PI = 3.15;
console.log(PI); // Output: 3.14
let name = "John";
name = "Jane";
console.log(name); // Output: "Jane"

17. What is a spread operator in JavaScript?

The spread operator ( ... ) allows an expression to be expanded in places


where multiple elements or variables are expected.

Example:

let numbers = [1, 2, 3];


let newNumbers = [...numbers, 4, 5];
console.log(newNumbers); // Output: [1, 2, 3, 4, 5]
let person = { name: "John", age: 30 };
let newPerson = { ...person, city: "New York" };
console.log(newPerson); // Output: { name: "John", age: 30, city: "New York" }

https://12ft.io/api/proxy?ref=&q=https%3A%2F%2Fmedium.com%2F%40f…20-javascript-interview-questions-with-code-answers-dd9fb28f3f5a Page 11 of 18
"Top 25 JavaScript Interview Questions and Answers for 5 Years of Experience (ES6 and Above) | Medium 6/23/23, 6:39 PM

18. What is a rest parameter in JavaScript?

A rest parameter is used to represent an indefinite number of arguments as


an array. It is prefixed with ... in the function definition.

Example:

function sum(...numbers) {
return numbers.reduce((total, num) => total + num, 0);
}
console.log(sum(1, 2, 3)); // Output: 6

19. What is a destructuring assignment in JavaScript?

Destructuring assignment is a way to extract data from arrays or objects


into separate variables.

let person = { name: "John", age: 30, city: "New York" };


let { name, age } = person;
console.log(name); // Output: "John"
console.log(age); // Output: 30

let numbers = [1, 2, 3];


let [a, b] = numbers;
console.log(a); // Output: 1
console.log(b); // Output: 2

https://12ft.io/api/proxy?ref=&q=https%3A%2F%2Fmedium.com%2F%40f…20-javascript-interview-questions-with-code-answers-dd9fb28f3f5a Page 12 of 18
"Top 25 JavaScript Interview Questions and Answers for 5 Years of Experience (ES6 and Above) | Medium 6/23/23, 6:39 PM

20. What is a higher-order function in JavaScript?

A higher-order function is a function that takes one or more functions as


arguments or returns a function as a result.

Example:

function multiplyBy(factor) {
return function (number) {
return number * factor;
};
}
let double = multiplyBy(2);
console.log(double(5)); // Output: 10

This series continued in the next blog here.

Thanks for reading!

If you’re interested in joining Medium member program, please use my


personal referral link to become a member. You’ll get unlimited access to
great content and supportive community and a platform that values quality
content!

https://12ft.io/api/proxy?ref=&q=https%3A%2F%2Fmedium.com%2F%40f…20-javascript-interview-questions-with-code-answers-dd9fb28f3f5a Page 13 of 18
"Top 25 JavaScript Interview Questions and Answers for 5 Years of Experience (ES6 and Above) | Medium 6/23/23, 6:39 PM

JavaScript Javascript Interview Javascript Tips Javascript Questions

Learn Javascript

Written by FullStackTips Follow

444 Followers

I am full stack developer with over 15 years of experience in various programming


languages such as Java, Java Script, Node Js, Spring Framework modules

More from FullStackTips

https://12ft.io/api/proxy?ref=&q=https%3A%2F%2Fmedium.com%2F%40f…20-javascript-interview-questions-with-code-answers-dd9fb28f3f5a Page 14 of 18
"Top 25 JavaScript Interview Questions and Answers for 5 Years of Experience (ES6 and Above) | Medium 6/23/23, 6:39 PM

FullStackTips FullStackTips

10 Commonly Asked JavaScript Spring Boot | Track requests using


Interview Questions With Answers tracking Id or request Id |Java
Photo by Nangialai Stoman on Unsplash Photo by Caspar Camille Rubin on Unsplash

· 5 min read · Mar 20 · 3 min read · Jan 14

-- 2 --

FullStackTips FullStackTips

Every new Java developer should Tricky Java Interview Coding


know these f! Questions with Answers—Part 4
As you might already know, Java shifted the 1. How to find the duplicate characters in a
gear with the release of version 8 few years… string?
ago. These features are still new to some…
· 4 min read · Dec 30, 2022 · 10 min read · May 8

-- --

See all from FullStackTips

https://12ft.io/api/proxy?ref=&q=https%3A%2F%2Fmedium.com%2F%40f…20-javascript-interview-questions-with-code-answers-dd9fb28f3f5a Page 15 of 18
"Top 25 JavaScript Interview Questions and Answers for 5 Years of Experience (ES6 and Above) | Medium 6/23/23, 6:39 PM

Recommended from Medium

React Dojo Maria Laramie in Frontend Weekly

Interview Questions CheatSheet 5 Advanced JavaScript Tricks for


for Junior React Developers Experienced Developers
Hi there, I’m Saya, In this blog post, I’ll In this article, I will uncover 5 advanced tricks
provide you with a cheat sheet that covers… that will help you write more efficient and…
some of the essential interview questions elegant code. These tricks are specially…
that· a…
3 min read · Mar 2 · 3 min read · Jan 25

-- 2 -- 13

Lists

https://12ft.io/api/proxy?ref=&q=https%3A%2F%2Fmedium.com%2F%40f…20-javascript-interview-questions-with-code-answers-dd9fb28f3f5a Page 16 of 18
"Top 25 JavaScript Interview Questions and Answers for 5 Years of Experience (ES6 and Above) | Medium 6/23/23, 6:39 PM

Stories to Help You Grow as a General Coding Knowledge


Software Developer 20 stories · 17 saves
19 stories · 141 saves

Modern Marketing Medium Publications


32 stories · 2 saves Accepting Story Submissions
145 stories · 163 saves

Abhinav Kumar shrey vijayvargiya in Frontend Weekly

Zalando Interview Experience 50 Node JS Interview Questions


My Zalando Interview experience for Senior Save the story for the Future and crack any
Frontend Engineer Node JS backend interview

· 5 min read · Feb 20 · 3 min read · Feb 13

-- -- 2

https://12ft.io/api/proxy?ref=&q=https%3A%2F%2Fmedium.com%2F%40f…20-javascript-interview-questions-with-code-answers-dd9fb28f3f5a Page 17 of 18
"Top 25 JavaScript Interview Questions and Answers for 5 Years of Experience (ES6 and Above) | Medium 6/23/23, 6:39 PM

FullStackTips Reed Barger in Web Dev Hero

Java Memory Management: 7 React Projects for Beginners in


Understanding the JVM, Heap,… 2023 (+ Code)
Method Area,
If you are new Stack
to Java, I would recommend You’re ready to start making simple projects
you to go through the 5 min read Getting… with React, but you don’t know what to mak…
started with java first, that will give an idea of Where should you start?
· 4 min read · Jan 25
what… · 6 min read · Jan 11

-- -- 7

See more recommendations

Help Status Writers Blog Careers Privacy Terms About Text to speech Teams

https://12ft.io/api/proxy?ref=&q=https%3A%2F%2Fmedium.com%2F%40f…20-javascript-interview-questions-with-code-answers-dd9fb28f3f5a Page 18 of 18

You might also like