Skip to main content
1:1 mock interviews with Sumit, scored on a real hiring rubric.Book a slot →

JavaScript Interview Questions for Freshers (With Answers)

The JavaScript interview questions freshers get asked most, with clear answers and code: var vs let vs const, hoisting, closures, this, == vs ===, promises, async/await, the event loop and more.

jiocoder TeamUpdated Oct 7, 20267 min readJavaScript
JavaScript Interview Questions for Freshers (With Answers)

JavaScript is the heart of every frontend interview. Freshers are expected to explain the basics clearly and predict the output of short snippets. Here are the 25 questions asked most, with answers you can say out loud and code you should try yourself.

How to use this list

Run every snippet in your browser console before reading the answer. Predicting output is a skill you build by practice. For the full theory, read our JavaScript complete guide.

Basics#

1. What are the data types in JavaScript?#

Seven primitives: string, number, bigint, boolean, undefined, null and symbol. Everything else is an object, including arrays and functions. Primitives are copied by value; objects are copied by reference.

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

var vs let vs const comparison table: scope, redeclare, reassign, hoisting and temporal dead zone

var is function-scoped, can be redeclared and is hoisted as undefined. let and const are block-scoped and cannot be redeclared in the same scope. const cannot be reassigned, but an object stored in a const can still be changed.

3. What is hoisting?#

Before code runs, JavaScript sets up declarations for their scope. var variables start as undefined, and function declarations are fully available. let and const are hoisted but stay in the temporal dead zone until their line runs.

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

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

sayHi();        // works
function sayHi() { console.log("hi"); }

4. What is the difference between == and ===?#

=== checks value and type with no conversion. == converts types first, so "5" == 5 is true but "5" === 5 is false. Always use ===.

5. What is the difference between null and undefined?#

undefined means a value has not been assigned yet; JavaScript sets it automatically. null is an intentional "no value" that you set yourself. Note that typeof null is "object", a long-standing bug in the language.

6. What are truthy and falsy values?#

Falsy values become false in a condition: false, 0, -0, 0n, "", null, undefined and NaN. Everything else is truthy, including [], {} and "0".

Functions and scope#

7. What is the difference between a function declaration and an arrow function?#

  • Arrow functions have no own this; they use this from the surrounding scope.
  • They have no arguments object and cannot be used with new.
  • Function declarations are hoisted; arrow functions stored in const are not usable before their line.

8. What is a closure?#

JavaScript closure example: makeCounter returns an inner function that remembers the count variable

A closure is a function that remembers the variables from the scope where it was created, even after the outer function has finished.

JavaScript
function makeCounter() {
  let count = 0;
  return () => ++count;
}
const next = makeCounter();
next(); // 1
next(); // 2

Uses: private variables, function factories, debounce, memoization and event handlers.

9. What does this code print, and why?#

JavaScript
for (var i = 0; i < 3; i++) {
  setTimeout(() => console.log(i), 0);
}

It prints 3, 3, 3. var creates one shared i, and by the time the callbacks run, the loop has finished. Using let creates a new i for each iteration, so it prints 0, 1, 2.

10. How does the this keyword work?#

this depends on how a function is called. In obj.method() it is obj; with new it is the new object; with call, apply or bind it is whatever you pass; in a plain function call in strict mode it is undefined. Arrow functions take this from their surrounding scope.

11. What is the difference between call, apply and bind?#

All three set this. call invokes the function immediately with arguments listed one by one. apply invokes it immediately with arguments as an array. bind returns a new function with this fixed, to call later.

JavaScript
greet.call(user, "Hi", "!");
greet.apply(user, ["Hi", "!"]);
const bound = greet.bind(user);
bound("Hi", "!");

12. What is a higher-order function?#

A function that takes another function as an argument or returns one. map, filter, reduce and setTimeout are higher-order functions.

Objects and arrays#

13. What is the difference between map, filter and reduce?#

map transforms every item and returns a new array of the same length. filter returns a new array with the items that pass a test. reduce combines all items into a single value, such as a total.

JavaScript
const nums = [1, 2, 3, 4];
nums.map((n) => n * 2);          // [2, 4, 6, 8]
nums.filter((n) => n % 2 === 0); // [2, 4]
nums.reduce((a, n) => a + n, 0); // 10

14. What are the spread and rest operators?#

Both use .... Spread expands an array or object: [...a, ...b] or { ...user, age: 22 }. Rest collects remaining items into an array: function sum(...nums) or const [first, ...others] = list.

15. What is the difference between a shallow copy and a deep copy?#

A shallow copy ({ ...obj }, Object.assign) copies the top level only; nested objects are still shared. A deep copy copies everything. Use structuredClone(obj) for a deep copy in modern browsers.

16. What is prototypal inheritance?#

Every object has a link to a prototype. If a property is not found on the object, JavaScript looks for it on the prototype, then that prototype's prototype, and so on. This is the prototype chain. The class keyword is syntax built on top of prototypes.

Asynchronous JavaScript#

17. What is the event loop?#

JavaScript runs on a single thread with one call stack. Async work like timers and network requests is handled by the browser. When it finishes, its callback waits in a queue. The event loop pushes queued callbacks onto the stack when it is empty, running all microtasks (promises) before the next task (timers, events).

18. What is the output of this code?#

JavaScript
console.log("A");
setTimeout(() => console.log("B"), 0);
Promise.resolve().then(() => console.log("C"));
console.log("D");

A, D, C, B. Synchronous logs run first. The promise callback is a microtask, so it runs before the setTimeout callback, which is a task, even with a 0 ms delay.

19. What is a Promise?#

An object representing a value that will be available in the future. It is pending at first, then settles once as fulfilled or rejected. You handle results with .then, errors with .catch, and cleanup with .finally.

20. What is async/await?#

Syntax that lets you write promise code like normal code. An async function always returns a promise, and await pauses that function until a promise settles. Handle errors with try...catch.

JavaScript
async function getUser() {
  try {
    const res = await fetch("/api/user");
    if (!res.ok) throw new Error("Request failed");
    return await res.json();
  } catch (err) {
    console.error(err);
  }
}

21. What is the difference between Promise.all and Promise.allSettled?#

Promise.all resolves when every promise fulfils and rejects as soon as one fails. Promise.allSettled waits for all of them and gives you the status of each, which is better when you want partial results.

DOM and browser#

22. What is event bubbling and capturing?#

When you click an element, the event first travels down from window to the target (capturing), then back up through its ancestors (bubbling). Listeners run during bubbling by default. event.stopPropagation() stops it.

23. What is event delegation?#

Adding one listener to a parent element instead of one to each child, then checking event.target to see which child was clicked. It uses less memory and works for elements added later, thanks to bubbling.

24. What are debouncing and throttling?#

Debouncing waits until events stop for a set time before running a function, which suits search boxes. Throttling runs a function at most once per time interval, which suits scroll and resize handlers.

JavaScript
function debounce(fn, delay) {
  let timer;
  return (...args) => {
    clearTimeout(timer);
    timer = setTimeout(() => fn(...args), delay);
  };
}

This is also a great example of a closure: timer is remembered between calls.

25. What is the difference between localStorage, sessionStorage and cookies?#

localStorage keeps data until it is cleared. sessionStorage keeps data until the tab closes. Both hold about 5 MB and are not sent to the server. Cookies are small (about 4 KB), can expire, and are sent with every request, which is why they are used for sessions.

Tips for answering JavaScript questions in interviews#

  • Think out loud on output questions. Walk through the call stack, microtasks and tasks step by step.
  • Write small examples when explaining closures, this or hoisting.
  • Know a few polyfills: debounce, Array.prototype.map and Function.prototype.bind are common live-coding tasks.
  • Connect to real work: "I used Promise.all to load the user and orders together."

Key takeaways#

  • let and const are block-scoped and sit in the temporal dead zone before their line.
  • A closure remembers its outer variables; the var loop puzzle is a classic.
  • this depends on how a function is called; arrow functions inherit it.
  • Order of execution: synchronous code, microtasks, then tasks.
  • Use async/await with try...catch, and know Promise.all vs allSettled.
  • Be ready to explain and write event delegation and debounce.