Operators

by Nick Jensen on October 14, 2013

Hello everyone. It is I, Nick and we are here to continue our adventure through JavaScript. Follow along with me as I digest Chapter Five of John Pollock’s JavaScript: A Beginner’s Guide. The learning concepts within Chapter Five are much simpler than those of Chapter Four, which means that I won’t have to cover this chapter in two separate blogs. For those that remember, we last covered functions. Today, we are moving on to operators. Now, first of all, what is an operator? According to Pollock, operators are the workhorses within scripts: they perform calculations, make comparisons and assign values to a variable. There are many types of operators: arithmetic, assignment, comparison, and more. Let’s take a look at some of them, shall we?

First, we have arithmetic operators. These specific operators can add, subtract, multiply, and divide values within one or more variables. Here are a couple of examples:

Addition Operator:

var numb1 = 2,

numb2 = 5;

var thesum = numb1 + numb2;

window.alert(thesum);

On a webpage, this script would show up as a pop-up message with the number 7. Of course, the example that I have just showed you is but a tiny sample of what arithmetic operators can do. If I substituted the plus sign with a minus sign, I would get -3 instead of 7. Feel free to experiment on your own using multiplication or division. Once you learn the basics, arithmetic operators becomes a breeze. The next on our list is assignment operators, which everyone should already be familiar with since I have been using them in almost every line of script. Assignment operators connect a value (or values) to a variable. You’ll recognize them as the equal signs (=) I’ve been using in the last few blogs. Assignment variables come in other forms as well. Allow me to show you some examples of various assignment operators.

Add-and Assign Operator:

var numb = 500

numb += 5

The new variable is now 505. The add-and-assign operator saves me the trouble of having to repeat my value in the code:

var numb = 500

numb = numb + 5

If I used a minus instead of plus sign, the new value would equal 495. Now here’s an example of a multiply-and-assign variable:

var numb = 500

var multby = 3

numb *= multby

By multiplying the variable by 3, I get a value of 1500 for my “numb” variable. If I replaced the “*=” with “/=” (thereby dividing the value), the new value for numb would equal “166.6”.

The last type of assignment operator is modulus-and-assign. This is kind of a tricky one to explain. The operator itself divides the variable to the left of the operator by the value on the right side of the operator (Forgive me for unleashing that salvo of prepositions). After that, it assigns the remainder to the new value. Perhaps it would be best if I just showed you instead.

var numb = 500

var div = 10

numb %= div

I have just programmed the script to divide 500 by 10. Since 500 divided by 10 equals 50 with a remainder of 0, the new value of numb becomes 0.

The next item on our list is the comparison operator. According to Pollock, conditional operators are assigned to “conditional statements and loops in order to perform actions[,but] only when a certain condition is met.” Unlike the previous operators, conditional operators can return a true or false value. For instance, the following script would be assigned as true

var numb1 = 2,

numb2 = 2,

numb3 = 5;

numb1 == numb2

However, if I wrote “numb1 == numb 3”, the script would be assigned as false. There are many other examples of conditional statements¬† and operators (much too many to fit in this blog), so if you want to learn more about operators or JavaScript in general, I suggest you find a copy of it. Now, that you and I have gotten a taste of Chapter Six, it’s time I move on to Chapter Seven. Join me next time when I discuss conditional statements and loops!

{ 0 comments… add one now }

Leave a Comment

Previous post:

Next post: