Daily JavaScript Quiz - 2024-04-09

  • Home /
  • Daily JavaScript Quiz - 2024-04-09

Daily Quiz - 2024-04-09

var a = 1
function output () {
    var a = 2
}
console.log(a)
output()
console.log(a)

2, 1

2, 2

1, 1

1, 2

The answer is Option 3

  1. var a = 1: This line declares a variable a in the global scope and assigns it the value 1.

  2. function output() { var a = 2; }: This line defines a function named output, which declares a new variable a with the value 2 within its scope. This variable a is local to the function output, and it doesn’t affect the global variable a.

  3. console.log(a): Here, the value of the global variable a is logged to the console. At this point, the value of a is 1, as it hasn’t been changed by the function output.

  4. output(): This line calls the output function, but since it doesn’t return any value, it doesn’t affect the global variable a.

  5. console.log(a): Finally, the value of the global variable a is logged again. It remains 1 because the function output did not modify the global variable a. The a inside the function output is a separate variable local to that function and does not affect the global scope.

So, the output of this code will be:

1
1

Because the function output doesn’t change the global variable a.

Javascript Quizzes