JavaScript Interview Questions – Introduction

1. [Amazon] What is JavaScript, and how is it different from Java?

Answer:
JavaScript is a lightweight, interpreted, and dynamic programming language primarily used for building interactive web applications.
Key differences:

  • JavaScript is mainly used for web development, while Java is a general-purpose, object-oriented programming language.
  • JavaScript is an interpreted language, whereas Java is compiled.
  • JavaScript runs in the browser (with exceptions like Node.js), whereas Java applications run in a JVM (Java Virtual Machine).

2. [Google] How does JavaScript work in the browser?

Answer:
When JavaScript runs in the browser:

  1. The browser downloads the JavaScript file and passes it to the JavaScript engine (like V8 in Chrome).
  2. The engine parses and compiles the script into machine code.
  3. The code is then executed line by line (single-threaded).
  4. JavaScript interacts with the DOM (Document Object Model) to update the webpage dynamically.

3. [Microsoft] What are the key features of JavaScript?

Answer:

  • Lightweight and Interpreted: Executes directly in the browser.
  • Dynamic Typing: No need to declare variable types.
  • Prototype-based Object-Oriented: Uses prototype chaining instead of class-based inheritance.
  • Event-Driven and Asynchronous: Supports event listeners and async functions.
  • Runs on Client and Server: Works in browsers and server-side environments like Node.js.

4. [Facebook] What are JavaScript data types?

Answer:
JavaScript has two categories of data types:

  1. Primitive Types (store single values)
    • String โ†’ "Hello, World!"
    • Number โ†’ 42
    • Boolean โ†’ true / false
    • Undefined โ†’ A declared variable without a value
    • Null โ†’ Intentional absence of value
    • Symbol โ†’ Unique identifiers
    • BigInt โ†’ Large integer values
  2. Reference Types (store objects and collections)
    • Object โ†’ { name: "Alice", age: 25 }
    • Array โ†’ [1, 2, 3]
    • Function โ†’ function sayHello() { console.log("Hi"); }

5. [Netflix] What is the difference between null and undefined?

Answer:

  • null: A variable is explicitly set to empty (no value). jsCopyEditlet x = null; console.log(x); // Output: null
  • undefined: A variable is declared but not assigned any value. jsCopyEditlet y; console.log(y); // Output: undefined
  • Key difference: null is an intentional empty value, while undefined means missing or uninitialized.

6. [Adobe] What are var, let, and const? Which one should you use?

Answer:

  • var (Function-scoped)
    • Can be redeclared and updated.
    • Not recommended due to hoisting issues.
  • let (Block-scoped)
    • Can be updated but not redeclared.
    • Recommended for variables that change.
  • const (Block-scoped)
    • Cannot be updated or redeclared.
    • Best for constants (values that don’t change).

Use let for variables that change and const for fixed values.


7. [PayPal] What is Hoisting in JavaScript?

Answer:
Hoisting is JavaScript’s default behavior where variable and function declarations are moved to the top of their scope before execution.

Example:

console.log(name); // Output: undefined
var name = "John";
  • The variable declaration (var name) is hoisted to the top, but not its value.
  • Functions are fully hoisted and can be called before declaration: jsCopyEditgreet(); // Output: "Hello!" function greet() { console.log("Hello!"); }

8. [IBM] What is the difference between == and === in JavaScript?

Answer:

  • == (Loose Equality): Compares values after type conversion. jsCopyEditconsole.log(5 == "5"); // true (string "5" converted to number)
  • === (Strict Equality): Compares both value and type. jsCopyEditconsole.log(5 === "5"); // false (number !== string)
  • Best Practice: Always use === to avoid unexpected type coercion.

9. [Apple] What are JavaScript functions? Explain function declaration vs. function expression.

Answer:
A function is a reusable block of code that performs a task.

  1. Function Declaration (Hoisted) jsCopyEditfunction greet() { console.log("Hello!"); } greet(); // Works before declaration
  2. Function Expression (Not Hoisted) jsCopyEditconst greet = function() { console.log("Hello!"); }; greet(); // Must be defined before calling

Key difference: Function declarations are hoisted, while expressions are not.


10. [Tesla] What is a callback function in JavaScript?

Answer:
A callback function is a function passed as an argument to another function, which executes it later.

Example:

function fetchData(callback) {
    setTimeout(() => {
        console.log("Data fetched");
        callback();
    }, 2000);
}

function processData() {
    console.log("Processing data...");
}

fetchData(processData); // "Data fetched" -> "Processing data..."
  • Why use callbacks? They handle asynchronous operations like API calls and event handling.