var

JavaScript 1.0, JScript 1.0, ECMAScript 1.0 NES2+, Nav2+, IE3+, Opera3+ Syntax

var variable
var variable = value
						

Description

The var keyword is used to declare variables within a script. If it is used in a function, the scope of the variable is confined to that function. If used outside of a function, it is not limited and can be accessed anywhere on the page.

Example

Listing 6.270 declares the variable, myVar, in three different locations. It is declared once at a global level, once within a function where it is the returned value, and once in a function where it is written to the page. The result of running this script is shown in Figure 6.20.

Figure 6.20. Results of running Listing 6.270 in a browser.

Listing 6.270 This Example ...

Get Pure JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.