JavaScript is a powerful and complex language—you can work with it for 10 years and still have more to learn. But don't let that frighten you away, you don't have to know everything about it to be able to take advantage of what it has to offer. In fact, you just have to get down a few basics.
This section introduces some JavaScript basics and JavaScript syntax. Don't be scared away by that developer word—syntax. Syntax just means the rules for writing a language, much like we have rules of grammar for writing English.
Let's start with something simple:
var x = 5;
This is a sentence in JavaScript. In English, we end a sentence with a period or maybe a question mark or exclamation point. In JavaScript we end our sentences with a semicolon.
In this sentence, we're creating a variable, x
. A variable is just a container for holding something. In this case, x
is holding the number 5
.
We can do math with JavaScript like this:
var x = 5; var y = 2; var z = x + y;
Just like algebra, our variable z
is now holding the value of the number 7
for us.
But variables can hold things other than numbers. For example:
var text = 'A short phrase';
Here, we've named our variable text
and it's holding some alphabetical characters for us. This is called a string. A string is a set of alpha-numeric characters.
Objects might be the hardest thing for a newcomer to JavaScript to grasp, but that's often because we overthink it, convinced it has to be more complicated than it actually is.
An object is just what it sounds like—a thing, anything. Just like a car, a dog, or a coffee maker are objects.
Objects have properties and methods. A property is a characteristic of an object. For example—a dog could be tall or short, have pointy ears or floppy ears, and be brown or black, or white. All of these are properties of a dog. A method is something an object can do. For example a dog can run, bark, walk, and eat.
Let's take my dog, Magdelena von Barkington, as an example to see how we would deal with objects, properties, and methods in JavaScript:
var dog = Magdelena von Barkington;
Here I've created a variable dog
that I'm using as a container to hold my dog, mostly because I don't want to have to type out her full name each time I refer to her in my code. Now let's say I wanted to get my dog's color:
var color = dog.color;
I created a container called color
and I'm using it to hold my dog's color property—color
is now equal to my dog's color.
Now, I've trained my dog very well, and I would like her to roll over. Here's how I would tell her to roll over with JavaScript:
dog.rollOver();
rollOver
is a method—something that my dog can do. After my dog rolls over, I might like to reward her with a treat. Here's how my dog eats a treat with JavaScript:
dog.eat('bacon');
Wait, what's going on here? Let's take it one step at a time. We have dog, which we know is a container for my dog, Magdelena von Barkington. We have the eat
method, which we know is something that my dog can do. But my dog can't just eat—she has to eat something. We use the parentheses to say what it is that she is eating. In this case, my lucky dog is eating bacon. In JavaScript speak, we would say we were passing bacon to the eat method of the dog.
So you see, objects aren't so difficult—they're just things. Properties are like adjectives—they describe traits or characteristics of an object. Methods are like verbs—they describe actions that an object can do.
A function is a bit of reusable code that tells JavaScript to do something. For example:
function saySomething() { alert('Something!'); }
This function tells JavaScript to pop up an alert box that says Something!
. We always start a function with the word function
then we name our function. That's followed by a set of parentheses and a set of curly brackets. The lines of instruction go inside the curly brackets.
Now, my saySomething
function won't actually do anything until it's called, so I need to add a line of code to call my function:
function saySomething() { alert('Something!'); } saySomething();
Tip
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.
You might wonder what those parentheses are for. Remember how we could pass things to a method by including them in parentheses?
dog.eat('bacon');
In this case, we passed bacon to say what the dog was eating. We can do much the same thing for functions. In fact, methods actually are functions—they're just functions specialized for describing what an object can do. Let's look at how we modify our saySomething
function so that we can pass text to it:
function saySomething(text) { alert(text); } saySomething('Hello there!');
In this case, when I wrote the saySomething
function, I just left a generic container in place. This is called a parameter—we'd say the saySomething
function takes a text parameter, since I've called my parameter text
. I chose the name text
because it's a short and handy descriptor of what we're passing in. We can pass in any bit of text to this function, so text
is an appropriate name. You can name your parameter anything you'd like—but you'll make your code easier to read and understand if you apply some commonsense rules when you're selecting names for your parameters. A parameter behaves very much like a variable—it's just a container for something.