Variables

A variable is a name for value which is stored on the browser to be used by a script. All variables have a name, type and value, and should be declared with a var statement before they are used in a script A variable name can be any valid identifier consisting of letters, digits, underscores ( _ ) and dollar signs ($) that does not begin with a digit and is not a reserved JavaScript keyword.

In JavaScript, there are three primitive data types:

  1. Numeric - The numeric data type handles numbers. For example, 12 or 12.77
  2. Strings - consists of letters and other characters. For instance, "hello". The strings are placed inside quote marks. The quotes can be single or double quotes, but they must match. If you start with a single quote, you must end with a single quote, and vice-versa.
  3. Boolean - Boolean data types can have one of two va lues: true or false

tutorial

By Convention, variable names begin with a lowercase letter and each subsequent word begins with upper case if there are more than one word in the variable. For instance, hourlyWage.

It is good programing pactice to to choose meaningful variable names to be able to easily understand what it is used for.

NEXT LESSON