JavaScript Operators

JavaScript Operators

The Early Man have been said to have used different methods for counting; of which is using stones and cowries. Thousands of years later we now have a lot of tools we can use for numerical operation. We are not here to tell a story so why don't we dive right into the topic above. JavaScript includes operators as in other languages. An operator performs some operation on single or multiple values (data value) and produces a result. For example 1 + 2, where + sign is an operator and 1 is the left value and 2 is the right value. + operator adds two numeric values and produces a result which is 3 in this case.

JavaScript includes the following categories of operators:

  1. Arithmetic Operators
  2. Comparison Operators
  3. Logical Operators
  4. Assignment Operators
  5. Conditional Operators

Arithmetic Operators

Arithmetic operators are used to perform arithmetic on numbers

Addition +

Subtraction -

Multiplication *

Exponentiation **

Division /

Modulus (Division Remainder) %

Increment ++

Decrement --

The following example demonstrates how arithmetic operators perform different tasks on values. Example: Arithmetic Operator

var x = 2, y = 5, z = 7;

x + y; //returns 7

y - x; //returns 3

x * y; //returns 10

y / x; //returns 2.5

x % 2; //returns 0

x++; //returns 3

x--; //returns 1
  • operator performs concatenation operation when one of the values is of string type.

The following example shows how + operator performs operation on values of different data types. Example:

var a = 10, b = "Hello ", c = "World!", d = 8;

a + b; // "10Hello "

b + c; // "Hello World!"

a + d; // 18

Comparison Operators

JavaScript language includes operators that compare two values and return Boolean value true or false.

== Compares the equality of two values without considering type.

=== Compares equality of two values with type.

!= Compares inequality of two values.

> Checks whether left side value is greater than right side value. If yes then returns true otherwise false.

< Checks whether left value is less than right value. If yes then returns true otherwise false.

>= Checks whether left value is greater than or equal to right value. If yes then returns true otherwise false.

<= Checks whether left value is less than or equal to right value. If yes then returns true otherwise false.

Example:

var a = 3, b = 15, c = "3";
var x = a;

a == c; // returns true

a === c; // returns false

a == x; // returns true

a != b; // returns true

a > b; // returns false

a < b; // returns true

a >= b; // returns false

a <= b; // returns true

a >= c; // returns true

a <= c; // returns true

you can read more about it on w3schools

Logical Operators

Logical operators are used to combine two or more conditions. JavaScript includes following logical operators.

&& is known as AND operator. It checks whether two values are non-zero (0, false, undefined, null or "" are considered as zero), if yes then returns 1 otherwise 0.

|| is known as OR operator. It checks whether any one of the two values is non-zero (0, false, undefined, null or "" is considered as zero).

! is known as NOT operator. It reverses the boolean result of the value (or condition)

Example:

var a = 10, b = 15;

(a != b) && (a < b); // returns true

(a > b) || (a == b); // returns false

(a < b) || (a == b); // returns true

!(a < b); // returns false

!(a > b); // returns true

Assignment Operators

JavaScript includes assignment operators to assign values to variables with less key strokes.

= is used as x = y and it is same as x = y

+= is used x += y and it is same as x = x + y

-=is used as x -= y and it is same as x = x - y

*= is used as x = y and it is the same as x = x y

/= is used as x /= y and it is the same as x = x / y

%= is used as x %= y and it is the same as x = x % y

**= is used as x = y and it is the same as x = x y

The addition assignment operator (+=) adds a value to a variable.

Example:

var x = 2, y = 8, z = 16;

x = y; //x would be 8

x += 1; //x would be 3

x -= 1; //x would be 1

x *= 2; //x would be 4

x /= 2; //x would be 1

x %= 2; //x would be 0

Conditional Operators

This operator is also known as Ternary Operator. JavaScript includes special operator called ternary operator :? that assigns a value to a variable based on some condition. This is like short form of if-else condition. When you want to execute a block of code if a particular test evaluates to true, you often use the if-else statement. For example, if age is greater than 16, then allow the person to drive can be coded as follows:

var age = 19;
var canDrive;
if (age > 16) {
    canDrive = 'yes';
} else {
    canDrive = 'no';
}

In this example, you can use the ternary operator as the shortcut for the if-else statement as follows:

var age = 19;
var canDrive = age > 16 ? 'yes' : 'no';

The code looks much cleaner.

In general, the syntax of the ternary operator is as follows:

condition ? expression_1 : expression_2;

The JavaScript ternary operator is the only operator that takes three operands.

The condition is an expression that evaluates to a Boolean value, either true or false. If the condition is true, the ternary operator returns expression_1, otherwise it returns the expression_2.

The expression_1, and expression_2 are expressions of any type.

Here are some resources to guide you:

w3schools tutorialspoint TutorialsTeacher

Thanks for reading!

Feel free to like and leave a comment.