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.
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:
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.
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 operatorThese 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
The *
operator performs a multiplication between two or more numbers.
The /
operator performs a division between two numbers.
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
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:
&&
)||
)!
)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 is the translation of the English word "and". For example, if we have to write apples and oranges, we write apples && oranges.
This returns the most specific value on an operand; if a non-Boolean value is used, then a non-Boolean value is returned.
We normally write an expression or a condition that is to be evaluated separated by the '&&'
sign.
This operator is the translation of the English word "or". For example, if we have to write apples or oranges, we write apples || oranges.
This returns the most specific value on an operand; if a non-Boolean value is used, then a non-Boolean value is returned.
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.
This operator is known as subtraction assignment operator.
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
.
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 <
(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.
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.
If the preceding condition is true, then it returns the Boolean value true
, otherwise it returns false
.
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.
If the preceding condition is true, then it returns the Boolean value true
, otherwise it returns false
.
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.
If the preceding condition is true, then it returns the Boolean value true
, otherwise it returns false
.
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.
If the preceding condition is true, then it returns the Boolean value true
, otherwise it returns false
.
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.
If the preceding condition is true, then it returns the Boolean value true
, otherwise it returns false
.
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.
If the preceding condition is true, then it returns the Boolean value true
, otherwise it returns false
.
18.117.98.250