What is the output of following code?var a = 10;function test() { a = 20;}test();console.log(a);Options: Pick one correct answer from below1020
Question
What is the output of following code?
var a = 10;
function test() {
a = 20;
}
test();
console.log(a);
Options: Pick one correct answer from below
- 10
- 20
Solution
The output of the code will be 20.
Here's the step by step explanation:
- The variable
a
is declared and initialized with the value10
in the global scope. - A function
test()
is declared which assigns the value20
to the variablea
. - The function
test()
is called. This changes the value ofa
from10
to20
. console.log(a)
is called which prints the current value ofa
to the console. Sincetest()
has already been called and changed the value ofa
to20
,20
is printed to the console.
Similar Questions
What is the output of this code?const a = 12;function myFunction(a) { console.log(a);}myFunction(89);112289
What is the output of this code?function myFunction(a) { console.log(a + b);}const b = 79;myFunction(10);
What will the below statements print on the console?"use strict"function abc() {console.log(this);}abc();
What will the following JavaScript code output?(function() { var a = b = 5;})();console.log(b);5ReferenceError: b is not definednullundefined
What will be the output of the following code snippet?var a = 1; var b = 0; while (a <= 3) { a++; b += a * 2; print(b); }*4 10 181 2 3None of Above1 4 7
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.