JavaScript Interview Questions for Freshers: Top 20 Questions with Answers

JavaScript Interview Questions for Freshers: Top 20 Questions with Answers

JavaScript is one of the most popular programming languages for web development. If you're preparing for a JavaScript interview as a fresher, it's essential to know the basics as well as some common coding concepts. Here’s a list of the top 20 JavaScript interview questions along with their answers.

1. What is JavaScript?

JavaScript is a high-level, interpreted programming language used to make web pages interactive. It is an essential part of web development alongside HTML and CSS.

2. What are the different data types in JavaScript?

JavaScript has the following primitive data types:

  • String

  • Number

  • Boolean

  • Undefined

  • Null

  • Symbol

  • BigInt

3. What is the difference between var, let, and const?

  • var has function scope and can be redeclared.

  • let has block scope and cannot be redeclared within the same scope.

  • const is also block-scoped but cannot be reassigned after initialization.

4. Explain the difference between == and ===.

  • == checks for equality but allows type conversion.

  • === checks for both value and type equality (strict equality).

5. What are JavaScript functions? How do you declare them?

A function is a reusable block of code that performs a specific task.

function greet(name) {
  return `Hello, ${name}!`;
}
console.log(greet("John"));

6. What is an arrow function in JavaScript?

Arrow functions provide a shorter syntax for writing functions.

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

7. What is a callback function?

A callback function is a function passed as an argument to another function and executed later.

function fetchData(callback) {
  setTimeout(() => {
    console.log("Data fetched");
    callback();
  }, 1000);
}
fetchData(() => console.log("Callback executed"));

8. What is an Immediately Invoked Function Expression (IIFE)?

An IIFE is a function that executes immediately after its definition.

(function() {
  console.log("IIFE executed");
})();

9. What is the difference between null and undefined?

  • null is an assigned value representing no value.

  • undefined means a variable has been declared but not assigned a value.

10. What is the this keyword in JavaScript?

this refers to the object that is executing the function. Its value depends on how the function is called.

11. What are template literals in JavaScript?

Template literals allow embedding expressions inside strings using backticks:

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

12. What are promises in JavaScript?

Promises are used to handle asynchronous operations.

let myPromise = new Promise((resolve, reject) => {
  setTimeout(() => resolve("Promise resolved!"), 2000);
});
myPromise.then(value => console.log(value));

13. What is the difference between synchronous and asynchronous code?

  • Synchronous code executes line by line.

  • Asynchronous code allows tasks to run in the background without blocking execution.

14. What is the map() function in JavaScript?

The map() function creates a new array by applying a function to each element of an existing array.

const numbers = [1, 2, 3];
const squared = numbers.map(num => num * num);
console.log(squared);

15. What is event delegation?

Event delegation allows handling events at a parent level instead of attaching listeners to individual elements.

16. What is the typeof operator?

The typeof operator returns the data type of a variable.

console.log(typeof 10); // "number"
console.log(typeof "hello"); // "string"

17. What are higher-order functions in JavaScript?

A higher-order function takes another function as an argument or returns a function.

function operate(operation, a, b) {
  return operation(a, b);
}
const multiply = (x, y) => x * y;
console.log(operate(multiply, 2, 3));

18. What is destructuring in JavaScript?

Destructuring allows extracting values from arrays or objects easily.

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

19. What is a closure in JavaScript?

A closure is a function that remembers variables from its outer scope even after the outer function has executed.

function outerFunction(x) {
  return function innerFunction(y) {
    return x + y;
  };
}
const addFive = outerFunction(5);
console.log(addFive(10));

20. What are modules in JavaScript?

Modules allow breaking JavaScript code into reusable files using export and import.

// module.js
export function greet() {
  return "Hello!";
}
// main.js
import { greet } from "./module.js";
console.log(greet());

Conclusion

Preparing for JavaScript interviews as a fresher can be overwhelming, but understanding these fundamental questions will help you gain confidence. Make sure to practice coding and build small projects to solidify your knowledge. Good luck with your JavaScript interview!