Certificate Series
JavaScript Tutorial 13
Operators



Hello!

Let's get the feel for operators. Look over this tutorial to get the feel for operators. There isn't much for you to do in this one so you may want to minimize CodeRunner for now.

Operators allow you to add, subtract, divide, multiply, check inequalities, etc. Operators work with numbers and strings.
Click on any operator to see an explanation and example:

= , + , - , * , / , % , += , -= , *= , /= , %= , == , < , > , <= , >= , - , ++ , --



The variables I use in these examples will be named "x" and "y" and "answer". Every time you see one of these 3 names, realize that it could hold any value that the respective operator allows.

There are two types of operators - unary and binary. Let's start with binary:

Binary operators require 2 values. In the first few of these examples, there are actually 2 operators: an = sign and something else. The equals (=) sign is always evaluated last (more on operator precedence later), and sets the value of one thing to the value of something else. When you see an = sign by itself and then some other operator later, realize that I am setting the value of what comes before the = sign to the value of the operator that comes afterward. Here we go; by the way, did you realize that you just learned the = operator?

=

Assignment (equals). This operator assigns values to variables. It works like this:

answer = x;

answer: = x:
top

+

Addition. It works like this:

answer = x + y;

Try this:
String Add (Try typing words into last two boxes).
answer: = x: + y:
JavaScript we used for this example.

top

Numerical Add (try typing numbers in the last two boxes):
answer: = x: + y:
JavaScript we used for this example.

top Easy huh?

-

Subtraction. This operator can only be used with numbers or variables of type number. It works like this:

answer = x - y;

Numerical Subtract
answer: = x: - y:
JavaScript we used for this example.

top

*

Multiplication. (YES, that is an asterisks)This operator can only be used with numbers or variables of type number. It works like this:

answer = x * y;

Numerical Multiply
answer: = x: * y:
JavaScript we used for this example.

top

/

Division. This operator can only be used with numbers or variables of type number. It works like this:

answer = x / y;

Numerical Divide
answer: = x: / y:
JavaScript we used for this example.

top

%

Modulus Operator. When you do long division, you often end up with a remainder . That's what this operator does. This operator can only be used with numbers or variables of type number. It works like this:

answer = x % y;

So, if you did "answer = 13 % 5", answer would equal '3'.

answer: = x: % y:
JavaScript we used for this example.

top

+=

Short-hand Addition. It works like this:

x += y;

This operator does the same thing as "x = x + y". It reads like this in English: "x becomes x plus y" . You can use this operator to concatenate (combine) 2 strings, or to add one number to another.

answer: += x:
JavaScript we used for this example.

top

-=

Short-hand Subtraction. This operator can only be used with numbers or variables of type number. It works like this:

x -= y;

This operator does the same thing as "x = x - y". It reads like this in English: "x becomes x minus y".

answer: -= x:
JavaScript we used for this example.

top

*=

Short-hand Multiplication. This operator can only be used with numbers or variables of type number. It works like this:

x *= y;

This operator does the same thing as "x = x * y". It reads like this in English: "x becomes x Times y".

answer: *= x:
JavaScript we used for this example.

top

/=

Short-Hand Division. This operator can only be used with numbers or variables of type number. It works like this:

x /= y;

This operator does the same thing as "x = x / y". It reads like this in English: "x becomes x divided by y".

answer: /= x:
JavaScript we used for this example.

top

%=

Short-hand Modulus. This operator can only be used with numbers or variables of type number. It works like this:

x %= y;

This operator does the same thing as "x = x % y". It reads like this in English: "x becomes x modulus y".

answer: %= x:
JavaScript we used for this example.

top


BINARY CONDITIONAL OPERATORS: The following are examples of Binary conditional operators. The Answer for these operators is always either TRUE or FALSE.


==

Conditional Equality. This operator is used to check if two things are equal....if they are it returns TRUE if not it returns FALSE. This operator is used mainly in if statements which you'll learn later in these tutorials. It works like this:

x == y;

Try either numbers or strings (words) below:

x == y;

Returns:

JavaScript we used for this example.

top


<

Less Than. This operator is used to check if one number is less than another....if they are it returns TRUE if not it returns FALSE. This operator is used mainly in statements which you'll learn later in these tutorials. It works like this:

x < y;

Try numbers only:

x < y;

Returns:

JavaScript we used for this example.

top


>

Greater Than. This operator is used to check if one number is greater than another....if they are it returns TRUE if not it returns FALSE. This operator is used mainly in statements which you'll learn later in these tutorials. It works like this:

x > y;

Try numbers only:

x > y;

Returns:

JavaScript we used for this example.

top


<=

Less Than or Equal. This operator is used to check if one number is less than or equal to another....if they are it returns TRUE if not it returns FALSE. This operator is used mainly in statements which you'll learn later in these tutorials. It works like this:

x <= y;

Try numbers only:

x <= y;

Returns:

JavaScript we used for this example.

top


>=

Greater Than or Equal. This operator is used to check if one number is less than or equal to another....if they are it returns TRUE if not it returns FALSE. This operator is used mainly in statements which you'll learn later in these tutorials. It works like this:

x >= y;

Try numbers only:

x >= y;

Returns:

JavaScript we used for this example.

top


You've just learned some of the common binary operators. Pretty simple huh? Now let's talk about the unary operators. Unary operators only work with one value.



-

Negation. This operator can only be used with numbers or variables of type number. What it does is make a number negative. It works like this:

-x;

answer: -
JavaScript we used for this example.

top

++

Positive Increment. This operator can only be used with numbers or variables of type number. What it does is take a value and add one (1) to it. It works like this:

x ++;

This operator does the same thing as x = x + 1;. In English: "x becomes x + 1".
answer:++
JavaScript we used for this example.

top

--

Negative Increment. This operator can only be used with numbers or variables of type number. What it does is take a value and subtract one (1) from it. It works like this:

answer:--
JavaScript we used for this example.

top

All things considered, operators are pretty much grade school stuff. But, their specific implementation in JavaScript is something that you need know before you can go on. If you need to, reread the operator explanations until you think you are familiar with them. Before long, they'll come naturally to you.
= x: + y:

">