JavaScript course menu
Beginner JavaScript Interview Questions and Answers
Common JavaScript interview questions for freshers with clear answers and runnable examples: let vs const vs var, data types, == vs ===, null vs undefined, truthy and falsy, template literals, arrow functions, arrays vs objects, and map vs forEach.
On this page
- 1. What is JavaScript, and where does it run?
- 2. What is the difference between var, let and const?
- 3. What are the data types in JavaScript?
- 4. What is the difference between == and ===?
- 5. What is the difference between null and undefined?
- 6. Which values are falsy?
- 7. What does typeof return for an array, and how do you check for one?
- 8. What are template literals?
- 9. What is the difference between a function declaration and a function expression?
- 10. How are arrow functions different from regular functions?
- 11. What is the difference between map and forEach?
- 12. How do you remove duplicates from an array?
- 13. What is the difference between an array and an object?
- 14. What is NaN, and how do you check for it?
- 15. What does "use strict" do?
- 16. How do you convert a string to a number?
- Keep going
These questions come up in almost every interview for front-end, full-stack and web developer roles. Answer each one out loud first, then open the answer. A short, correct explanation with a tiny example is what interviewers look for.
1. What is JavaScript, and where does it run?#
Show solution
JavaScript is a dynamically typed, high-level programming language and the scripting language of the web. It runs in every browser, where it makes pages interactive through the DOM, and outside the browser with runtimes such as Node.js (servers, tools), as well as in mobile and desktop frameworks. It is standardised as ECMAScript.
2. What is the difference between var, let and const?#
Show solution
varis function-scoped, can be redeclared, and is hoisted asundefined.letis block-scoped, can be reassigned, and throws aReferenceErrorif used before its declaration (the temporal dead zone).constis block-scoped likelet, but cannot be reassigned. Objects stored in aconstcan still be modified.
Use const by default, let when the value changes, and avoid var.
3. What are the data types in JavaScript?#
Show solution
Seven primitives: string, number, bigint, boolean, undefined, null and symbol. Everything else is an object, including arrays, functions and dates. Primitives are immutable and copied by value; objects are passed around by reference.
4. What is the difference between == and ===?#
Show solution
=== (strict equality) compares type and value with no conversion. == (loose equality) converts types first, which gives surprising results:
console.log(0 == "", 0 === "");
console.log("5" == 5, "5" === 5);
console.log(null == undefined, null === undefined);true false
true false
true falseAlways use ===.
5. What is the difference between null and undefined?#
Show solution
undefined means a value was never assigned: an unset variable, a missing argument or a non-existent property. null is assigned deliberately to mean "no value". typeof undefined is "undefined", while typeof null is "object" (a historic bug).
6. Which values are falsy?#
Show solution
false, 0, -0, 0n, "", null, undefined and NaN. Everything else is truthy, including "0", "false", [] and {}.
7. What does typeof return for an array, and how do you check for one?#
Show solution
typeof [] is "object", because arrays are objects. Use Array.isArray(value).
8. What are template literals?#
Show solution
Strings written with backticks that support interpolation with ${expression} and span multiple lines: `Hello, ${name}! You have ${count} messages.`
9. What is the difference between a function declaration and a function expression?#
Show solution
A declaration (function add(a, b) {}) is hoisted, so it can be called before it appears in the code. An expression (const add = function (a, b) {} or an arrow function) is created when that line runs, so it cannot be called earlier.
10. How are arrow functions different from regular functions?#
Show solution
Arrow functions have shorter syntax and an implicit return for single expressions. They do not have their own this (they use the this of the surrounding code), have no arguments object, and cannot be used with new. Use them for callbacks; use regular methods in objects and classes when you need this.
11. What is the difference between map and forEach?#
Show solution
map returns a new array made from the callback's return values. forEach returns undefined and is used only for side effects such as logging.
const nums = [1, 2, 3];
console.log(nums.map((n) => n * 2));
console.log(nums.forEach((n) => n * 2));[ 2, 4, 6 ]
undefined12. How do you remove duplicates from an array?#
Show solution
Convert it to a Set and back:
console.log([...new Set([3, 1, 3, 2, 1])]);[ 3, 1, 2 ]13. What is the difference between an array and an object?#
Show solution
An array is an ordered list accessed by numeric index (list[0]), with methods like push, map and filter. An object stores named properties accessed by key (user.name). Use arrays for collections of similar items and objects for describing a single thing.
14. What is NaN, and how do you check for it?#
Show solution
NaN ("not a number") is a number value produced by invalid maths, such as "abc" * 2. It is not equal to anything, even itself, so NaN === NaN is false. Check with Number.isNaN(value).
15. What does "use strict" do?#
Show solution
It turns on strict mode, which turns silent mistakes into errors (assigning to undeclared variables, writing to read-only properties) and makes this undefined in plain function calls. Modules and classes are always strict.
16. How do you convert a string to a number?#
Show solution
Number("42") converts a whole string (giving NaN if invalid); parseInt("42px", 10) and parseFloat("3.5kg") read a leading number; unary +"42" is a shortcut for Number. Remember that form inputs always give strings.
Keep going#
Move on to the intermediate JavaScript questions, or browse every question with hints on the JavaScript interview page.
Finished this lesson?