Category: Interview Questions

  • keyword in JavaScript?

    • Refers to the current execution context.
    • In global → this is window (in browsers).
    • Inside objects → refers to that object.

    Example:

    let person = {
      name: "Ali",
      greet: function() {
    
    console.log("Hello " + this.name);
    } }; person.greet(); //
  • What is this keyword in JavaScript?

    Answer:

    • Refers to the current execution context.
    • In global → this is window (in browsers).
    • Inside objects → refers to that object.

    Example:

    let person = {
      name: "Ali",
      greet: function() {
    
    console.log("Hello " + this.name);
    } }; person.greet(); //
  • forEach() and map()?

    • forEach() → Executes a function on each item, but returns undefined.
    • map() → Transforms elements and returns a new array.

    Example:

    let arr = [1, 2, 3];
    arr.forEach(x => console.log(x * 2)); // prints 2, 4, 6
    console.log(arr.map(x => x * 2)); // [2, 4, 6]
    
  • Event Loop in JavaScript

    • JavaScript is single-threaded.
    • The event loop handles asynchronous tasks (like promises, timers) by moving them from the callback queue to the main execution stack when it’s free.

    Example:

    console.log("Start");
    setTimeout(() => console.log("Callback"), 0);
    console.log("End");
    // Output: Start → End → Callback
    

    Even with 0ms, the async callback runs after synchronous code.

  • var, let, and const in scope?

    • var → Function scoped.
    • let & const → Block scoped {}.

    Example:

    if (true) {
      var x = 1;   // accessible outside
      let y = 2;   // block scope
      const z = 3; // block scope
    }
    console.log(x); // 1
    // console.log(y); // error
  • What are arrow functions?

    • Shorter way to write functions.
    • Do not have their own this.
    let add = (a, b) => a + b;
    console.log(add(5, 3)); // 8
    
  • synchronous and asynchronous JavaScript?

    • Synchronous → Code executes line by line (blocking).
    • Asynchronous → Tasks (like API calls, setTimeout) run in the background without blocking code.

    Example:

    console.log("Start");
    setTimeout(() => console.log("Async Task"), 2000);
    console.log("End");
    // Output: Start → End → (after 2 sec) Async Task
    
  • null and undefined?

    • undefined → variable declared but not assigned.
    • null → assigned value, meaning “nothing”.

    Example:

    let a;      // undefined
    let b = null; // null
  • function declaration and function expression?

    • Function Declaration → Hoisted, can be called before definition.
    • Function Expression → Not hoisted, assigned to a variable.

    Example:

    // Declaration
    function greet() { return "Hello"; }
    
    // Expression
    let sayHi = function() { return "Hi"; };
  • Explain Hoisting in JavaScript.

    • Hoisting means variable and function declarations are moved to the top of their scope before code execution.

    Example:

    console.log(a); // undefined
    var a = 5;
    
    • var is hoisted (declared but not initialized).
    • With let and const, hoisting happens too, but they remain in a temporal dead zone until declared.