You are troubleshooting a web page that includes the following code segment.
You need to evaluate the value of the variable x.
What will be displayed in the user interface?
A.
0
B.
1
C.
2
D.
An error
Explanation:
* Alert(x) is within the scope of the outermost assignment, x=0.
* Local variables have local scope: They can only be accessed within the function.
Example
// code here can 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
}
* The alert() method displays an alert box with a specified message and an OK button.
An alert box is often used if you want to make sure information comes through to the user.
JavaScript Scope