Variables and Assignment

A variable in a programming language is a name given to some storage location for data. You might think of it as a box that holds something or a piece of paper with something written on it, to keep the boxes or papers separate, we can name them.

Variables are defined in JavaScript using the var keyword. Examples:

var a; // Defines a single variable named a, but doesn't anything with it. var a, b; // Defines variables a and b. var thisIsAReallyLongVariableName;

Variables can also be defined with the let keyword, which defines a variable with a more limited "scope", but that is a discussion for a later time. For the time being, just use var when defining a variable.

Keywords in a language BTW, are words that have a very specific meaning in the language, usually to specifically instruct the computer to do something, and you're not usually allowed to make variables or functions that have the same name, so as not to confuse the computer.

Variable names can have letters, numbers and underscores, but must begin with a letter or underscore:

var ok, _ok2; var 3not_ok;

A variable cannot start with a number, because it would confuse the computer into thinking that what follows is a number, so the meaning becomes ambiguous. These rules for variable and function naming are intended to eliminate such ambiguity.

Variables are assigned a value, using the = or assignment operator:

a = 10;

The = is not like in algebra. The = operator stores the value on the right side into the variable specified on the left side. = is not to be confused w/ ==, the equality test operator which is discussed in Boolean Logic.

Until a variable is actually given a value, it is undefined and has the undefined "value".

When variables are used on the right side of an assignment, we just use their values, with some exceptions.

We can define a variable and assign it a value in one step:

var a = 10;

We can change it's value after we've defined it, repeatedly:

a = 2 + 10; // Change a from 10 to (2+10) or 12 //... a = "test"; // Now a holds the string "test"

Variables in JavaScript are "loosely-typed" or "weakly typed". They don't care what kind of data they hold, they can hold anything, this is not true in other programming languages, such as C or C++ which are "strongly-typed".

You will note the ; at the end of each variable assignment. Each assignment is a statement and ; is the end-of-statement-marker and works much like the period at the end of a sentence. You don't always need ;'s in JavaScript, but you should always use them anyway. It's good grammar.

What follows is a demonstration of how two variables a and b change over time:

// value trace for: a | b // --+-- var a, b; // undefined | undefined a = 5; // 5 | undefined b = 10; // 5 | 10 a = a + b; // 15 | 10 b = a - b; // 15 | 5 a = "test"; // "test" | 5

Assignment with operations:

We often want to modify the value of a variable by adding or subtracting, etc things from it. Normally this might be accomplished by:

x = x + 2; // Takes the value of x, adds two to it and assigns the new // value back to x.

There is a shortcut for the generalized form of:

x = x op value;

Where op is one of: +, -, *, /, %, &, |, ^, <<, >>, >>> or **, can be re-written as:

x op= value;

Example:

x += 2; // Same as x = x + 2; x *= 5; // Same as x = x * 5;