Top 10 Tricky JavaScript Interview Questions: The Ultimate Challenge Quiz

  1. Hoisting and Variable Output

    Given the following JavaScript code:nnconsole.log(variable_1);nvar variable_1 = 10;nif (true) { var variable_2 = 20; }nconsole.log(variable_2);nnWhat will be printed to the console?

    1. A. undefined, 20
    2. B. 10, undefined
    3. C. ReferenceError, 20
    4. D. undefined, undefined
    5. E. 10, 20
  2. Function Scope and Let Keyword

    Consider this loop:nnfor (let i = 0; i u003C 5; i++) {n setTimeout(() =u003E console.log(i), 0);n}nnWhat values will be printed to the console and in what order?

    1. A. 0, 1, 2, 3, 4
    2. B. 5, 5, 5, 5, 5
    3. C. 0, 1, 2, 3, 4 in reverse
    4. D. undefined, undefined, undefined, undefined, undefined
    5. E. 1, 2, 3, 4, 5
  3. Closures and Persistent Counters

    You are asked to write a function that returns a function which increments and returns a private counter each time it's called. Which concept makes this possible in JavaScript?

    1. A. Closure
    2. B. Event Loop
    3. C. Hoisting
    4. D. Currying
    5. E. Block Scope
  4. Closures Gone Wrong

    Given the following code:nnfor(var n=1; nu003C=5; n++){n setTimeout(function(){ console.log('n = ' + n); }, 1000);n}nnWhat will be printed after one second?

    1. A. n = 1, n = 2, n = 3, n = 4, n = 5
    2. B. n = 5, n = 5, n = 5, n = 5, n = 5
    3. C. n = 0, n = 1, n = 2, n = 3, n = 4
    4. D. Undefined, five times
    5. E. n = 4, n = 3, n = 2, n = 1, n = 0
  5. Hoisting Nuances with Let and Var

    Given:nnvar a = 35;nfunction test() {n console.log(a);n let a = 10;n console.log(a);n}ntest();nnWhat will be the output?

    1. A. 35, 10
    2. B. ReferenceError, 10
    3. C. undefined, 10
    4. D. 10, 35
    5. E. 10, undefined
  6. Understanding 'this' with Arrow Functions

    Given the following code:nnconst obj = {n value: 42,n regular: function() { return this.value; },n arrow: () =u003E this.valuen};nnWhat will obj.regular() and obj.arrow() respectively return?

    1. A. 42, undefined
    2. B. undefined, 42
    3. C. 42, 42
    4. D. undefined, undefined
    5. E. 0, 42
  7. Difference in Object Creation

    Which of the following statements correctly describes the difference between creating an object with {} and using Object.create(null)?

    1. A. Object.create(null) creates an object with no prototype; {} creates an object with Object.prototype.
    2. B. Both methods create objects with no prototype.
    3. C. {} creates an object with Array.prototype; Object.create(null) does not.
    4. D. Object.create(null) creates an object with primitive properties only.
    5. E. {} is slower than Object.create(null).
  8. Alternative to Closures

    If JavaScript did not support closures, which of the following would be most affected?

    1. A. The ability to create private variables in functions
    2. B. Hosting external scripts
    3. C. Prototypical inheritance
    4. D. Variable hoisting
    5. E. Global scope manipulation
  9. Output Prediction with Variable Hoisting

    Consider this example:nnconsole.log(a);nvar a = 10;nfunction foo() {n console.log(a);n var a = 15;n bar();n function bar() { console.log('foo:' + a + ' bar:' + b); }n var b = 15;n}nfoo();nnWhat will be printed?

    1. A. undefined, undefined, foo:15 bar:15
    2. B. undefined, undefined, foo:undefined bar:15
    3. C. undefined, undefined, foo:undefined bar:undefined
    4. D. undefined, undefined, foo:10 bar:15
    5. E. undefined, undefined, foo:1 bar:15
  10. Pros and Cons of Closures

    Which of the following correctly states a disadvantage of using closures excessively in JavaScript applications?

    1. A. They can lead to higher memory usage due to retained references.
    2. B. They prevent variable hoisting.
    3. C. Closures cannot be used with arrow functions.
    4. D. They disable asynchronous execution.
    5. E. They only allow primitive data types to be captured.