BC.

Full Stack Web & Mobile App Developer

JavaScript Interview Questions

Found 600 questions

1. What is JavaScript and where is it commonly used?

JavaScript is a programming language mainly used to make websites interactive and dynamic. It runs inside web browsers and can also run on servers using technologies like Node.js. It is one of the core technologies of web development along with HTML and CSS.

Example: When you click a button and a popup appears on a website, JavaScript is usually handling that action.

button.addEventListener("click", () => {
  alert("Button clicked!");
});

2. What are the different ways to include JavaScript in a web page?

JavaScript can be added to a web page in three main ways: inline, internal, and external. The external method is the most commonly used because it keeps code clean and reusable.

Inline JavaScript

  • Written directly inside HTML elements

  • Best for very small actions

Internal JavaScript

  • Written inside <script> tags in the same HTML file

  • Useful for small projects or testing

External JavaScript

  • Written in a separate .js file

  • Best for large applications and reusable code

Example: A button click action can be handled using inline JavaScript.

Inline

<button onclick="alert('Hello')">Click</button>

Internal

<script>
  console.log("Hello");
</script>

External

<script src="app.js"></script>

3. What is the difference between interpreted and compiled languages, and where does JavaScript fit?

Compiled languages are converted into machine code before execution.

Interpreted languages are executed line by line by an interpreter at runtime.

JavaScript is mainly considered an interpreted language, but modern JavaScript engines like Google V8 use Just-In-Time (JIT) compilation to improve performance.

Example

  • C++ code is compiled into an executable file before running.

  • JavaScript runs directly in the browser.

console.log("Hello JavaScript");

The browser executes this code immediately.

4. What are the primitive data types in JavaScript?

Primitive data types are basic immutable values in JavaScript. JavaScript has 7 primitive data types.

  • string → text values

  • number → integers and decimals

  • bigint → very large numbers

  • booleantrue or false

  • undefined → variable declared but not assigned

  • null → intentional empty value

  • symbol → unique identifiers

let name = "John";       // string
let age = 25;            // number
let isActive = true;     // boolean
let score = null;        // null
let data;                // undefined

5. What is the difference between primitive and reference types in JavaScript?

Primitive types store actual values directly, while reference types store a reference (memory address) to the value. Primitive values are copied by value, but reference values are copied by reference.

Primitive Types
  • Immutable

  • Stored directly

  • Examples: string, number, boolean, null, undefined, symbol, bigint

Reference Types
  • Stored in memory by reference

  • Objects can be modified

  • Examples: object`, `array`, `function

Example: Changing one primitive variable does not affect another copied variable. Changing a referenced object affects all references pointing to it.

// Primitive
let a = 10;
let b = a;
b = 20;
console.log(a); // 10
console.log(b); // 20

// Reference
let user1 = { name: "John" };
let user2 = user1;
user2.name = "Mike";
console.log(user1.name); // Mike

6. What is the difference between null and undefined?

undefined means a variable has been declared but not assigned a value. null means an intentional empty or unknown value set by the developer. Both represent absence of value, but they are used differently.

undefined
  • Default value for uninitialized variables

  • Returned by functions with no return statement

  • Type is "undefined"

null
  • Assigned manually

  • Represents empty or missing data

  • Type is "object" (this is a known JavaScript behavior)

Example:

let a;
console.log(a); // undefined
let b = null;
console.log(b); // null

7. What is NaN in JavaScript?

NaN stands for Not a Number. It represents an invalid numeric result in JavaScript. It usually appears when a mathematical operation cannot produce a valid number.

  • NaN is a special numeric value

  • Type of NaN is "number"

  • Produced by invalid math operations

  • NaN is not equal to itself

Example: Trying to divide text by a number returns NaN.

console.log("hello" / 2); // NaN
console.log(typeof NaN); // "number"
console.log(NaN === NaN); // false

8. How do you check whether a value is NaN?

The recommended way to check for NaN is using Number.isNaN(). It checks whether a value is truly NaN without unwanted type conversion.

  • Number.isNaN() → safest and recommended

  • Global isNaN() converts values before checking

  • NaN !== NaN, so direct comparison does not work

Example: Checking invalid math results.

let result = 0 / "hello";
console.log(Number.isNaN(result)); // true

Difference between isNaN() and Number.isNaN():

console.log(isNaN("123")); // false
console.log(isNaN("hello")); // true
console.log(Number.isNaN("hello")); // false
console.log(Number.isNaN(NaN)); // true

9. What is the typeof operator and what are its limitations?

The typeof operator is used to check the data type of a value in JavaScript. It returns the type as a string. However, it has some known limitations and can give unexpected results for certain values.

Limitations
  • typeof null returns "object" (known JavaScript bug)

  • Arrays also return "object"

  • Cannot clearly identify objects, arrays, and null

console.log(typeof "Hello"); // string
console.log(typeof 10);      // number
console.log(typeof true);    // boolean
console.log(typeof null);     // object
console.log(typeof [1, 2]);   // object
console.log(typeof {});       // object

Better array checking:

console.log(Array.isArray([1, 2])); // true

10. Why does typeof null return "object"?

typeof null returns "object" because of an old bug in JavaScript. In the early implementation of JavaScript, values were stored with type tags, and null was incorrectly tagged as an object. This behavior became part of the language and could not be changed later for backward compatibility.

  • null is not actually an object

  • It is a primitive value

  • Result is a historical JavaScript bug

  • Kept unchanged to avoid breaking old code

console.log(typeof null); // "object"

Correct way to check for null:

let value = null;
console.log(value === null); // true