What is a variable?

A variable is a container that holds a value.

A variable can be used in the script for calculating, adding, determining information to be true or false, to be placed into an HTML tag or an assortment of other things. Any variables to be used by the program need to be declared as a variable so that the script knows what to do with them. With some programming languages you must declare what type of variable, number, string, or Boolean, that will be held in the container, but with JavaScript, this is not required. This is becuase JavaScript is what is known as a dynamically typed language.

Variables can hold information such as a counter for a loop, someone first name, age, a certain quantity. The value of the variable may well change during the running of the script. The variable can hold a value right from the start, it can be assigned a null value or it can not be assigned any value, making it what is called undefined.

When assigning a value to a variable, you use a single = sign. There are times in JavaScript where you are testing the value of the variable, then you use double == signs, this situation will be disucussed in a later article. To declare a variable name in JavaScript, use the following formats. You can put each variable on a separate line, or you can declare a group of variables and separate each with a comma putting the semi-colon at the very end.

Rules for naming variables

  • Variable names, also called identifiers, can consist of uppercase and lowercase letters, numbers, dollar signs $ and underscores _.
  • The variable name must start with an underscore _, a letter or a dollar sign $. It can not start with a number.
  • JavaScript is case sensitive. You must be consistant with the use of lowercase or uppercase when you use the variable later on in the script.
  • You cannot use dashes - in identifiers or other characters

var fName - "Fred";
var counterA;  //this would be a undefined variable
var init_Value = 34;  //if the variable is a number, do not use quotation marks.


var fName = "Fred", counterA, init_Value = 34;

You can declare your variables right in the beginning of the script, or they can be declared closer to where they will be used. Global variables, vs those variables to be used inside a function, have special rules that will be covered later.

Three dot cartouche

Types of values in Javascript

In JavaScript, there is something known as primitive types when referring to values. Primitive types are the following:

String Number Boolean null undefined

String Type

When declaring a variable that is to be used as a string, you place it inside of single or double quotes. Usually it is better to use double quote for strings as the string itself may have an apostrophe which can confuse the matter as JavaScript will think that it has reached the end of the string. If you put a number inside of quotation marks when declaring a variable, JavaScript will see it as a string and not a number.

var boysName = "Tommy";
var introText = "Welcome to Tommy's World"

Number Type

Variables used as numbers are those with a numeric value. The numeric value can be positive or negative. It can be a round number (integer) or it can have decimal values. You do not put the value in quotation marks. You do not combine the number with any letters or other characters other than a decimal point .. You do not separate values with a comma to show thoushands. To declare a negative value, preceed the value with a minus sign -

var retAge = 65;
var intRate = 0.043568;
var reduceFactor = -3.568;

Boolean Type

A boolean value is simply true or false. It is used for testing if certain situations are true. If so, something happens, if false, something else happens. When declaring Boolean values, do not put true or false in quotation marks, else the value becomes a string.

var tookTest = true;  //note how the value is NOT placed inside of quotes
var handHeld = false; //note how the value is NOT placed inside of quotes

null Type

With JavaScript, a null value represents an absence of a value. If declaring a variable the null value, do not place the word null in quotation marks. When you evaluate a variable that has a value of null in your program, the null value is considered to be 0 (the number zero) in mathmatical operations and false in Boolean operations.

var bootSize = null;  //note how the value is NOT placed inside of quotes

undefined Type

With JavaScript you can declare a variable to be undefined. The value of undefined is evaluated differently than null when testing the variable. Do not put the word undefined inside of quotes. You can choose to just declare the variable and not assign a value to it, this is the same as calling it undefined.

var goPath = undefined; //note how the value is NOT place inside of quotes
var topNum; //this is the same as declaring it undefined

Three dot cartouche

Variable Scope: Global Variables vs. Local variables

Global Variables

When you declare a variable outside of a function, it is considered to be a global variable. This means that it is available as a variable to any other code in the document.

Local Variable

When you declare a variable inside of a function, in is considered to be a local variable. This variable is only recognized inside of that function.