Operators

In programming languages, operators are operations to be performed on operands. Basically, operators are symbols used to perform various operations. Consider the following example:

9 + 11 = 20

In the preceding example, 9 and 11 are operands and + is the additional operator.

Overview

Like other languages, in JavaScript, there are several operators to perform an operation, such as addition, multiplication, subtraction, and so on.

In JavaScript, there are different operators such as:

  • The logical operator
  • The bitwise operator
  • The conditional operator
  • The arithmetic operator
  • The assignment operator

Binary operator

As the name suggests, binary operators require two operands; for example:

A + B

Here, A and B are operands and + is the operator to perform addition between them.

Unary operator

In programming languages, unary operators have just one operand and operator with them, for example:

A++;

This can also be written as:

++A;

Note

A++ is a post increment operator. It will first evaluate A and then increment it; whereas,++A is a pre-increment operator. It increments the value of A and then evaluates the expression.

Ternary operator

The ternary operators require three operands to perform different operations; for example:

<condition> ? <true>: <false>;

In the preceding statement, ? is the ternary operator.

Arithmetic operators

In programming languages, arithmetic operators are use to perform arithmetic operations on values and variables. The basic arithmetic operations are as follows:

  • +: Addition
  • -: Subtraction
  • *: Multiplication
  • /: Division
  • %: Modulus operator
  • ++: Increment operator
  • --: Decrement operator

These operators perform operations on numeric operands and return a result as a numeric value.

In JavaScript, operators are the same as those in other programming languages; for example, if you want to perform division between variables, then you will write the expression as follows:

var a=2;
a=(2+a)*1/a;// a=2
console.log(a); // prints 2

Note

In JavaScript, arithmetic operators are the most useful and powerful operators.

The + operator

The + operator performs an addition of two or more numbers.

Returns

The + operator returns the addition of the numbers.

Parameter

The + operator does not have any parameters.

Description

The + operator is used to add two numbers or two variables in which numbers are stored.

For example:

var a = 3;
var b = 4;
var c = a + b;
Document.Write("The value of c is " + c);
Document.Write(3+1);

The output of this will be:

The value of c is 7
4

Note

This operator can also be used to append or concat two strings; for example:

document.write("Apple "+"Banana");

The output will be as follows:

Apple Banana

The - operator

The - operator performs a subtraction between two numbers.

Returns

The result of the - operator returns subtraction between two numbers.

Parameter

The - operator has no parameters.

Description

The - operator is used to subtract two numbers or two variables in which numbers are stored.

Consider the following example:

var a = 6;
var b = 4;
var c = a - b;
Document.Write("The value of c is " + c);
Document.Write(3-1);

The output of this will be as follows:

The value of c is 2
2

The * operator

The * operator performs a multiplication between two or more numbers.

Returns

The result of the multiplication between the numbers.

Parameter

There are no parameters.

Description

This operator is used to multiply two or more numbers or two or more variables in which numbers are stored.

Consider the following example:

var a = 3;
var b = 4;
var c = a * b;
Document.Write("The value of c is " + c);
Document.Write(3*1);

The output of this will be as follows:

The value of c is 12
3

The / operator

The / operator performs a division between two numbers.

Returns

The result of the division between the numbers.

Parameter

There are no parameters.

Description

This is also called the remainder operator, since the value returned is not an absolute value (in that, it retains the sign of the first operand, not the second).

Consider the following example:

var a = 5;
var b = 2;
var c = a % b;
Document.Write("The value of c is " + c);
Document.Write(10%3);

The output will be as follows:

The value of c is 1
1

The % operator

The % modulus operator is used to calculate the remainder.

Returns

The result of the multiplication between the numbers.

Parameter

There are no parameters.

Description

This operator is used to multiply two or more numbers or two or more variables in which numbers are stored.

Consider the following example:

var a = 3;
var b = 4;
var c = a * b;
Document.Write("The value of c is " + c);
Document.Write(3*1);

The output will be as follows:

The value of c is 12
3

The ++ Operator

The ++ operator is used to increment a value.

Returns

The result is the incremented value.

Parameter

There are no parameters.

Description

Increment. the value and stores the value in itself.

Consider the following example:

I = 0; // I contains 0
I++; // this line is equivalent to I = I + 1 

The output of this will be:

I = 1

The -- Operator

The -- operator is used to decrement a value.

Returns

The result is the decremented value.

Parameter

There are no parameters.

Description

The -- operator decrements the value and stores the value in itself.

For example:

I = 0; // I contains 0
I--; // this line is equivalent to I = I - 1 

The output will be as follows:

I = - 1

Logical operators

In programming languages, logical operators are Boolean operators. These operators work on Boolean logic. Like other languages, JavaScript works on Boolean evaluation. These Boolean operators always return a Boolean value. There are two possible results of any logical operator:

  • True
  • False

The following are the logical operators used in programming languages as well as JavaScript:

  • Logical AND (&&)
  • Logical OR(||)
  • Logical NOT(!)

Note

Arithmetic operators get priority over logical operators.

Let's say, if there is an expression and there are logical and arithmetic operators in it, then the arithmetic operators will be evaluated first because of their higher priority. Logical operators work from left to right.

Logical operations are applicable on everything. A non-Boolean value can result in a Boolean value after evaluation.

The && Operator

The && operator is the translation of the English word "and". For example, if we have to write apples and oranges, we write apples && oranges.

Returns

This returns the most specific value on an operand; if a non-Boolean value is used, then a non-Boolean value is returned.

Parameter

We normally write an expression or a condition that is to be evaluated separated by the '&&' sign.

Description

This is called the AND operator.

Here is an example of the && operator:

var a= true && true; // it will be true
var b=true && false;//it will be false

The || Operator

This operator is the translation of the English word "or". For example, if we have to write apples or oranges, we write apples || oranges.

Returns

This returns the most specific value on an operand; if a non-Boolean value is used, then a non-Boolean value is returned.

Parameter

We normally write an expression or a condition that is to be evaluated separated by the '||' sign.

Description

This is called the OR operator.

Here is an example of the || operator:

var a= true  ||  true; // it will be true
var b=true  ||  false;// it will be true

The ! Operator

This is the NOT operator. NOT operator evaluates if the values are not equal to each other. For instance:

if(abc != 0){
 //if abc is not equal to 0 run this block
}else{
 //if abc = 0 then run this block
}

Returns

Boolean value.

Parameter

null

Description

This is called as the NOT operator.

Here is an example of the ! operator:

var a=!false //it will print true
var b=!true //it will print false

Note

In JavaScript, these logical operators are used in conditional statements and in the while loop.

Assignment operators

In JavaScript, assignment operators are used to assign values to variables. The assignment operator = assigns the value of the operand on it right to the operand on its left. For instance, abc = xyz, here xyz is right operand whose value is being assigned to abc, which is the left operand. An assignment operator can assign the value of a single variable to multiple variables. In the following sections, we will discuss the assignment operators in JavaScript.

The = operator

The = operator is used to assign the value to a variable.

Returns

There are no parameters but it is used between two or more operands.

Parameters

There are no parameters.

Description

This is the simplest of all assignment operators and is used commonly. It assigns the value of the right side operand to the left side operand.

Consider the following example:

var x=2;
console.log("The value of x is " + x);

The output of this will be:

The value of x is 2

The += operator

This operator is known as addition assignment operator.

Returns

This adds the value to the variable and returns the result of the addition.

Parameters

There are no parameters.

Description

The addition assignment operator is used to add a value to a variable and assign the value to another or the same variable in the same operation.

Consider the following example:

var abc = 2;
abc += 10;
console.log(abc); //12

This is short hand of writing abc = abc + 10.

The -= operator

This operator is known as subtraction assignment operator.

Returns

This operator subtracts the given value from the variable and returns the result of the subtraction.

Parameters

There are no parameters.

Description

The -= operator is used to subtract a value from a variable and assign the given value to another or the same variable in the same operation.

Consider the following example:

var abc = 10;
abc -= 2;
console.log(abc); //8

This is short hand of writing abc = abc - 2.

The *= Operator

This operator is known as multiplication assignment operator.

Returns

This multiplies the variable and the value and returns the result of the multiplication.

Parameters

There are no parameters.

Description

This operator is used to multiply a value with a variable and assign the value to another or the same variable in the same operation.

Consider the following example:

var abc = 5;
abc *= 5;
console.log(abc); //25

This is short hand of writing abc = abc * 5.

The /= Operator

This operator is known as division assignment operator.

Returns

It divides the variable with the value of right operand and assign it to the variable.

Parameters

There are no parameters.

Description

This operator is used to divide a variable with the value and assign the result to the variable.

Consider the following example:

var abc = 5;
abc /= 5;
console.log(abc); //1

This is short hand of writing abc = abc / 5.

The %= Operator

The %= operator is also called remainder assignment operator.

Parameters

The %= operator takes a null parameter.

Returns

The %= operator returns the remainder.

Description

It divides the variable with the value of right operand and assigns the remainder value it to the variable. For example:

var abc = 10;
abc %= 3;
console.log(abc); //1

This is short hand of writing abc = abc % 3.

Note

This is an experimental technology, part of the ECMAScript 2016 (ES7) proposal.

Exponentiation assignment (**=)

This operator returns the result of raising power of left operand to the value of second operand.

var abc = 5;
abc **= 2;
console.log(abc); //25

This is short hand of writing abc = abc ** 2.

Relational operators

In programming languages, relational operators are also known as comparison operators. These operators show the relative order of two values. These comparison operators are covered in the following sections.

The < operator

The < (Less Than) operator operator is used to compare the values on both sides of the expression and check whether the value of the left-hand side is less than the value on the right-hand side of the operator.

Returns

If the preceding condition is true, then expression returns true else false.

Parameter

Here, both the operands must be numbers or both must be strings.

Description

This is also known as the less than operator.

A simple example of the usage of this operator is as follows:

if(2<3) { //returns true
  //block of code
}

The <= Operator

The <= operator is used to compare the values on both sides of the expression and check whether the value on the left-hand side is less than or equal to the value on the right-hand side of the operator.

Returns

If the preceding condition is true, then it returns the Boolean value true, otherwise it returns false.

Parameter

Here, both the operands must be numbers or both must be strings.

Description

This is also known as the less than or equal to operator.

A simple example of the usage of this is as follows:

if(2 <= 3) { //returns true
  //block of code
}

The > Operator

The > operator is used to compare the values on both sides of the expression and check whether the value on the left-hand side is greater than the value on the right-hand side of the operator.

Returns

If the preceding condition is true, then it returns the Boolean value true, otherwise it returns false.

Parameter

Here, both the operands must be numbers or both must be strings.

Description

This is also known as the greater than operator.

A simple example of the usage of this is as follows:

if(2>3) { //returns false
  //block of code
}

The >= Operator

The >= operator is used to compare the values on both the sides of the expression and check whether the value on the left-hand side is greater than or equal to the value on the right-hand side of the operator.

Returns

If the preceding condition is true, then it returns the Boolean value true, otherwise it returns false.

Parameter

Here, both the operands must be numbers or both must be strings.

Description

This is also known as the greater than or equal to operator.

A simple example of the usage of this is as follows:

if(3 >= 3) { //returns true
  //block of code
}

The != Operator

The != operator is used to compare the values on both the sides of the expression and check whether the value on the left-hand side is not equal to the value on the right-hand side of the operator.

Returns

If the preceding condition is true, then it returns the Boolean value true, otherwise it returns false.

Parameter

Here, both the operands must be numbers or both must be strings.

Description

This is also known as the not equal to operator.

A simple example of the usage of this is as follows:

if(2!=3) { //returns true
  //block of code
}

The == Operator

The == operator is used to compare the values on both sides of the expression and check whether the value of the left-hand side is equal to the value on the right-hand side of the operator.

Returns

If the preceding condition is true, then it returns the Boolean value true, otherwise it returns false.

Parameter

Here, both the operands must be numbers or both must be strings.

Description

This is also known as the Equal To operator.

A simple example of the usage of this is as follows:

if(2==3) { //returns false
  //block of code
}

The === Operator

The === operator is used to compare the values on both sides of the expression and see if the value of the left is equal to the value on the right of the operator and to also check if both the operands are of the same datatype.

Returns

If the preceding condition is true, then it returns the Boolean value true, otherwise it returns false.

Parameter

The === operator takes parameter null.

Description

This is also known as the equal value and equal type operator.

A simple example of the usage of this is as follows:

if(3 === 3) { //returns true
  //block of code
}
if(3 === "3") { //returns false
  //block of code
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.76.72