Chapter 2. Primitive Data Types, Arrays, Loops, and Conditions

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 following topics:

  • 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

Variables are used to store data; they are placeholders for concrete values. When writing programs, it's 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 initially assigned, hence the name variable. You can also use variables to store data that is unknown to you while you write the code, such as the result of a later operation.

Using a variable requires the following two steps. You will need to:

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

To declare a variable, you will use the var statement like the following piece of code:

    var a; 
    var thisIsAVariable;  
    var _and_this_too;  
    var mix12three; 

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

    var 2three4five; 

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

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

An example of the latter is as follows:

    var a = 1; 

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, as shown in the following line of code:

    var v1, v2, v3 = 'hello', v4 = 4, v5; 

For readability, this is often written using one variable per line, as follows:

    var v1,  
        v2,  
        v3 = 'hello',  
        v4 = 4,  
        v5; 

Note

The $ character in variable names

You may see the dollar sign character ($) used in variable names, as in $myvar or less commonly my$var. This character is allowed to appear anywhere in a variable name, although previous versions of the ECMA standard discouraged its use in handwritten programs and suggested it should only be used in generated code (programs written by other programs). This suggestion is not well respected by the JavaScript community, and $ is in fact commonly used in practice as a function name.

Variables are case sensitive

Variable names are case sensitive. You can easily verify this statement using your JavaScript console. Try typing the following code by pressing Enter after each line:

    var case_matters = 'lower'; 
    var CASE_MATTERS = 'upper';  
    case_matters; 
    CASE_MATTER; 

To save keystrokes when you enter the third line, you can type case and press the Tab or right arrow key. Console autocompletes the variable name to case_matters. Similarly, for the last line, type CASE and press the Tab key. The end result is shown in the following figure:

Variables are case sensitive

Throughout the rest of this book, only the code for the examples is given instead of a screenshot, as follows:

    > var case_matters = 'lower'; 
    > var CASE_MATTERS = 'upper'; 
    > case_matters; 
    "lower" 
    > CASE_MATTERS; 
    "upper" 

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

Note

You can see in the preceding screenshot that sometimes what you type in Console results in the word undefined. You can simply ignore this, but if you're curious, here's what happens when evaluating (executing) what you type-the Console prints the returned value. Some expressions, such as var a = 1;, don't return anything explicitly, in which case, they implicitly return the special value undefined (more on in a bit). When an expression returns some value (for example, case_matters in the previous example or something such as 1 + 1), the resulting value is printed out. Not all consoles print the undefined value; for example, the Firebug console.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.255.127