Pure Programmer
Blue Matrix

Cluster Map


Named constants are fine for values that never change during the execution of our programs but we also need to have symbols that represent values that do change as our program runs. Variables are symbols that we set up for such purposes. A variable can not only be initialized to a value when it is created, but can be changed as often as we need. Think of a variable as a box or container that has a name drawn on its side. The name of the variable always stays the same, but what is inside the box can be changed.

Variables Illustration


JavaScript constants and variables are not declared with a specific data type in mind. Instead they are generally declared as existing then we can put a value of any type we wish into them. This is idea is known as weak typing. It makes it easy for the programmer to create constants and variables but can lead to some types of run-time errors because the language interpreter can not check to see if the proper type of value has been used. The JavaScript Number type is double-precision 64-bit binary format IEEE 754 value. This means that it can represent integer values between -9,007,199,254,740,991 and 9,007,199,254,740,991. For floating point values the number of significant digits is about 16 and the exponent can range from -308 to 308.

Declaration Syntax

Named constants must be initialized to a specific value when they are declared. Once initialized, they can not have their value changed during the execution of the program. The syntax for a variable declaration consists of the keyword var, the name of the constant (see rules for identifiers), and optionally the equal sign (=) followed by the initializer value for the variable. The declaration ends with a semicolon (;). If no initializer is provided, the variable will have the special value undefined. Examples of statements used to declare variables are illustrated below.

var denominator;
var current_tax_rate = 0.05;
var i = 3;
var errorMsg = "Error 404!";

Variable Declaration Example


The thing that separates variables from constants is that we can change their value at any point in the program. This is done with an assignment statement. Assignment statements consiste of the variable name, the equals sign, and a new value for the variable. Examples of statements used to assign new values to a variable are illustrated below.

denominator = 1.2;
current_tax_rate = 0.075;
i = 4;
errorMsg = "Error 403!";

Variable Assignment Example


  1. Who?
  2. What?
  3. Where?


More ★'s indicate higher difficulty level.