Interview Q&A / JavaScript

INTERVIEW Q&A · 71 QUESTIONS

JavaScript interview questions

Closures, the event loop, this, promises and the core language questions every frontend round asks.

  1. Are strings mutable in JavaScript?

    No. Strings are immutable primitives. Methods like toUpperCase, slice and replace never change the original; they return a new string, so you must assign or use the result.

    BeginnerQuick answer1 min read
  2. How do default values and renaming work in object and array destructuring?

    In objects, {name: n = 'x'} reads property name into variable n with a default used only when the value is undefined. In arrays, position matters: [a, , c = 9]. Defaults ignore null.

    BeginnerQuick answer1 min read
  3. How do Number(), parseInt() and the unary plus differ when converting a string to a number?

    Number() and unary + are strict: any junk gives NaN. parseInt reads digits from the left and stops at the first non-digit, so "42px" gives 42. Always pass a radix to parseInt.

    BeginnerQuick answer1 min read
  4. How do you check whether a property exists on an object?

    Use Object.hasOwn(obj, key) for own properties and the in operator to include inherited ones. Comparing to undefined is unreliable, because a property can exist and hold undefined.

    BeginnerQuick answer1 min read
  5. How do you check whether a value is an array in JavaScript?

    Use Array.isArray(value). typeof returns "object" for arrays, and instanceof Array can fail for arrays created in another frame or realm.

    BeginnerQuick answer1 min read
  6. How do you remove duplicate values from an array?

    The shortest way is [...new Set(arr)], because a Set stores each value once and keeps insertion order. Filtering with indexOf also works but is slower. For objects, dedupe by a key using a Map.

    BeginnerQuick answerCoding1 min read
  7. How does Array.reduce work, and why should you give it an initial value?

    reduce walks the array carrying an accumulator: each callback call gets the accumulator and the current item and returns the next accumulator. Without an initial value, an empty array throws a TypeError.

    BeginnerQuick answer1 min read
  8. How would you mask all but the last four digits of a card number in JavaScript?

    Take the last four characters with slice(-4) and pad the front back to the original length with padStart(length, '*'). padStart and padEnd fill a string to a target length from the left or right.

    BeginnerQuick answer1 min read
  9. Is JavaScript pass by value or pass by reference?

    JavaScript is always pass by value. For objects, the value passed is a reference, so a function can change the object's contents but cannot make the caller's variable point to a different object.

    BeginnerQuick answer1 min read
  10. What are template literals and why are they better than string concatenation?

    Template literals use backticks. They allow ${expression} interpolation and multi-line strings without escape characters, which makes strings with variables far easier to read than chains of + signs.

    BeginnerQuick answer1 min read
  11. What are the primitive data types in JavaScript?

    JavaScript has seven primitive types: string, number, bigint, boolean, undefined, null and symbol. Everything else, including arrays and functions, is an object.

    BeginnerQuick answer1 min read
  12. What are the states of a Promise, and what happens if you resolve it twice?

    A promise is pending, then becomes fulfilled with a value or rejected with a reason. Once settled it never changes, so later resolve or reject calls are ignored.

    BeginnerQuick answer1 min read
  13. What are truthy and falsy values in JavaScript?

    When a value is used in a condition, JavaScript converts it to a boolean. Only eight values become false: false, 0, -0, 0n, empty string, null, undefined and NaN. Everything else, including [] and {}, is truthy.

    BeginnerQuick answer1 min read
  14. What do Object.keys, Object.values, Object.entries and Object.fromEntries do?

    keys gives an array of own enumerable property names, values gives their values, and entries gives [key, value] pairs. fromEntries does the reverse and builds an object from pairs, which makes transforming objects easy.

    BeginnerQuick answer1 min read
  15. What does an async function return?

    Always a Promise. A returned value becomes the fulfilled value, and a thrown error becomes a rejection. That is true even when the function contains no await.

    BeginnerQuick answer1 min read
  16. What does typeof return for a variable that was never declared?

    typeof returns the string "undefined" for an undeclared name and does not throw. Reading the same name any other way throws a ReferenceError.

    BeginnerQuick answer1 min read
  17. What is an IIFE and why would you use one?

    An IIFE (immediately invoked function expression) is a function that is defined and called in one go, like (function () { ... })(). It creates a private scope so variables inside do not leak out.

    BeginnerQuick answer1 min read
  18. What is lexical scope in JavaScript?

    Lexical scope means a function's accessible variables are decided by where it is written in the source code, not by where it is called. Inner code can see outer variables, never the reverse.

    BeginnerQuick answer1 min read
  19. What is NaN, and how do you check whether a value is NaN?

    NaN means 'Not a Number' and comes from failed numeric operations like Number("abc"). It is the only value not equal to itself, so use Number.isNaN(value), which avoids the coercion done by the older global isNaN.

    BeginnerQuick answer1 min read
  20. What is the difference between a function declaration and a function expression?

    A declaration is hoisted with its body, so you can call it before the line it appears on. An expression assigns a function to a variable, and the variable follows var, let or const rules, so calling it early fails.

    BeginnerQuick answer1 min read
  21. What is the difference between a shallow copy and a deep copy of an object?

    A shallow copy duplicates only the top level, so nested objects are still shared with the original. A deep copy duplicates every level, so the two are fully independent. Spread and Object.assign are shallow; structuredClone is deep.

    BeginnerQuick answer1 min read
  22. What is the difference between call, apply and bind?

    All three set this for a function. call runs it now with arguments listed one by one, apply runs it now with an array of arguments, and bind returns a new function with this fixed, to be run later.

    BeginnerQuick answer1 min read
  23. What is the difference between implicit and explicit type coercion?

    Explicit coercion is when you convert on purpose, like Number("5") or String(5). Implicit coercion is when JavaScript converts for you during an operation, like "5" * 2 or if (value).

    BeginnerQuick answer1 min read
  24. What is the difference between includes and indexOf for arrays?

    includes returns true or false and can find NaN. indexOf returns the position or -1, uses strict equality and can never find NaN. Use includes for a yes/no check and indexOf when you need the position.

    BeginnerQuick answer1 min read
  25. What is the difference between map and forEach?

    map returns a new array built from your callback's return values. forEach returns undefined and is only for side effects like logging. Use map to transform data and forEach to do something with each item.

    BeginnerQuick answer1 min read
  26. What is the difference between null and undefined?

    undefined means a value was never assigned: JavaScript gives it to you automatically. null means 'no value' and is set on purpose by the programmer. They are loosely equal (null == undefined) but not strictly equal.

    BeginnerQuick answer1 min read
  27. What is the difference between replace and replaceAll on strings?

    replace with a string pattern changes only the first match. replaceAll changes every match. With a regular expression, replace needs the g flag to change all matches, and replaceAll throws if the regex lacks it.

    BeginnerQuick answer1 min read
  28. What is the difference between slice and splice?

    slice copies a section into a new array and leaves the original alone. splice changes the original array in place by removing, replacing or inserting items, and returns the removed items.

    BeginnerQuick answer1 min read
  29. What is the difference between slice and substring on a string?

    Both return part of a string without changing it. slice accepts negative indexes, counting from the end, and returns an empty string if start is after end. substring treats negatives as 0 and swaps its arguments if start is greater than end.

    BeginnerQuick answer1 min read
  30. What is the difference between the spread and rest operators?

    Both are written as three dots. Rest collects many values into one array or object, in function parameters and destructuring. Spread expands one array or object into many items, in calls and literals.

    BeginnerQuick answer1 min read