Online Book Reader

Home Category

Getting Good with JavaScript - Andrew Burgess [6]

By Root 227 0
It returns the remainder or the division of the two numbers. The result for the example above is 1, because 31 / 2 is 15, with remainder 1.

Comparison Operators

Besides acting on values, you'll want to compare them. You'll be familiar with some of these:

Example 2.7


alert( 10 < 5 ); // false

alert( 10 > 5 ); // true

alert( 4 <= 3 ); // false

alert( 4 >= 3 ); // true


It's not hard at all figure which number is greater, or lesser. Besides "less-than" and "greater-than", we've got "less-than-or-equal-to" and "greater-than-or-equal-to." All of these operators return boolean values.

You can use these operators on strings as well:

Example 2.8


alert( "cat" > "dog" ); // false

alert( "bacon" > "chunks" ); // false

alert( "cats" >= "cats" ); // true

alert( "fishing" <= "sleeping" ); // true

alert( "cat" > "CAT" ); // true


How does this work? Well, each letter has a character code, a number that identifies it. It's the numbers that are compared. It's important to note that capital letters don't line up beside their lowercase children. All the capital letters come before the lowercase ones, so "A" < "z" is true.

And it's not just letters that have character codes; every character you can type, including ones you can't (like Shift, Escape, and the others in the modifier gang), has a character code. So a string with numbers or punctuation can be compared this way.

How about just checking to see if two values are equal? Don't make the mistake of doing this:

var a = "cat",

b = "dog";

a = b // WRONG: DOES NOT COMPARE


Look at this for a second; can you see why you can't use = to compare values? That's the assignment operator; you use a single equal-sign to assign a value to variable. To test for equality, you can use three equal-signs (often called the triple-equals operator).

Example 2.9


var a = "cat",

b = "dog";

console.log( a === b ); // false

console.log( a === "cat" ); // true

ROCKSTAR TIP

You might be thinking, "What about two equal-signs? Isn't it dumb to jump from one to three?" Well, there actually is a double-equal operator. Usually, you won't want to use it, because it tries to change the type of your variables. For example, 1 === '1' is false, but 1 == '1' is true, because the double-equal operator will convert the string "1" to the number 1 (We'll talk about converting values soon).

Unary Operators

All the operators we've looked as so far work with two values, one on each side. There are several operators that only work with one value. First off, we've got the incrementing and decrementing operators:

Example 2.10


var i = 0;

i++;

alert(i); // 1

++i;

alert(i); // 2

i--;

alert(i); // 1

--i;

alert(i); // 0


Putting "plus-plus" before or after a number will add one to the number. Predictably, "minus-minus" will subtract one from the number. So, what's the difference between the prefix version and the postfix version? It's this: these operators return a value too, just like the other ones. In this case, they return the value of the number they are incrementing; however, the prefix (that the operator on the front) operators increment the number before returning the value, which the postfix operators return the value, then increment the number. Here's an example:

Example 2.11


var num1 = 1, num2, num3;

num2 = ++num1;

console.log("num1: ", num1); // num1: 2

console.log("num2: ", num2); // num2: 2

num3 = num1++;

console.log("num1: ", num1); // num1: 3

console.log("num3: ", num2); // num3: 2


Another useful unary operator is typeof; this operator will return the type of the given variable:

Example 2.12


alert( typeof 10 ); // "number"

alert( typeof "test" ); // "string"

alert( typeof new Date() ); // "object"


The only catch here is that arrays aren't given this type of array; they are called objects, because—technically—they are. But that's not helpful.

There are a few other unary operators, but we'll see them in a while. Right now, let's talk about our last two groups of operators: extra assignment operators and Boolean operators.

Assignment

Return Main Page Previous Page Next Page

®Online Book Reader