quiz Javascript #javascript #webdevelopment #softwareengineer

preview_player
Показать описание
The function foo() is called in the code snippet. Inside this function, the variable x is re-declared in the local scope and is initialised to 20. Both the variables x in the global scope and x in the local scope are accessible by the function foo(). Now, you know that the local scope takes priority over the global scope; hence, the value of variable x in the local scope is printed on the console, which implies that 20 will be printed.

Then the bar() function is called. Inside the bar() function, the value of x is updated to 30. Note that in this function, x is not declared and initialised to 20. Rather, it is updated. This means that the variable x in the global scope now contains the value 30 and is printed on the console.

Next, the final statement of the code is executed, which prints the value of x. Since x defined in the global scope was updated to 30 by the bar() function previously, the value of x is printed as 30. Therefore, the output is 20 30 30 (each number printed on a new line). Therefore, this is the correct option.
Рекомендации по теме