Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Advanced Javascript development – Part 4

Wednesday, December 4th, 2013 by Servage

Equal versus identical
The tricky part is understanding the difference between “equal to” (==) and “identical to” (===). We already learned that all of these values fall under a certain data type. For example, a string of “5” and a number 5 are similar, but they’re not quite the same thing.

Well, that’s exactly what === is meant to check.

alert( "5" == 5 ); // This will alert "true". They're both "5".
alert( "5" === 5 ); // This will alert "false". They're both "5", 

but they’re not the same data type.

alert( “5” !== 5 ); // This will alert “true”, since they’re not the same data type.

Even if you have to read it a couple of times, understanding the preceding sentence means you’ve already begun to adopt the special kind of crazy one needs to be a programmer. Welcome! You’re in good company.
Mathematical Operators
The other type of operator is a mathematical operator, which performs mathematical functions on numeric values. We touched briefly on the straightforward mathematical operators for add (+), subtract (-), multiply (*), and divide (/). There are also some useful shortcuts you should be aware of:

+= Adds the value to itself
++ Increases the value of a number (or a variable containing a number
value) by 1
-- Decreases the value of a number (or a variable containing a number
value) by 1
 If/else statements
 If/else statements are how we get JavaScript to ask itself a true/false question. They are more or less the foundation for all the advanced logic that can be written in JavaScript, and they’re about as simple as programming gets. In fact, they’re almost written in plain English. The structure of a conditional statement is as follows.

if( true ) {

// Do something.


It tells the browser “if this condition is met, then execute the commands listed between the curly braces ({ }).” JavaScript doesn’t care about whitespace in our code, remember, so the spaces on either side of the ( true ) are purely for the sake of more readable code.

Here is a simple example using the array we declared earlier:

var foo = [5, "five", "5"];
if( foo[1] === "five" ) {
alert("This is the word five, written in plain English.");
Since we’re making a comparison, JavaScript is going to give us a value of either “true” or “false”. The highlighted line of code breaks says “true or false: the value of the foo variable with an index of 1 is identical to the word ‘five’?”

In this case, the alert would fire because the foo variable with an index of 1 (the second in the list, if you’ll remember) is identical to “five”. In this case, it is indeed true, and the alert fires.

We can also explicitly check if something is false, by using the != comparison operator that reads as “not equal to.”

if( 1 != 2 ) {
alert("If you're not seeing this, we have bigger problems than
// 1 is never equal to 2, so we should always see this alert.

I’m not much good at math, but near as I can tell, 1 will never be equal to 2. JavaScript says, “That ‘1 is not equal to 2’ line is a true statement, so I’ll run this code.”

If the statement doesn’t evaluate to “true”, the code inside of the curly braces will be skipped over completely:

if( 1 == 2 ) {
alert("If you're seeing this, we have bigger problems than
// 1 is not equal to 2, so this code will never run.
Advanced Javascript development - Part 4, 5.0 out of 5 based on 2 ratings
Categories: Guides & Tutorials


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.