Getting Good with JavaScript - Andrew Burgess [3]
How the language is actually written (the syntax)
We're going to start by looking at types of data you can use in JavaScript: the parts that will keep your programs running. Then, we'll move on to looking at syntax: that's how to code things so the JavaScript compiler—the thing that reads your code—will understand it.
String
A string is simply the way to handle any kind of text in JavaScript. Whenever you're working with text, you'll be using a string. A string of text must be surrounded by either double or single quotes; there's no difference. Here are some examples:
"Here is some text"
'This text is surrounded by single quotes'
If you want to include a quote character inside your string, then you'll have to escape it. To escape a character inside a string means to precede the character with a backslash. This basically tells the JavaScript interpreter that the character after the slash shouldn't be interpreted normally; it's something special. In this case, the normal interpretation of a quote is to close the string.
Example 2.2
alert( "John says \"What are you doing?\"" ); // John says "What are you doing?"
alert( '"Let\'s go out for lunch."' ); // "Let's go out for lunch."
Notice how, in that last string, I don't have to escape the double quotes, because the string is delimited by single quotes. You only have to escape the quote-type that holds your string.
Numbers
While some programming languages offer different types of numbers, JavaScript has only one. In most cases, you'll simply type the number of your choice.
10
3.14159
JavaScript makes no distinction (at least for you, the coder) between numbers with and without decimals. For extra large or extra small numbers, you can use scientific notation:
Example 2.3
alert( 3.45e5 ); // 345000
alert( 123e-3 ); // 0.123
The first number above represents 3.45 x 105, or 345,000. The second represents 123 x 10-3, or 0.123. Be careful though: JavaScript doesn't accept numbers with more that 17 decimal places. It will cut off any digits after that.
ROCKSTAR TIP
This is a good time to mention that numbers in JavaScript aren't always that reliable. For example, type 0.1 + 0.2 into Firebug. The result is 0.30000000000000004; what? Why does this happen? The reasons are pretty technical, and they're based on the number specification JavaScript uses. Thankfully, as you start out with JavaScript, you won't be doing too much number crunching. However, consider this a general rule of thumb: when adding and subtracting with decimals, multiply them first. After calculating, divide. We haven't discussed operators, yet, but this is the way we'd do it: ( ( 0.1 * 10 ) + ( 0.2 * 10 ) ) / 10 This will come up with the right answer: 0.3. I know this looks painful, but hey, that's JavaScript: at times, terribly frustrating (but mostly fun and cool).
Booleans
There are two Boolean values: true and false. There are many places you'll use Boolean values in your JavaScript code, and when necessary, you'll use these values. However, more often you'll evaluate other expressions for their "truthiness" or "falsiness." We'll see more about how to do this later on in this chapter, when discussing looping and conditionals.
Null and Undefined
Most languages have a value that is the "non-existent" value. By non-existent, I mean that when you try to get a value that doesn't exist (like a variable with no value), you'll get undefined back. There's another value in this strain, and that's null. There isn't usually a need to distinguish between null and undefined; but if you need to "get rid of" the value in a variable, you can set it to null.
Object
What we've looked at so for are considered primitive values; they're the raw pieces that every language has, in some form or another. Now, we move on to reference values. Reference values are different from primitive values (i.e. strings, numbers, booleans, null, and undefined) in that they are passed around your code by reference. I know that's abstract, so we'll come back