27.10.2014 Views

Google Maps API 3

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 3 ■ CREATING YOUR FIRST MAP<br />

Booleans<br />

Booleans can have only two values, true or false. This might not seem much, but it’s actually the logic<br />

on which computers are constructed—you know, ones and zeros. You can actually use 1 instead of true<br />

and 0 instead of false since they mean the same thing. Actually, any numbers except 0 will return true,<br />

so all you’ll need to remember is that 0 is false.<br />

var male = true;<br />

var female = 0; // false<br />

■ Tip You might have noticed that I always include a semicolon at the end of each line of code. This is actually<br />

optional in JavaScript, but I strongly suggest that you always use semicolons since omitting them makes<br />

the JavaScript interpreter guess where they should be. This leads to slower performance and potentially hardto-find<br />

bugs.<br />

Functions<br />

Functions are one of the core features of a programming language. They are perfect for reusing code. A<br />

simple function that will throw an alert with the classic phrase “Hello world” looks like this:<br />

function message() {<br />

alert('Hello world!');<br />

}<br />

To execute it, you call it by its name, including its parentheses:<br />

message();<br />

This will result in the alert shown in Figure 3-4 being thrown.<br />

Figure 3-4. Hello world!<br />

All it does right now is to throw the alert “Hello world” each time you call it, but if you want it to be a<br />

bit more useful, such as throwing an alert with a phrase of your choice, then you need to provide it with<br />

arguments. Arguments are used to pass values to the function.<br />

34

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!