Getting Good with JavaScript - Andrew Burgess [2]
If a snippet doesn't have a title, it's just a quick example that needs to be shown in code, but won't give you any result if you execute it by itself.
You won't fully understand this now, but I need to explain it, and you'll understand it soon: in the snippet above, I've used the alert function to show the result of the code; this function will pop up an alert box, with the text we give it. Notice the two backslashes at the end of the line: those denote a comment in the code (more on this soon); in our case, I've put the result you should see in the alert box in the comment, so you can compare results.
Sometimes, I'll use console.log instead of alert; instead of popping up an alert box, this will write the information to the Firebug console. This the more popular way of getting output when writing JavaScript. We'll use both, so that you're comfortable with both.
Example 1.2
console.log("Hi there"); // Hi there
console.log(1 + 4); // 5
Open the 'example 1.2' file in Firefox, and open Firebug (actually you'll have to refresh the page after you open Firebug, because nothing will log if Firebug isn't enabled for that page). You should see two lines in the console, one saying "Hi there", and the other saying "5."
Summary
I hope you're getting excited, because I know I am! We're about to undertake a journey that will change your life forever (well, at least it could). Let's swing into Chapter 2 to begin talking about the basics of JavaScript.
The Basics
Here's where we actually get down and dirty; in this chapter, we're going to cover the nuts and bolts of JavaScript: all the little seemingly-unrelated pieces that will build the core of your JavaScript knowledge.
Variables
In a minute, we'll be discussing data types in JavaScript, but we need to cover variables first. Depending how you look at this, we might be putting the cart before the horse, but we'll figure this all out. For now, know that we have values like text and numbers in JavaScript.
Variables are basically labels that you give to your values. The variable points to the place in memory where the value is stored. So, how do you make one of these variables?
Example 2.1
var name = "Joe",
age = 45,
job = "plumber";
alert( name ); // Joe
There are several things to notice here: firstly, when creating variables, you must put the keyword var before the variable name. Next comes the variable name. You can name your variables whatever you want, within these boundaries: you can use any combination of letters, numbers, dollar signs, and underscores, as long as you don't start with a number. As you can see, you can declare multiple variables at once by separating the expressions with commas. It's a good idea to do this: at the top of your code, and at the top of every function, declare all the variables you'll need within that file or function (functions: coming soon to a page near you).
But, what do you do if you don't know what the value of that variable should be when you create it? Maybe it's a value that the user is going to give you later on, or something you need to calculate. It's still a good idea to declare the variable, but you don't have to give it a value:
var name;
name = "Jim";
When you declare the variable without giving it a value, the JavaScript engine gives it the default value of undefined. Then, you can give it a new value later.
I know I already said it, but it's important that you understand it: variables are just labels for values. This means they are interchangeable for the most part.
With that out of the way, let's look at the types of data we have in JavaScript.
Types
The first step in learning JavaScript (after we know what a variable is) is to understand two things:
What