Getting Good with JavaScript - Andrew Burgess [7]
We've looked at assigning with the = operator; but there are a few others that I think are taught best by demonstration:
Example 2.13
var num = 10;
num += 5; // same as num = num + 5
alert(num); // 15
num -= 3; // same as num = num - 3
alert(num); // 12
num *= 2; // same as num = num * 2
alert(num); // 24
num /= 6; // same as num = num / 6
alert(num); // 4
num %= 3; // same as num = num % 3
alert(num); // 1
As you can see, these assignment operators are used to perform an operation on a value already in a variable and re-assign the new value to the variable. If you run the above code, you should find that num equals 1 after you run them all.
Boolean Operators
Boolean operators are used to test the trueness or falseness of a value. Every value in JavaScript can be evaluated as a Boolean. Some values are considered false and others are considered true; you'll see how this is important later on in this chapter. For now, know that these values are considered false:
false
null
undefined
"" (an empty string)
0
Nan (what you get when you try to add, subtract, etc. a number and another non-number value)
Every other value is considered true; keep in mind, this includes the strings "false", "null", and so on.
Now then, back to Boolean operators. The first is the logical NOT; the logical NOT operator converts the given value to the opposite Boolean value.
Example 2.14
var a = "a string";
alert( !a ); // false
alert( !!a ); // true
Since a string with something in it is a true value, using the logical NOT operator will return false. It's just like saying "NOT true." Notice that using NOT twice gives us true; this is a great trick to use when you want to convert any value to its Boolean value.
While the logical NOT is a unary operator, the logical AND and logical OR operators use two values. Look at the syntax, then we'll discuss them:
Example 2.15
var a = true;
var b = false;
alert( a && b ); // false
alert( a || b ); // true
How does this work? And where do these operators do any good? The first one above (&&) is the logical AND operator; it only returns true if both sides of the operator are true. The logical OR (||) returns true as long as just one of the values are true.
So, where is this useful? Well, soon we'll be looking at loops and conditionals, and that's where you'll use this most. In a conditional statement, you'll want to check for certain conditions. If you need two values to be something specific before proceeding, you'll use the logical AND; if only one needs to be true, logical OR will work fine.
I should note that you can use any expression that returns a value; for example:
Example 2.16
var day = 14, year = 2011;
alert( day > 10 || year === 2011 ); // true
alert( day < 20 && year >= 2010 ); // true
You should also know that if the first side of the operator determines the answer, the second side won't be evaluated. For example, in the use of logical OR above, year === 2011 will never be evaluated, because day > 10 returns true, and OR only requires one to be true. On the other hand, if the first half of a logical AND returns false, the second half won't be evaluated, because both sides need to be true for the whole expression to be true.
This fact—that if the first half of the expression determines the final value, the second isn't tested—is used by some of the JavaScript ninjas to shorten their code a bit. Let's say we want to invoke a function (remember, we'll get to functions soon), but we're not sure if the function exists. We could do this:
funcName && funcName();
Remember, to execute the function, we have to have the parentheses, so the first part of this code doesn't execute the function, it just returns the function. If the function exists, this will evaluate to true, and the evaluation will continue to the second part, which executes the function. This avoids the error that would be thrown if we tried to execute a function that doesn't exist (and, yes, we will talk about errors).
Conditional Statements
Several