PrepAway - Latest Free Exam Questions & Answers

You have a webpage that includes the following markup and code:

You have a webpage that includes the following markup and code:



You need to troubleshoot the code by clicking the Submit button.

Which value will be displayed?

A. 10

B. 20

C. Undefined

D. Runtime error

Explanation:

* The outermost assignment, counter = 10; will decide the output that is displayed.

* Local variables have local scope: They can only be accessed within the function.

Example

// code herecan not use carName

function myFunction() {

var carName = -Volvo-;

// code here can use carName

}

* A variable declared outside a function, becomes GLOBAL.

A global variable has global scope: All scripts and functions on a web page can access it.

Example

var carName = – Volvo-;

// code here can use carName

function myFunction() {

// code here can usecarName

}

Reference: JavaScript Scope

One Comment on “You have a webpage that includes the following markup and code:

  1. _mr says:

    Correct answer is A. 10, but not because of scope difference. Function fun() has been defined and assigned but not invoked. If there would be fun() invoked then variable would have value of 20.




    2



    0

Leave a Reply