Declaring Variables with the var Keyword

Problem
You want to know how to create and assign values to variables in JavaScript.

Solution
In JavaScript, variable creation and assignment are done using the var keyword.

Examples

// A single variable is declared.
var oneVariable;

// Multiple variables are declared.
var firstVariable, secondVariable;

/* The variables firstVariable and secondVariable are declared.
 * firstVariable is assigned the number 5, while secondVariable is undefined.
 */
var firstVariable = 5, secondVariable;

/* The variables firstVariable and secondVariable are declared.
 * firstVariable is assigned the number 10, and secondVariable is assigned the string ten.
 */
var firstVariable = 10, secondVariable = "ten";

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s