Hosting Solutions
mobile_logo Table of Contents


A variable is used to represent values(or objects) that may or may not be subject to changes in a script. Every variable is associated with an object interface that you can access the properties/methods/events of. Alternatively authors can use constants instead of variables when they need to represent values that will not change during script execution.

Representing number and string objects

var num = 5000; var str = "How are you today?";

Change a variable's value in the current scope of the script

num = 10; str = "Where were you born?";

Initialize a variable without defining initial value

var num;

Variables can represent object instances

var now = new Date();

Variables can represent anonymous functions

var x = function(){ return 7; }; alert( x() );

Declaring multiple variables in a single var statement

The following two code blocks are identical for all practical purposes. var x = 50, y = 30, z = 100; var x = 50; var y = 30; var z = 100; Values are optional when declaring variables. Values can be defined for them later in the script. Until they are assigned a value they will return undefined. var x, y, z;



IntroductionImplementationFallback ContentDevelopment OutputSyntax GuidelinesVariablesFunctionsStatementsOperatorsEvent HandlingObjects