News Outline Schedule Tutorials Project Tools Links

The various types of operations which can be used in JavaScript are outlined here:

1: Arithmetic Operations

The purpose of each of these is to perform some basic mathematical calculation on some given numbers. You would use any of these to manipulate the values in variables that contain numbers. With the exception of the addition operators, you can not use these on anything other than numbers, and trying to do so will cause an error.

c = a + bA plus B goes into C
c = a - bA minus B goes into C
c = a * bA times B goes into C
c = a / bA divided by B goes into C (NOT INTEGER DIVISION!)
c = a % bA modulus B goes into C
(the modulus is the remainder after integer division)
c += aC plus A goes into C
c -= aC minus A goes into C
c *= aC times A goes into C
c /= aC divided by A goes into C (NOT INTEGER DIVISION)
c %= aC modulus A goes into C
(the modulus is the remainder after integer division)
c++Increase C by 1
c--Reduce C by 1

2: Operators for Strings, and anything that is not a number

There are only two real operators that can be used directly on string values, the + and += operators. This is because the + is a special operator in Javascript, and it behaves differently from all of the others, following one simple rule:

If two things are added together with +, and either of them is not a number, both will be converted into strings, and the results will be concatenated.

For example, in the second tutorial, we added numbers and strings together using the +, and the result was a string that looked like the first item, followed by the second. The same thing can be done with the += operator, which can simply add more text to the end of an existing string for you. This means that if you have a long message that you need to put into a single string, you can do it in steps, to make your code easier to read. After all, which of the following two statements would be simpler to read and edit later on?

var st1 = "This is a long string full of statements about how long and full of statements this string is.  It has multiple sentances in it, and they are all very long and drawn out, so they take up a lot of space on the screen."; var st2 = "This is a long string full of statements about how long"; st2 += " and full of statements this string is.  It has multiple"; st2 += " sentences in it, and they are all very long and drawn out,"; st2 += " so they take up a lot of space on the screen.";

3: Relational Operations

The comparisons done in if statements use Relational Operators to compare things to each other. Up until now, we've only used one specific Relational Operator, the ==, or "double equals", which compares two values and gives back a value of true if the two values are the same, or false otherwise. Several other Relational Operators exist, which can also be used to compare values to each other. These operators are used to compare between two possible values to determine if they are the same, or if one is greater or less than the other. Make sure you are aware of the differences between < and <= type operators!

a == bA is Equal To B
a != bA is Not Equal To B
a < bA is Less Than B (numerically)
a <= bA is Less Than OR Equal To B (numerically)
a > bA is Greater Than B (numerically)
a >= bA is Greater Than OR Equal To B (numerically)

Relational operations are typically used in comparisons for if statements and loops, which will be discussed more in the next tutorial. Assigning the result of a relational operation to a variable will result in the variable holding a special value of either true or false, which, like the NaN error code, should not be confused with strings, as there are no quotation marks involved. This means that you can also do a comparison ahead of time, and store whether it was true or false in a variable, and refer to that variable later, like this:

var theTest = a == b; if (theTest) document.write("It was true!"); if (a != b) document.write("It was not true!"); if (a < b) document.write("A was less than B!");

Below are examples of these comparisons applied directly to number values:

1 == 5 false 1 != 5 true
5 == 5 true 5 != 5 false
5 == 1 false 5 != 1 true
1 <  5 true 1 <= 5 true
5 <  5 false 5 <= 5 true
5 <  1 false 5 <= 1 false
1 >  5 false 1 >= 5 false
5 >  5 false 5 >= 5 true
5 >  1 true 5 >= 1 true

Remember that all of these comparisons can also be applied to string values. In the case of strings, rather than deciding which is greater based on numerical order, the comparison for > and < is decided based on alphabetical order, meaning that "a" < "z", but "aa" > "a". Also, since Javascript regards capital and lower case letters as being different, "A" < "a" is also true.

4: Logical Operators

These operators are used to join other comparisons (which would use either Relational Operators, or other Logical Operators) into single comparisons.

&&two ampersands make the AND operator, which returns true if BOTH conditions are true of A && B.
||two vertical bar characters make the OR operator, which returns true if EITHER condition is true of A || B.
!a single exclamation mark is the NOT operator, which returns true if the condition is false of !A

Here are examples of each being used in combination with other conditions in if statements, and explanations of each:

if ( (a == b) && (b == c) ) { window.alert("Wow!"); };

Here, we check to see if variable A and B are the same, AND if B and C are the same - if both of those are true, then the condition for the if statement is true, and the "Wow!" alert will appear. Otherwise it is false, and nothing will happen.

if ( (a == b) || (b == c) ) { window.alert("Wow!"); };

Here, we check to see if variable A and B are the same, OR if B and C are the same - if either of these are true, or if both are true, then the condition for the if statement is true, and the "Wow!" alert will appear. It will only be false if both statements are false.

if ( !(a == b) ) { window.alert("Wow!"); }; if (a != b) { window.alert("Also Wow!"); };

Here are two statements that are actually identical - in the first, the condition (a == b) has a ! in front of it, which means it is only true if (a == b) would have returned false. This is the same as saying (a != b), which is true if A is not equal to B. The ! operator is mostly useful in large compound statements such as the one below:

if ( ( (a == b) && (b == d) ) || !( (a < b) && (c >= d) ) ) { window.alert("What?"); };

Each time one comparison is bound to another with the && or ||, or negated by the !, it is a good idea to wrap a set of brackets around it, to prevent confusion regarding what is being compared to what.