How to Use Variables in JavaScript

JavaScript variables are containers for storing data values. All JavaScript variables must be identified with unique names. These unique names are called identifiers. Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume). JavaScript identifiers are case-sensitive.

Rules of Naming Variables

The general rules for constructing names for variables (unique identifiers) are:

  • Names can contain letters, digits, underscores, and dollar signs.
  • Names must begin with a letter.
  • Names are case sensitive (a and A are different variables).
  • Reserved words (JavaScript keywords) cannot be used as names.

Assignment Operator

In JavaScript, the equal sign (=) is an "assignment" operator. The "equal to" operator is written like == in JavaScript. You declare a JavaScript variable with the var keyword.


var carName;
var x = 5;
var y = 8;

Here, x stores the value 5 and y stores the value 8. If you re-declare a JavaScript variable, it does not lose its value.

Data Types

JavaScript variables can hold numbers and text strings. Strings are written inside double or single quotes. Numbers are written without quotes. If you put a number in quotes, it is treated as a text string.

You can use the JavaScript typeof operator to find the type of a JavaScript variable. The typeof operator returns the type of a variable or an expression. In JavaScript, a variable without a value, has the value undefined. The type is also undefined. Any variable can be emptied, by setting the value to undefined.

1. String

A string (or a text string) is a series of characters. Strings are written with single or double quotes. To find the length of a string, you can use the built-in length property. For example,

var sln = txt.length;

2. Number

JavaScript has only one type of number. Numbers can be written with, or without decimals. JavaScript numbers are always stored as double precision floating point numbers.

3. Boolean

Boolean can only have two values: true or false. These are often used in conditional testing. A Boolean expression is used within the if statement's condition to determine whether the code within the braces will be executed.

4. Null

Null is special data type in JavaScript. Null is, simply, nothing. It represents and evaluates to false. An empty value or variable is not null.

5. Undefined

Undefined is a state, sometimes used like a value, to represent a variable that has not yet contained a value. This state is different from null, although both null and undefined can evaluate the same way.

6. Arrays

An array is a special variable, which can hold more than one value at a time. JavaScript arrays are written with square brackets. Array items are separated by commas. Array indexes are zero-based, which means the first item is [0], second is [1], and so on.

var array_name = [item1, item2, ...];

7. Objects

JavaScript objects are written with curly braces {}. Object properties are written as name:value pairs, separated by commas. The objects in JavaScript are a collection of properties, each of which can contain a value. You can define your own objects with JavaScript, or you can use the several built-in objects.

For example, to create an empty object:

var myObject = {};

Variable Scope

A variable’s scope refers to the locations from which its value can be accessed. Variables are globally scoped when they are used outside a function. A globally scoped variable can be accessed throughout the JavaScript program.

Variables defined within a function are scoped solely within that function. This means that the values of those variables cannot be accessed outside the function. Function parameters are scoped locally to the function as well.