In the Lesson 4, we discussed JavaScript Variables. In this lesson, we will examine Operators. Is there a relationship between the two? Yes. Operators work with operands (which are variables sometimes). For example, x = y +z. Here the ‘+’ sign is an operator while y and z are operands.
So we would consider 5 JavaScript operators:
- Arithmetic Operators
- Comparison Operators
- Logical Operators (Relational Operators)
- Bitwise Operators
- Assignment Operators
- Conditional Operator
- The typeof Operator
1. Arithmetic Operators
You use arithmetic operators to perform calculations. There are 7 of them listed in the table below.
The operations assume x = 10 and y = 20
Sr.No. | Operator & Description |
---|---|
1 | + (Addition)
Adds the two operands Example: x + y will give 30 |
2 | – (Subtraction)
Subtracts the second operand, x from the first one, y Example: x – y will give -10 |
3 | * (Multiplication)
Multiply the two operands Example: x * y will give 200 |
4 | / (Division)
Divide the numerator x by the denominator y Example: y / x will give 2 |
5 | % (Modulus)
Returns the remainder of an integer division Example: x% y will give 0 |
6 | ++ (Increment)
Takes only one operand. Increases an integer value of the operand by one. Example: x++ will give 11 |
7 | — (Decrement)
Takes only one operand. Decreases an integer value by one Example: x– will give 9 |
While the addition operator works with numbers, it can also work with strings. Hence you can add two strings together.
For example “name” + 20 will give name 20.
In this way, the ‘+’ is called a ‘concatenation’ operator. It concatenates or combines two strings. The would be discussed more in later chapters.
The html file below shows how arithmetic operators work.
<html> <head><title>Arithmetic Operators Example</title></head> <body> <script type = "text/javascript"> <!-- var x = 33; var y = 10; var z = "Test"; var linebreak = "<br />"; document.write("x + y = "); result = x + y; document.write(result); document.write(linebreak); document.write("x - y = "); result = x - y; document.write(result); document.write(linebreak); document.write("x / y = "); result = x / y; document.write(result); document.write(linebreak); document.write("x % y = "); result = x % y; document.write(result); document.write(linebreak); document.write("x + y + z = "); result = x + y + x; document.write(result); document.write(linebreak); x = ++x; document.write("++x = "); result = ++x; document.write(result); document.write(linebreak); y = --y; document.write("--y = "); result = --y; document.write(result); document.write(linebreak); //--> </script> You can try different values for x and y </body> </html>
If you view this file, the output would be as shown below
x + y = 43 x - y = 23 x / y = 3.3 x % y = 3 x + y + z = 76 ++x = 35 --y = 8 You can try different values for x and y
2. Comparison Operators
You use comparison operators to compare a value against another value. The table below gives a list of comparison operators in JavaScript.
We assume x = 10 and y = 20
SNo. | Operator and brief description |
---|---|
1 | = = (Equal)
Checks if the value of two operands are equal , if they are, then the condition becomes true. Otherwise, false Example: (x == y) is not true. |
2 | != (Not Equal)
Checks if the value of two operands are equal not equal, if the values are not equal, then the condition becomes true. Example: (x != y) is true. |
3 | > (Greater than)
Checks if the value of the left operand is greater than the value of the right operand, if true, then the condition becomes true. Otherwise the condition becomes false. Example: (x > y) is not true. |
4 | < (Less than)
Checks if the value of the left operand is less than the value of the right operand, if true, then the condition becomes true. Example: (x < y) is true. |
5 | >= (Greater than or Equal to)
Checks if the value of the left operand is greater than or equal to the value of the right operand, if true, then the condition becomes true. Otherwise, false Example: (x >= y) is not true. |
6 | <= (Less than or Equal to)
Checks if the value of the left operand is less than or equal to the value of the right, if they are equal, then the condition becomes true. Example: (x <= y) is true. |
The html file below shows how the comparison operator works. I recommend you take time to examine it. Also watch the video lesson for clarification.
<html> <head><title>Comparison Operators Example</title></head> <body> <script type = "text/javascript"> <!-- var x = 10; var y = 20; var linebreak = "<br />"; document.write("(x == y) => "); result = (x == y); document.write(result); document.write(linebreak); document.write("(x < y) => "); result = (x < y); document.write(result); document.write(linebreak); document.write("(x > y) => "); result = (x > y); document.write(result); document.write(linebreak); document.write("(x != y) => "); result = (x != y); document.write(result); document.write(linebreak); document.write("(x >= y) => "); result = (x >= y); document.write(result); document.write(linebreak); document.write("(x <= y) => "); result = (x <= y); document.write(result); document.write(linebreak); //--> </script> You can try different values for x and y </body> </html>
If you view this file in the browser, then the output would be:
(x == y) => false (x < y) => true (x > y) => false (x != y) => true (x >= y) => false (x <= y) => true You can try different values for x and y
3. Logical Operators (Relational Operators)
Logical operator are used for performing logical operations. Let’s take some examples to illustrate this.
Assuming x = 10 and y = 20, then the table below holds
SNo. | Operator and brief description |
---|---|
1 | && (Logical AND)
If both of the operands are non-zero, then the condition holds true. Otherwise false Example: (x && y) is true. |
2 | || (Logical OR)
If any of the two operands are non-zero, then the condition holds true. Otherwise false Example: (x || y) is true. |
3 | ! (Logical NOT)
It takes only one operand. Reverses the logical state of the operand. If a condition is true, then the Logical NOT operator will make it false. Example: ! (x && y) is false. |
Now you can run the code below to see how logical operations work. I also recommend you change up the values of x and y and see what happens.
<html> <head><title>Logical Operators Example</title></head> <body> <script type = "text/javascript"> <!-- var x = true; var y = false; var linebreak = "<br />"; document.write("(x && y) => "); result = (x && y); document.write(result); document.write(linebreak); document.write("(x || y) => "); result = (x || y); document.write(result); document.write(linebreak); document.write("!(x && y) => "); result = (!(x && y)); document.write(result); document.write(linebreak); //--> </script> You can try different values for x and y </body> </html>
If you view this html file, then you will have the output below:
(x && y) => false (x || y) => true !(x && y) => true You can try different values for x and y
4. Bitwise Operators
Bitwise operators are used to perform certain operations on the individual bits of the binary equivalent of the operator. To understand how it works, you need to have an idea of binary numbers.
The table below shows how bitwise operators work.
We assume x = 2 and y = 3
SNo. | Operator and brief description |
---|---|
1 | & (Bitwise AND)
It performs a Boolean AND operation on each bit of its integer arguments. Example: (x & y) is 2. |
2 | | (BitWise OR)
It performs a Boolean OR operation on each of the bit of its integer operand. Example: (x | y) is 3. |
3 | ^ (Bitwise XOR)
It performs a Boolean exclusive OR (XOR) operation on each bit of the given integer arguments. Exclusive OR means that either of the operand, just one is true or operand two is true, but not both. 101 ^ 111 = 010 (it’s 1 only for the middle bit where) Example: (x ^ y) is 1. |
4 | ~ (Bitwise Not)
It is a unary(works on a single operand) operator and operates by reversing all its bits in the operand. Example: (~y) is -4. |
5 | << (Bitwise left Shift)
It moves all the bits in its first operand to the left by the number of places given in the second operand. New bit spaces are filled with zeros. Shifting a value left by one position is equivalent to multiplying by 2, shifting two positions is equivalent to multiplying by 4. And so one. Example: (y << 1) is 4. |
6 | >> (Bitwise right Shift)
Binary Right Shift Operator. The left operand’s value is moved right by the number of bits specified by the right operand. Example: (x >> 1) is 1. |
7 | >>> (Bitwise right shift with Zero)
This operator is just like the >> operator, except that the bits shifted in on the left are always zero. Example: (x >>> 1) is 1. |
The html file below illustrates how the bitwise operator work
<html> <head><title>Bitwise Operators Example</title></head> <body> <script type = "text/javascript"> <!-- var x = 2; // Binary equivalent of 10 var y = 3; // Binary equivalent of 11 var linebreak = "<br />"; document.write("(x & y) => "); result = (x & y); document.write(result); document.write(linebreak); document.write("(x | y) => "); result = (x | y); document.write(result); document.write(linebreak); document.write("(x ^ y) => "); result = (x ^ y); document.write(result); document.write(linebreak); document.write("(~y) => "); result = (~y); document.write(result); document.write(linebreak); document.write("(x << y) => "); result = (x << b); document.write(result); document.write(linebreak); document.write("(a >> y) => "); result = (x >> y); document.write(result); document.write(linebreak); //--> </script> You can try different values for x and y </body> </html>
If you view the html file above, you will have the output below:
(x & y) => 2 (x | y) => 3 (x ^ y) => 1 (~y) => -4 (x << y) => You can try different values for x and y
5. Assignment Operators
You probably have used some assignment operators before. The assignment operators support in JavaScript are given below:
Sr.No. | Operator & Description |
---|---|
1 | = (Simple Assignment )
Assigns values from the right side operand to the left side operand Example: z = x + y will assign the value of x + y into z |
2 | += (Add and Assignment)
It adds the right operand to the left operand and then assigns the result to the left operand. Example: z += x is equivalent to z = z + x |
3 | −= (Subtract and Assignment)
It subtracts the right operand from the left operand and then assigns the result to the left operand. Example: z -= x is equivalent to z = z – x |
4 | *= (Multiply and Assignment)
It multiplies the right operand with the left operand and then assigns the result to the left operand. Example: z *= x is equivalent to z = z * x |
5 | /= (Divide and Assignment)
It divides the left operand with the right operand and then assigns the result to the left operand. Example: z /= x is equivalent to z = z / x |
6 | %= (Modules and Assignment)
It takes modulus using two operands and assigns the result to the left operand. Example: z %= x is equivalent to z = z % x |
The html file below illustrates these assignment operators discussed. I recommend you view it and also change the values to see what you get.
<html> <head><title>Assignment Operators Example</title></head> <body> <script type = "text/javascript"> <!-- var x = 33; var y = 10; var linebreak = "<br />"; document.write("Value of x => (x = y) => "); result = (x = y); document.write(result); document.write(linebreak); document.write("Value of x => (x += y) => "); result = (x += y); document.write(result); document.write(linebreak); document.write("Value of x => (x -= y) => "); result = (x -= y); document.write(result); document.write(linebreak); document.write("Value of x => (x *= y) => "); result = (x *= y); document.write(result); document.write(linebreak); document.write("Value of x => (x /= y) => "); result = (x /= y); document.write(result); document.write(linebreak); document.write("Value of x => (x %= y) => "); result = (x %= y); document.write(result); document.write(linebreak); //--> </script> You can try different values for x and y </body> </html>
If you view the file above, then you will have the output.
Value of x => (x = y) => 10 Value of x => (x += y) => 20 Value of x => (x -= y) => 10 Value of x => (x *= y) => 100 Value of x => (x /= y) => 10 Value of x => (x %= y) => 0 You can try different values for x and y
6. Conditional Operator (?:)
The is operation applies to an expression. It first evaluates the expression for either true or false. Then it executes one of the two specified statements depending on the result of the initial evaluation.
The syntax of the contitional operator is:
(condition) ? x : y
If condition is true. the value x: otherwise value y
Let’s take an example.
The code below illustrates the use of conditional operator.
<html> <head><title>Conditional Operators Example</title></head> <body> <script type = "text/javascript"> <!-- var x = 10; var y = 20; var linebreak = "<br />"; document.write ("((ax > y) ? 100 : 200) => "); result = (x > y) ? 100 : 200; document.write(result); document.write(linebreak); document.write ("((x < y) ? 100 : 200) => "); result = (x < y) ? 100 : 200; document.write(result); document.write(linebreak); //--> </script> You can try different values for x and y </body> </html>
If you view the file, you will have the output below:
((ax > y) ? 100 : 200) => 200 ((x < y) ? 100 : 200) => 100 You can try different values for x and y
7. The typeof Operator
This operator works on a single operand (it is a unary operator).
The typeof operator returns the data type of the specified operand. So it could return number, string or boolean depending on the type of its operand.
The table below summarizes the values that can be returned by the typeof operator
Type | Value Returned by typeof |
---|---|
Number data | “number” |
String data | “string” |
Boolean data | “boolean” |
Object operand | “object” |
Function operand | “function” |
Undefined | “undefined” |
Null | “object” |
The code below illustrates how the typeof operator works. I recommend you make sure to try it yourself.
<html> <head><title>Typeof Operators Example</title></head> <body> <script type = "text/javascript"> <!-- var x = 10; var y = "String"; var linebreak = "<br />"; output = (typeof y == "string" ? "Y is String" : "Y is Numeric"); document.write("Output => "); document.write(output); document.write(linebreak); output = (typeof x == "string" ? "X is String" : "X is Numeric"); document.write("Output => "); document.write(output); document.write(linebreak); //--> </script> You can try different values for x and y </body> </html>
If you view the file, you will have the output below
Output => Y is String Output => X is Numeric You can try different values for x and y
Note: Ensure you try out all the examples yourself.
One thought on “JavaScript – Operators”