The JavaScript Operators - JavaScript: Academy: The Stress Free Way to Learning JavaScript Inside & Out (2016)

JavaScript: Academy: The Stress Free Way to Learning JavaScript Inside & Out (2016)

Chapter 5. The JavaScript Operators

The JavaScript language supports the following operator types:

· Logical Operators

· Comparison Operators

· Assignment Operators

· Arithmetic Operators

· Bitwise Operators

· Miscellaneous Operators

Let’s discuss each type of operator:

The Logical Operators

JavaScript supports these logical operators:

Note: Let’s assume that x = 5 and that y = 10.

· “&&” – This is known as the Logical AND operator. If both operands are not equal to zero, the condition is true. For example: (x && y) is true.

· “||” – This operator is called Logical OR. If at least one of the operands is not equal to zero, the condition is true. For example: (x || y) is true.

· “!” – Programmers refer to this operator as Logical NOT. This operator reverses the result of the operator it is used on. For instance: ! (x && y) is false.

The Comparison Operators

Here are the comparison operators that you can use in JavaScript:

Note: Let’s use the following variables: x = 2; y = 4.

· “==” – This operator checks the equality of the two operands. If the values are equal, the condition is true. For example: (x == y) is false.

· “!=” – With this operator, you can check if the operands have unequal values. If the values are unequal, the condition is true. For instance: (x != y) is true.

· “>” – Here, you’ll check if the left-hand operand’s value is greater than that of the right-hand one. If it is, the condition is true. For instance, (y > x) is true.

· “<” – This operator checks the values of both operands. If the value of the left-hand operand is less than that of the right-hand operand, the condition is true. For example: (x > y) is true.

· “>=” - You can use this operator to check if the left-hand operand’s value is greater than or equal to that of the right-hand one. If it is, the condition is true. For instance: (y >= x) is true.

· “<=” – With this operator, you can check if the value of the left-hand operand is less than or equal to that of the right-hand operand. If it is, the condition is true. For example: (x <= y) is true.

The Assignment Operators

These are the operators that you can use to assign values:

· “=” - This is the Simple Assignment operator. It assigns the value of the right-hand operand to the left-hand operand. For example: (z = x + y) assigns the value of x + y to z.

· “+=” – This operator is known as“Addition and Assignment” operator. It adds the values of both operands and gives the result to the left-hand operand. For example: (z += x) is equal to (z = z + x).

· “-=” – Programmers call this the“Subtraction and Assignment” operator. It subtracts the value of the right-hand operand from that of the left-hand operand. Then, it assigns the difference to the left-hand operand. For example: (z -= x) is equal to (z = z– x).

· “*=” – This operator is called“Multiplication and Assignment.” It multiplies the values of both operands and gives the product to the left-hand operand. For example: (z *= x) is equal to (z = z * x).

· “/=” – This is known as the“Division and Assignment” operator. It divides the value of the left-hand operand by the value of the right-hand operand. Then, it assigns the quotient to the left-hand operand. For instance: (z /= x) is equal to (z = z / x).

· “%=” – JavaScript users refer to it as the“Modulus and Assignment” operator. It takes the modulus of both operands. Then, it gives the result to the left-hand operand. For example: (z %= x) is equal to (z = z % x).

The Arithmetic Operators

The list below shows the arithmetic operators available in JavaScript. To help you understand how these operators work, let’s use two sample variables: x = 2; y = 4.

· “+” – You should use this operator if you want to perform addition on two operands (i.e. the values on either side of the operator). For instance: x + y = 6.

· “-” – This operator allows you to perform subtraction on your codes. Here, you’ll deduct the value of the second operand from that of the first. For example: y– x = 2.

· “*” – You should use an asterisk if you want to multiply the two operands. For example: x * y = 8.

· “/” – With this operator, you’ll divide the value of the left-hand operand from that of the right-hand operand. For example: y / x = 2.

· “++” – This operator allows you to add 1 to the operand it is used on. For example: 4++ = 5.

· “--” – This operator subtracts 1 from the operand it is attached to. For example: x-- = 1.

Important Note: You may also use the“+” operator to combine numeric and string elements. For example,“x” + 4 = x4.

The Bitwise Operators

This programming language allows you to perform bitwise operations in your codes. Here are the operators that you can use:

Note: Let’s use two variables: x = 2; y = 3.

· “&” – This is called the Bitwise AND operator. It conducts the Boolean AND operation on all of the bits involved in its arguments. For example: (x & y) is 2.

· “|” – Programmers refer to this operator as Bitwise OR. It conducts the Boolean OR operation on the bits involved in its arguments. For instance: (x | y) is 3.

· “^” – This is known as the Bitwise XOR operator. It conducts the Boolean Exclusive OR operation on all of the bits involved in its arguments. With Exclusive OR, only one of the operands can be true. For example: (x ^ y) is 1.

· “~” – This operator is“unary” (i.e. it works on a single operand). Known as the Bitwise NOT operator, it reverses the bits of the operand. For example (~y) is -4.

· “<<” – This operator is called Left Shift. It moves the bits of the left-hand operand to the left based on the number specified by the right-hand operand. Also, it uses zeros to fill new bits. Moving a value to the left by 1 position is like multiplying it by 2. Thus, (x << 1) gives 4.

· “>>” - This is known as the Right Shift operator. It moves the value of the left-hand operand to the right based on the number given by the right-hand operand. For example: (x >> 1) gives 1.

· “>>>” – JavaScript users call this“Right Shift and Zero.” Its function is similar to that of“>>.” The only difference is that the leftmost bits are always equal to zero.

The Miscellaneous Operators

This section of the book will discuss two useful operators that don’t belong to the categories discussed above. These operators are:

· “?:” – This is known as the“Conditional Operator.” It checks whether the value of an expression is true or false. Then, it performs one of the assigned statements based on the evaluation’s result.

For example, if the condition is true, the operator will assign 1 to the expression. If the condition is false, however, the operator will assign 2.

· typeof– This is a unary operator, which means you can use it on a single operand. You can use it to determine an operand’s data type. This operator gives“string,” “boolean,” or“number” if the operand is a string, Boolean, or number. Then, it gives true or false, depending on the result of the evaluation.

The table below provides useful information regarding the typeof operator:

Data Type

The typeof Result

Object

“object”

Number

“number”

Boolean

“boolean”

Undefined

“undefined”

Null

“object”

Function

“function”

String

“string”