Top 18 JavaScript Interview Questions

Here you can see the top 18 JavaScript Interview Questions and answers. I’d like you to please read each JavaScript interview question and prepare yourself for the interview. most of the JavaScript interviews are based on this type of question.

1) What is JavaScript ?

Answer: JavaScript is a high-level, interpreted scripting language that is used to create interactive and dynamic web pages in applications.

2) What are the data types in JavaScript?

Answer: There are two types of datatypes available in JavaScript:

  1. Primitive Data Types
    • This Data Type includes a Number, String, Boolean, Undefined, Null and Symbol.
  2. Non-Primitive Data Types
    • Non-Primitive includes an Object, Array, Function, Date, Map and Set.

3) What is hoisting in JavaScript?

Answer: Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope before code execution. But only the declarations are hoisted without its values.

Example of variable hoisting:

console.log(a); // Output: undefined
var a = 5;

The output is undefined because JavaScript read the code like this:

var a;
console.log(a); // undefined
a = 5;

Only the var a is hoisted to the top. The assignment (a = 5) stays in place.

Example for Function Hoisting:

sayHello(); // Output: Hello!

function sayHello() {
console.log("Hello!");
}

Why?

The entire function sayHello is hoisted to the top. So you can call it even before the definition.

We can not used with let and const

console.log(b); // ReferenceError
let b = 10;

Let and const are not initialized during hoisting.

4) What is the difference between null and undefined?

Answer :

  • null is an intentional assignment value that represents “no value” or “nothing”. It means the developer has set the variable to be empty.
  • undefined means a variable has been declared but not yet assigned any value. It is set by JavaScript automatically.

For example:

let a;
console.log(a); // undefined (no value assigned)

let b = null;
console.log(b); // null (manually assigned as empty)

5) What is a callback function?

Answer: A callback function is a function that is passed into another function as an argument, which is invoked inside the outer function to complete some action.

For example:

function greet(name, callback) {
console.log("Hello, " + name);
callback(); // This is the callback function
}

function sayBye() {
console.log("Goodbye!");
}

greet("John", sayBye);

Output:

Hello, John
Goodbye!
  • sayBye is the callback function.
  • It is passed into the greet function.
  • Then we call the callback() function inside the greet function.

6) What is the purpose of the setTimeout() function?

Answer: The setTimeout() function is used to run a function in a certain amount of time (delay).

It does not run the function immediately, but waits for the delay (in milliseconds) and then runs it once.

Syntax:

setTimeout(function, delay_in_ms);
  • function – the code you want to run.
  • delay_in_ms – the time to wait in milliseconds (1000 ms = 1 second).

Example of setTimeout() function:

setTimeout(function() {
console.log("This runs after 2 seconds");
}, 2000);

Output (after 2 seconds):

This runs after 2 seconds

7) How can you check if an array includes a certain value or not?

Answer: We can use the includes() method to check a certain value of an array.

Syntax:

array.includes(value)
  • If the value is found in the array, it returns true; otherwise returns false.

For example:

let fruits = ["apple", "banana", "mango"];

console.log(fruits.includes("banana")); // true
console.log(fruits.includes("grapes")); // false
  • “banana” is the part of an array → so it returns true.
  • “grapes” is not → so it returns false.

8) What is the purpose of async and await in JavaScript?

Answer: Async and await are JavaScript keywords that help you write code that waits for something to finish (like API data, timer, file load).

async means – this function will do something slowly (like loading data).
await means – wait here until that slow task is done, then go to the next step.

For example:

async function showMessage() {
console.log("Step 1");
await new Promise(resolve => setTimeout(resolve, 2000)); // wait for 2 seconds
console.log("Step 2");
}

showMessage();

Output:

Step 1
(wait 2 seconds...)
Step 2

9) Explain the difference between let, var, and const.

Answer: let, var, and const are variable scopes in JavaScript.

  • var is function-scoped, it can change its values after being assigned.
  • let and const are block-scoped, let is the modern way for variables and that can change its values.
  • const variables cannot be changed (reassigned) after they are declared.

10) What is the DOM in JavaScript?

Answer: The DOM stands for Document Object Model. It represents the tree structure of HTML elements with JavaScript.

11) What is the difference between == and ===?

Answer:

  • == compares only values.
  • === compares both value and data type.

For example:

5 == "5"    // true  → value is same, types are different but JS converts them
5 === "5" // false → value is same, but type is number vs string
null == undefined   // true
null === undefined // false

12) What is the difference between synchronous and asynchronous code?

Answer: Synchronous code runs one line at a time. It waits for each task to finish before going to the next one.

Asynchronous code does not wait. If a task takes time (like loading data), it runs in the background, and the next lines run right away.

Example of Synchronous:

console.log("1");
console.log("2");
console.log("3");

Output:

1  
2
3

Everything runs step-by-step.

Example of Asynchronous:

console.log("1");
setTimeout(() => {
console.log("2");
}, 2000);
console.log("3");

Output:

1  
3
2 ← (after 2 seconds)

Line 2 (setTimeout) runs in the background, and the rest of the code continues to run.

13) What is the fetch() API?

Answer: The fetch() API is a modern and powerful function in JavaScript that is used to get data from a server or send data to a server over the internet.

It is used to make network requests, like calling an API, and it returns a promise with the response.

For example:

fetch('https://api.example.com/data')
.then(response => response.json())
.then(data => console.log(data));

14) What is the reduce() method in JavaScript?

Answer: The reduce() method is useful in JavaScript for taking each value from the array, one by one, and combining them to make one final result.

Example: Sum of numbers

const numbers = [1, 2, 3, 4];

const total = numbers.reduce((acc, curr) => acc + curr, 0);

console.log(total); // Output: 10
  • acc = accumulator (to total the sum)
  • curr = It means current number in the loop
  • 0 = starting value

15) What is the typeof operator?

Answer: The typeof operator in JavaScript is used to check which type of value a variable has. For example: Number, String, Boolean, Object, etc.

Simple Example typeof operator:

let name = "John";
console.log(typeof name); // "string"

let age = 25;
console.log(typeof age); // "number"

let isHappy = true;
console.log(typeof isHappy); // "boolean"

16) What is the new keyword in JavaScript?

Answer: The new keyword in JavaScript is used to create a new object using a constructor function.

Foe example:

function Person(name) {
this.name = name;
}

let user1 = new Person("Alice");
console.log(user1.name); // Output: Alice

17) What is the difference between slice() and splice() methods?

Answer:

slice() is used to copy part of an array into a new array. It does not change the original array.

splice() is used to add, remove, or replace items in the array. It can change the original array.

Example of slice():

let fruits = ["apple", "banana", "mango", "orange"];
let sliced = fruits.slice(1, 3);
console.log(sliced); // ["banana", "mango"]
console.log(fruits); // ["apple", "banana", "mango", "orange"]

Code example of splice():

let fruits = ["apple", "banana", "mango", "orange"];
fruits.splice(1, 2); // removes 2 items starting from index 1
console.log(fruits); // ["apple", "orange"]

The original array is changed, and banana and mango are removed.

18) What is the Promise.all() method?

Answer: Promise.all() is a tool in JavaScript that helps you run many tasks at the same time, and wait for all of them to finish.

  • It takes a list of promises and gives you one result only when all the promises are done.
  • If even one promise fails, the whole thing will show an error.

For example:

const p1 = Promise.resolve(1);
const p2 = Promise.resolve(2);
const p3 = Promise.resolve(3);

Promise.all([p1, p2, p3]).then(values => {
console.log(values); // [1, 2, 3]
});

All three promises resolve, so you get the results in an array.

Above You Learn Top Most IMP JavaScript Interview Questions.

Top 10 Java Coding Interview Questions

HTML CSS JavaScript Interview Questions

What Is Graphic Design ?

Top 10 Artificial Intelligence (AI) Interview Questions and Answers

Leave a Comment

BoxofLearn