Write the output of the following JavaScript code (Justify youranswer) :(i) var x =10;function function1(){ var x=20; }functionl();alert(x);(ii) x =10;function functionl(){ x=20; }functionl();alert(x);
Question
Write the output of the following JavaScript code (Justify youranswer) :(i) var x =10;function function1(){ var x=20; }functionl();alert(x);(ii) x =10;function functionl(){ x=20; }functionl();alert(x);
Solution
(i) The output of the JavaScript code will be 10. This is because the variable x inside function1() is declared with the var keyword, making it a local variable. This means it only exists within the scope of function1(). So, when we call alert(x), it refers to the global variable x, which is 10.
(ii) The output of the JavaScript code will be 20. This is because the variable x inside function1() is not declared with the var keyword, making it a global variable. So, when we call function1(), it changes the value of the global variable x to 20. Therefore, when we call alert(x), it refers to the global variable x, which is now 20.
Similar Questions
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
Write the output of the following JavaScript statements :(i) document.write(1 + ‘‘2’’ + ‘‘2’’);(ii) document.write(‘‘1’’ + ‘‘2’’ + ‘‘2’’);
What will the following JavaScript code output?for (var i = 0; i < 3; i++) { setTimeout(function() { alert(i); }, 1000 + i);}
What will be the output of the following code snippet?(function(){ setTimeout(()=> console.log(1),2000); console.log(2); setTimeout(()=> console.log(3),0); console.log(4); })();*1 2 3 42 3 4 14 3 2 12 4 3 1
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.