Skip to content
CompanyBenchCompanyBench

JavaScript coding interview questions and answers

JavaScript interview questions with short answers on equality, arrow functions, timers, hoisting, closures, null vs undefined, Promises and template literals.

Core JavaScript interview questions covering ES6+, asynchronous code, the DOM and small coding tasks, with short answers. Level: beginner to intermediate. For framework-level questions, see the React developer interview questions.

Frequently asked questions

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

== compares values after type coercion. === compares value and type, with no coercion.

What are arrow functions and how do they differ from regular functions?

They are a shorter way to write functions. They do not bind their own this, arguments or super, and they cannot be used as constructors.

What is the difference between setTimeout and setInterval?

setTimeout runs a function once after a delay. setInterval runs it repeatedly at a fixed interval until you call clearInterval().

How do you reverse a string in JavaScript?

Split it into characters with .split(''), reverse the array with .reverse(), then join it back with .join('').

How do you add a click event to a button using JavaScript?

Call addEventListener('click', handler) on the button element.

What is hoisting in JavaScript?

Declarations are moved to the top of their scope during compilation. Variables declared with var are hoisted and set to undefined. let and const are hoisted but not initialised, so using them before the declaration throws an error (the temporal dead zone).

Explain closures in JavaScript.

A closure is a function together with references to the scope it was created in. It lets an inner function keep using the outer function's variables after the outer function has returned.

What is the difference between null and undefined?

undefined means a variable was declared but never given a value. null is a value you assign on purpose to mean "no value". typeof null returns 'object', which is a long-standing quirk of the language.

What are Promises and how do they work?

A Promise represents a value that may be available now, later or never. It is pending, fulfilled or rejected, and you handle the outcome with then(), catch() and finally().

What are template literals and how are they used?

Strings written with backticks. They support embedded expressions with ${} and can span multiple lines.