Skip to content

Latest commit

 

History

History
69 lines (57 loc) · 2.23 KB

File metadata and controls

69 lines (57 loc) · 2.23 KB
title ms.custom ms.date ms.prod ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic f1_keywords dev_langs helpviewer_keywords ms.assetid caps.latest.revision author ms.author manager
var Statement (JavaScript) | Microsoft Docs
01/22/2017
windows-client-threshold
devlang-javascript
language-reference
var_JavaScriptKeyword
JavaScript
TypeScript
DHTML
declaring variables, var statement
var statement
56f900af-a5c4-4667-9664-5956d30f0aae
18
mikejo5000
mikejo
ghogen

var Statement (JavaScript)

Declares a variable.

Syntax

var variable1 = value1  

Parameters

variable1
The name of the variable being declared.

value1
The initial value assigned to the variable.

Remarks

Use the var statement to declare variables. You can assign values to the variables when you declare them or later in your script.

A variable is declared the first time it appears in your script.

You can declare a variable without using the var keyword and assign a value to it. This is known as an implicit declaration, and it is not recommended. An implicit declaration gives the variable global scope. When you declare a variable at the procedure level, though, you typically do not want it to have global scope. To avoid giving the variable global scope, you must use the var keyword in your variable declaration.

If you do not initialize your variable in the var statement, it is automatically assigned the [!INCLUDEjavascript] value undefined.

Example

The following examples illustrate the use of the var statement.

var index;  
var name = "Thomas Jefferson";  
var answer = 42, counter, numpages = 10;  
var myarray = new Array();  

Requirements

[!INCLUDEjsv1]

See Also

function Statement
new Operator
Array Object
Variables