Getting Good with JavaScript - Andrew Burgess [5]
There are several parameters you can use to create other dates. You can pass in a string date:
new Date("January 17, 2012")
Or, you can pass a list of parameters, corresponding to the folowing list: year, month, day, hour, minute, second, millisecond. You don't have to add them all; only the ones that matter. It's important to note that the month number is zero-based. This means that to get January, you use the number 0 and to get December, you use 11.
new Date(2009, 3, 20, 17, 30)
This will return April 20, 2009 at 5:30:00.0 PM. Your date object will also include time zone information, based on the settings on your computer.
Semicolons
You may have notices the semicolons popping up here are there in the bits of code we've looked at so far. These are important in JavaScript; they delimit statements. So, when should you use semicolons? Well, the short and easy answer is to use them at the end of each line. The more accurate answer is to use one after each expression statement. Vague, eh? Explaining how all this works is a bit beyond this book. When you're beginning, it's easiest to learn where to put your semicolons by looking at other people's code. As you take your cues from the code snippets you see in this book, these rules will hold true most of the time:
Any single line that you could put in your Firebug console should have a semicolon at the end.
var someVar = "value"; // semicolon
someFunction("parameter"); // semicolon
Any statement that includes a block (lines of code between curly braces) should not have semicolons at the end of the lines that open and close the block (the lines that usually end with an opening or closing curly brace). if (true) { // no semicolon doThis(); // semicolon } // no semicolon
I know this code might not make sense right now, but just keep these semicolon rules in the back of your mind.
Comments
Here's something you'll definitely find useful: you can add comments to your JavaScript code. To make a single-line comment, use two backslashes. You can do this for a whole line, or just part of one:
// This is a useless comment
var x = 10; // x is now equal to 10
If you want multi-line comments, use a backslash and an asterisk; reverse that to end the comment:
/* file: navigtion.js
author: Andrew Burgess
date: July 2, 2011
purpose: provides animation for the site navigation
*/
Make sure you don't abuse comments. Don't litter your files with them, and make the comments you do use worthwhile.
Operators
Well, now that we know what values we can use, and how to store them, let's talk about operators. Operators are used to work with variables and values; often, they'll change variables, or return values that you'll want to assign to variables. Let's take a look at some of the operators we've got in our toolbox.
Arithmetic Operators
These are probably the most often used operators. They're your standard math operators, good friends of most people since about grade 2.
Example 2.6
var four = 2 + 2, //addition operator
hour = 24 - 13, // subtraction operator
seventy = 7 * 10, // multiplication operator
avg_days_per_week = 365 / 52, // division operator
remainder = 31 % 2, // modulus operator
msg = "the time is " + hour + " o'clock"; // addition operator used on strings
console.log(four); // 4
console.log(hour); // 11
console.log(seventy); // 70
console.log(avg_days_per_week); // 7.019230769230769
console.log(remainder); // 1
console.log(msg); // "the time is 11 o'clock"
Besides the obvious ways these operators work, you can see that we're assigning the value of each operation to a variable. Also, notice that we only need to use one var statement, since each variable assignment is separated by commas. Then, notice that + operator is used to concatenate strings. We can even throw a number in there and JavaScript will convert the it to part of the resulting string.
The one operator above that you might not be familiar with is the modulus (%) operator.