JQuery_ Novice to Ninja - Earle Castledine [153]
var a = 2;
var b = "two";
var c = "2";
alert(typeof a);// alerts "number"
alert(typeof b);// alerts "string"
alert(typeof c);// alerts "string"
No surprises there—the variables are behaving as we expect. Things look different, though, when we ask JavaScript to take its best guess at what type we want the variables to be:
alert(a * a);// alerts 4
alert(a + b);// alerts 2two
alert(a * c);// alerts 4
alert(typeof (a * a));// alerts "number"
alert(typeof (a + b));// alerts "string"
alert(typeof (a * c));// alerts "number"
When we “add” a string and a number using the + operator, JavaScript assumes we’re trying to concatenate the two, so it creates a new string. It would appear to change the number’s variable type to string. When we use the multiplication operator (*) though, JavaScript assumes that we want to treat the two variables as numbers.
The variable itself remains the same throughout, it’s just treated differently. We can always explicitly tell JavaScript how we intend to treat a variable, but if we don’t, we need to understand just what JavaScript is doing for us. Here’s another example:
alert(a + c);// alerts 22
alert(a + parseInt(c));// alerts 4
Equality Operators
The equal sign (=) and its related operators can also provide a trap for young players. And where it was once just a little odd, it became even more so in JavaScript 1.3. The first trick is that the equal sign has a different meaning than what you remember from your school mathematics classes:
var a = 2;
var b = "2";
A single equal sign is an assignment operator, and is used to assign values to variables. We all knew what it did, but now we know what it’s called:
var c = (a == b);
Two = signs together, ==, is known as the equality operator, and establishes a Boolean value. In our example, the variable c will have a value of true, as JavaScript compares the values before and after the equality operator, and considers them to be equal. Using the equality operator, JavaScript pays no heed to the variable’s type, and attempts to coerce the values to assess them.
Switch out the first equal sign for an exclamation mark, and you have yourself an inequality operator (!=). This operator will return false if the variables are equal, or true if they aren’t:
var d = (a != b);
The variable d will now have a value of false, since a and b are equal. It may be a little complex, but at least it’s consistent.
In JavaScript 1.3, the situation became less simple still, with the introduction of one further operator: the strict equality operator, shown as ===.
var e = (a === b);
The strict equality operator differs from the equality operator, in that it pays strict attention to type as well as value when it assigns its Boolean. In the above case, d is set to false: while a and b both have a value of 2, they have different types.
And as you might have guessed, where the inequality operator was paired with the equality operator, the strict equality operator has a corresponding strict inequality operator:
var f = (a !== b);
In this case the variable f will return true, as we know the two compared variables are of different types, though similar values.
Suffice it to say that some equal signs are more equal then others!
Truthiness and Falsiness
JavaScript’s Boolean type has two possible values—true and false:
var jQueryIsFun = true;
var javaScriptIsJava = false;
But we know that JavaScript likes to be trickier than this. In reality, there are a multitude of ways that variables can evaluate to true or false. These values are referred to as being truthy or falsy. So when we write if(variable) { … }, variable need not be a Boolean value: the code between the brackets will run if variable contains the number 5, or the string "Hello World!",