Primitive Data Types, Arrays, Loops, and Conditions: Part 3 / Page 2 | WebReference

Primitive Data Types, Arrays, Loops, and Conditions: Part 3 / Page 2

[previous] [next]

Primitive Data Types, Arrays, Loops, and Conditions: Part 3 [con't]

Arrays of arrays

An array can contain any type of values, including other arrays.

Let's see an example where you have an array of two elements, each of them being an array.

The first element of the array is a[0] and it is an array itself.

To access an element in the nested array, you refer to the element index in another set of square brackets.

Note also that you can use the array notation to access individual characters inside a string.

There are more ways to have fun with arrays (and we'll get to that in Chapter 4), but let's stop here for now, remembering that:

  • An array is a data store
  • An array contains indexed elements
  • Indexes start from zero and increment by one for each element in the array
  • To access array elements we use the index in square brackets
  • An array can contain any type of data, including other arrays

Conditions and Loops

Conditions provide a simple but powerful way to control the flow of execution through a piece of code. Loops allow you to perform repeating operations with less code. Let's take a look at:

  • if conditions,
  • switch statements,
  • while, do-while, for, and for-in loops.

Code Blocks

Let's start by clarifying what a block of code is, as blocks are used extensively when constructing conditions and loops.

A block of code consists of zero or more expressions enclosed in curly brackets.

You can nest blocks within each other indefinitely:

Best Practice Tips

  • Use end-of-line semicolons. Although the semicolon is optional when you have one expression per line, it's good to develop the habit of using them. For best readability, the individual expressions inside a block should be placed one per line and separated by semi-colons.
  • Indent any code placed within curly brackets. Some people use one tab indentation, some use four spaces, and some use two spaces. It really doesn't matter, as long as you're consistent. In the example above the outer block is indented with two spaces, the code in the first nested block is indented with four spaces and the innermost block is indented with six spaces.
  • Use curly brackets. When a block consists of only one expression, the curly brackets are optional, but for readability and maintainability, you should get into the habit of always using them, even when they're optional.

Ready to jump into loops and ifs? Note that the examples in the following sections require you to switch to the multi-line Firebug console.

if Conditions

Here's a simple example of an if condition:

The parts of the if condition are:

  • The if statement
  • A condition in parentheses—"is a greater than 2?"
  • Code block to be executed if the condition is satisfied

The condition (the part in parentheses) always returns a boolean value and may contain:

  • A logical operation: !, && or ||
  • A comparison, such as ===, !=, >, and so on
  • Any value or variable that can be converted to a boolean
  • A combination of the above

There can also be an optional else part of the if condition. The else statement is followed by a block of code to be executed if the condition was evaluated to false.

In between the if and the else, there can also be an unlimited number of else if conditions. Here's an example:

You can also nest conditions by putting new conditions within any of the blocks.

[previous] [next]