Primitive Data Types, Arrays, Loops, and Conditions | WebReference

Primitive Data Types, Arrays, Loops, and Conditions

Stoyan Stefanov

Digg This Add to

Before diving into the object-oriented features of JavaScript, let's first take a look at some of the basics. This chapter walks you through:

  • The primitive data types in JavaScript, such as strings and numbers
  • Arrays
  • Common operators, such as +, -, delete, and typeof
  • Flow control statements, such as loops and if-else conditions


Variables are used to store data. When writing programs, it is convenient to use variables instead of the actual data, as it's much easier to write pi instead of 3.141592653589793 especially when it happens several times inside your program. The data stored in a variable can be changed after it was initially assigned, hence the name "variable". Variables are also useful for storing data that is unknown to the programmer when the code is written, such as the result of later operations.

There are two steps required in order to use a variable. You need to:

  • Declare the variable
  • Initialize it, that is, give it a value

In order to declare a variable, you use the var statement, like this:

For the names of the variables, you can use any combination of letters, numbers, and the underscore character. However, you can't start with a number, which means that this is invalid:

To initialize a variable means to give it a value for the first (initial) time. You have two ways to do so:

  • Declare the variable first, then initialize it, or
  • Declare and initialize with a single statement

An example of the latter is:

Now the variable named a contains the value 1.

You can declare (and optionally initialize) several variables with a single var statement; just separate the declarations with a comma:

Variables are Case Sensitive

Variable names are case-sensitive. You can verify this statement using the Firebug console. Try typing this, pressing Enter after each line:

To save keystrokes, when you enter the third line, you can only type ca and press the Tab key. The console will auto-complete the variable name to case_matters. Similarly, for the last line—type CA and press Tab. The end result is shown in Figure 1.

Throughout the rest of this book, only the code for the examples will be given, instead of a screenshot:

The three consecutive greater-than signs (>>>) show the code that you type, the rest is the result, as printed in the console. Again, remember that when you see such code examples, you're strongly encouraged to type in the code yourself and experiment tweaking it a little here and there, so that you get a better feeling of how it works exactly.


Operators take one or two values (or variables), perform an operation, and return a value. Let's check out a simple example of using an operator, just to clarify the terminology.

In this code:

+ is the operator The operation is addition The input values are 1 and 2 (the input values are also called operands) The result value is 3

Instead of using the values 1 and 2 directly in the operation, you can use variables. You can also use a variable to store the result of the operation, as the following example demonstrates:

The following table lists the basic arithmetic operators:

Operator symbol Operation Example
+ Addition >>> 1 + 2
- Subtraction >>> 99.99 - 11
* Multiplication >>> 2 * 3
/ Division >>> 6 / 4
% Modulo, the reminder of a division >>> 6 % 3
>>> 5 % 3
It's sometimes useful to test if a number is even or odd. Using the modulo operator it's easy. All odd numbers will return 1 when divided by 2, while all even numbers will return 0.
>>> 4 % 2
>>> 5 % 2
++ Increment a value by 1 Post-increment is when the input value is incremented after it's returned.
>>> var a = 123; var b = a++;
>>> b
>>> a
The opposite is pre-increment; the input value is first incremented by 1 and then returned.
>>> var a = 123; var b = ++a;
>>> b
>>> a
-- Decrement a value by 1 Post-decrement
>>> var a = 123; var b = a--;
>>> b
>>> a
>>> var a = 123; var b = --a;
>>> b
>>> a

When you type var a = 1; this is also an operation; it's the simple assignment operation and = is the simple assignment operator.

There is also a family of operators that are a combination of an assignment and an arithmetic operator. These are called compound operators. They can make your code more compact. Let's see some of them with examples.

In this example a += 3; is just a shorter way of doing a = a + 3;

Here a -= 3; is the same as a = a - 3;


In addition to the arithmetic and assignment operators discussed above, there are other types of operators, as you'll see later in this and the following chapters.

This chapter is an excerpt from the book, Object-Oriented JavaScript by Stoyan Stefanov, published by Packt Publishing Ltd, July 2008, ISBN 1847194141, Copyright 2008 Packt Publishing Ltd

Digg This Add to