Category: Interview Questions

  • closures in JavaScript?

    A closure is when a function remembers variables from its outer scope, even after the outer function has finished.

    Example:

    function outer() {
      let count = 0;
      return function inner() {
    
    count++;
    return count;
    }; } let counter = outer(); console.log(counter()); // 1 console.log(counter()); // 2

    Here, inner() remembers count from outer().

  • Difference between == and ===?

    • ==loose equality, converts data types before comparison.
    • ===strict equality, checks value and type.

    Example:

    console.log(5 == "5");  // true  (type conversion)
    console.log(5 === "5"); // false (strict check)
  • types of variables in JavaScript?

    • var → Function-scoped, can be redeclared. (Older way, avoid in modern JS)
    • let → Block-scoped, can be updated but not redeclared in the same scope.
    • const → Block-scoped, cannot be updated (immutable reference).

    Example:

    var x = 10; // function scoped
    let y = 20; // block scoped
    const z = 30; // constant