Servage Magazine

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

Advanced JavaScript Development Part 3

Sunday, December 1st, 2013 by Servage

We can also assign a variable a “true” or “false” value. This is called a Boolean value, and it is the lynchpin for all manner of advanced logic. Boolean values use the true and false keywords built into JavaScript, so quotation marks are not necessary.

var foo = true; // The variable "foo" is now true

Just as with numbers, if we were to wrap the value above in quotation marks, we’d be saving the word “true” to our variable instead of the inherent value of “true” (i.e., “not false”).

In a sense, everything in JavaScript has either an inherently “true” or “false” value. “null”, “undefined”, “0”, and empty strings (“”) are all inherently false, while every other value is inherently true. These values, although not identical to the Booleans “true” and “false”, are commonly referred to as being “truthy” and “falsy.” I promise I didn’t make that up.
An array is a group of multiple values (called members) that can be assigned to a single variable. The values in an array are said to be indexed, meaning you can refer to them by number according to the order in which they appear in the list. The first member is given the index number 0, the second is 1, and so on, which is why one almost invariably hears us nerds start counting things at zero—because that’s how JavaScript counts things, and many other programming languages do the same. We can avoid a lot of future coding headaches by keeping this in mind.

So, let’s say our script needs all of the variables we defined earlier. We could define them three times and name them something like foo1, foo2, and so on, or we can store them in an array, indicated by square brackets ([ ]).

var foo = [5, "five", "5"];

Now anytime you need to access any of those values, you can grab them from the single foo array by referencing their index number:

alert( foo[0] ); // Alerts "5"
alert( foo[1] ); // Alerts "five"
alert( foo[2] ); // Also alerts "5"
 Comparison operators
 Now that we know how to save values to variables and arrays, the next logical step is knowing how to compare those values. There is a set of special characters called comparison operators that evaluate and compare values in different ways:
== Is equal to
!= Is not equal to
=== Is identical to (equal to and of the same data type)
!== Is not identical to
> Is greater than
>= Is greater than or equal to
< Is less than
<= Is less than or equal to

There’s a reason all of these definitions read as parts of a statement. In comparing values, we’re making an assertion, and the goal is to obtain a result that is either inherently true or inherently false. When we compare two values, JavaScript evaluates the statement and gives us back a Boolean value depending on whether the statement is true or false.

alert( 5 == 5 ); // This will alert "true"
alert( 5 != 6 ); // This will alert "true"
alert( 5 < 1 ); // This will alert "false"
Advanced JavaScript Development Part 3, 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.