Javascript contains all the arithmetic operators that any average person
who has taken math courses is used to, as well as several they probably are not.
In the table below the seven arithmetic operators are listed along with their
name, an example and then the results of that example so you can see them in
action. Explanations of some of the more complex is given below the table.
Operator |
Name |
Example |
Results |
+ |
Addition |
N = 6
N + 5 |
12 |
- |
Subtraction |
N = 6
N - 5 |
1 |
* |
Multiplication |
N = 6
N * 5 |
30 |
/ |
Division |
20 / 5
7 / 2 |
4
3.5 |
% |
Modulus (remainder) |
20 % 5
7 % 2
11 / 8 |
0
1
3 |
++ |
Increment |
N = 6
N++ |
7 |
-- |
Decrement |
N = 6
N-- |
5 |
Modulus: This term and how it is portrayed may be new to anybody who hasn't
done programming before. Basically it is just the remainder of the equation
after division. In the example of 11 / 8. The remainder would of course be 3,
since 11 goes into 8 one time, with 3 left over.
Increment: Basically it is what it is, going up in increments of one. This is
very commonly used in loops later on when you want to check every single number
in a set of numbers or every character in a string. So if you only want to
increase something by 1, usually the increment is a good choice.
Decrement: This is the same as increment except for you are going down in
increments of one. This is also commonly used in loops when counting down
towards a certain value before the loop is exited. If you want to decrease
something by 1, the decrement would be a good choice.
Assignment Operators
These operators give the left operand a value based on the right operand. An
operand being what is on the left or right of the operator. (e.g. in x = 5, x
would be the left operand and 5 would be the right operand. In assignments in
JavaScript things always work from right to left. It may seem a bit odd as we
are used to working left to right our whole lives out of books. However in
the example of x = 5, it is really saying that 5 is assigned to x. The most
basic assignment operator is =. Below in the table are more of them. Keep in
mind that the right operand is assigned to the left operand.
Operator |
Example |
Same As |
= |
x = y |
x = y |
+= |
x += y |
x = x + y |
-= |
x -= y |
x = x - y |
*= |
x *= y |
x = x * y |
/= |
x /= y |
x = x / y |
%= |
x %= y |
x = x % y |
Comparison Operators
Comparison operators compare the operands and returns a logical value that is
based on whether or not the results of the comparison are true or false. The
operands being compared can be either strings or numbers in value. In a
comparison there are only two different solutions, either true or false.
Operator |
Name |
Example |
== |
is equal to |
2 == 3 (false) |
!= |
is not equal to |
2 != 3 (true) |
> |
is greater than |
2 > 3 (false) |
< |
is less than |
2 < 3 (true) |
>= |
is greater than or equal to |
2 >= 3 (false) |
<= |
is less than or equal to |
2 <= 3 (true) |
Logical Operators
Logical operators are used to combine different expressions to either add or
subtract them to produce results.
Operator |
Name |
Example |
&& |
and |
x = 1
y = 2
x < 2 && y > 1 (true) |
|| |
or |
x = 1
y = 2
x > 2 || y > 1 (true) |
! |
not |
x = 1
y = 2
!( x + y == 3 ) (false) |
With and, you are saying that two equations are BOTH true in
order for something to be true. Only a true && true will result in a true with
an and operator. With an or however, it is only saying that one or the other
equation is true. A true or false would be true, as would a true and true, only
a false and false will give a false result with an or. It is kind of backwards
because or will give you more results, while and will give you less. The not
simply is saying that what is inside, has to be false, for it to be true.
String Operators
These are basically operators that help someone deal with text.
There is one thing to keep in mind when dealing with text and that is the fact
that everything in the quotes is the string. Basically "
hello" is a lot different than "hello". One has a lot more spaces in it. Spaces
within the quotes do matter, any spaces outside the quotes do not matter at all.
To concatenate two different strings together you use the + sign. Examples of
using + to combine strings is shown in the table below.
String Operator (+) |
txt1 = "Emperor's"
txt2 = "Hammer"
txt3 = " Hammer"
str1 = txt1+txt2
str2 = txt1+" "+txt2
str3 = txt1+txt3 |
After you declare the three variables str1, str2 and str3 you can notice above
that each is declared differently.
str1 = Emperor'sHammer. The whole string is bunched together because there
are no spaces in the quotes nor was one added in. This is a common error that
happens especially when going fast.
str2 = Emperor's Hammer. This string has a space in it, as a " " blank space
was added in between the two strings.
str3 = Emperor's Hammer. This is the exact same as str2 except this time the
space was there because it was added before Hammer in the original string.
Just remember that spaces are important when dealing with strings or text and
that the + operator is what is used to combine them.
|