Operator in JavaScript

JavaScript’s Operators

Operators are used for JavaScript’s arithmetic, comparison, logical, assignment expressions and more. Most operators are represented by punctuation characters such as + and =, some, however, are represented by keyword such as delete and instanceof.

Table below is organized by operator precedence.

  • The operator listed first has higher precedence then those listed last.
  • Operators separated by a horizontal line have different precedence levels.
  • The column labeled A gives the operator associativity, which can be L (left to right) or R (right to left).
  • Column N specifies the number of operands.
  • The column labeled Types lists the expected types of the operands and (after the symbol) the result type for operator.
Operator

Operation

A

N

Types

++

Pre- or post-decrement

R

1

Ivalnum

Pre- or post-decrement

R

1

Ivalnum

Negative number

R

1

numnum

+

Convert to number

R

1

numnum

~

Invert bits

R

1

intint

!

Invert boolean value

R

1

boolbool

delete

Remove a property

R

1

Ivalbool

typeof

Determine type of operand

R

1

anystr

void

Return undefined value

R

1

anyundef

*,/,%

Multiply, divide, remainder

L

2

num,numnum

+,-

Add, subtract

L

2

num,numnum

+

Concatenate strings

L

2

str,strstr

<<

Shift left

L

2

int,intint

>>

Shift right with sign extension

L

2

int,intint

>>>

Shift right with zero extension

L

2

int,intint

<,<=,>,>=

Compare in numeric order

L

2

num,numbool

<,<=,>,>=

Compare in alphabetic order

L

2

str,strbool

instanceof

Test object class

L

2

obj,funcbool

in

Test whether property exists

L

2

str,objbool

==

Test for equality

L

2

any,anybool

!=

Test for inequality

L

2

any,anybool

===

Test for strict equality

L

2

any,anybool

!==

Test for strict inequality

L

2

any,anybool

&

Compute bitwise AND

L

2

int,intint

^

Compute bitwise XOR

L

2

int,intint

|

Compute bitwise OR

L

2

int,intint

&&

Compute logical AND

L

2

any,anyany

||

Compute logical OR

L

2

any,anyany

?:

Choose 2nd and 3rd operand

R

3

bool,any,anyany

=

Assign to a variable or property

R

2

Ival,anyany

*=,/=,%=,+=,

-=,&=,^=,|=,

<<=,>>=,>>>=

Operate and assign

R

2

Ival,anyany

,

Discard 1st operand, return 2nd

L

2

any,anyany

 

lval :Notice that the assignment operators and a few of other operators listed in table above expect an operand of type lval. lavlue is a historical term that means “an expression that can legally appear on the left side of an assignment expressions.”

In JavaScript, variables, properties of objects, and elements of arrays are lvalues. The ECMAScript specification allows built-in functions to return lvalues but does not define any function that behaves that way.

Order of Evaluation

Operator precedence and associativity specify the order in which operations are performed in a complex expression, but they do not specify the order in which the sub-expressions are evaluated. JavaScript always evaluates expression in strictly left-to-right order. In the expression w=x+y*z, for example, the sub-expression w is evaluated first, followed by x, y and z. Then the values of y and z are multiplied, added to the value of x and assigned to the variable or property specified by expression w. Adding parentheses to the expression can change the relative order of multiplication, addition and assignment, but not the left-to-right order of evaluation.

Order of evaluation only makes a difference if any of the expressions being evaluated has side-effects that affect the value of another expression. If expression x increments a variable that is used by expression z, then the fact that x is evaluated before z in important.

 

Arithmetic Operators

Arithmetic operations can be carried out by these arithmetic operators between variables or values. Taking value of b as 10 below are the arithmetic operators explained with example.

Operator

Operation

Example

Result

+

Addition

a = b+7

a = 17, b = 10

Subtraction

a = b – 7

a = 3, b = 10

*

Multiplication

a = b * 7

a = 70, b = 10

/

Division

a = b / 3

a = 3.3333333333333335,

b = 10

%

Modulus

(remainder after division)

a = b % 7

a = 3, b = 10

++

Increment

a = ++b

a = b++

a = 11, b = 11

a = 10, b = 11

Decrement

a = –b

a = b–

a = 9, b = 9

a = 10, b = 9

<!DOCTYPE html>

<html lang="en">

<head>

<meta http-equiv="Content-Type" content="text/html charset=utf-8" />

<title>JavaScript Arithmetic Operator</title>

</head>

<body>

	<h2>This is an example of JavaScript Arithmetic Operator</h2>

	<p>b = 10, then calculate a = b % 7.</p>

	<button onclick="showResult()">Rssult</button>

	<p id="result_here">
		<font color="green">result here</font>
	</p>

	<script>
		functionshowResult()

		{

			var b = 10;

			var a = b % 7;

			document.getElementById("result_here").innerHTML = "a = " + a
					+ ", b = " + b;

		}
	</script>

</body>

</html>

 

Assignment Operators

As indicated by the name, assignment is done by these operators. Taking a = 4, b = 3.

Operator

Example

Result

=

a = b

a = b

a = 3

+=

a += b

a = a + b

a = 7

-+

a -= b

a = a – b

a = 1

*=

a *= b

a = a * b

a = 12

/=

a /= b

a = a / b

a = 1.3333333333333333

%=

a %= b

a = a % b

a = 1

 

String Operator

The ‘+’ operator can be used to concatenate strings. Like this :

string1 = "I am learning ";

string2 = "JavaScript here.";

string3 = string1 + string2; // I am learning JavaScript here.

 

Bitwise Operators

The bitwise operators perform low-level manipulation of the bits in the binary representation of numbers. They expect integer operands and behave as if those values were represented as 32-bit integers rather than 64-bit floating point values.

Operator

Operation

Example

Result

&

AND

a = 5& 1

0101 & 0001

0001

1

|

OR

a = 5 | 1

0101 | 0001

0101

5

~

NOT

a = ~ 5

0101

-6

^

XOR

a = 5 ^ 1

0101 ^ 0001

0100

4

<<

Left Shift

a = 5 << 1

0101 << 0001

1010

10

>>

Right Shift

a = 5 >> 1

0101 >> 0001

0010

2

 

Comaprion Operators

Comparison operator tests equality or difference between variables or values. Taking a = 7:

Operator

Operation

Example

Result

==

equal to

a == 9

a == 7

false

true

===

equal value and equal type

a === “7”

a === 7

false

true

!=

not equal

a != 9

true

!==

not equal value or not equal type

a !== “7”

a !== 7

true

false

>

greater than

a > 9

false

<

less than

a < 9

true

>=

greater than or equal to

a >=9

false

<=

less than or equal to

a <=9

true

 

Logical Operators

Logic between variables or values is tested by logical operators. Taking a = 3 and b = 7:

Operator

Operation

Example

Result

&&

and

( a < 5 && b > 6 )

true

||

or

( a == 5 || b == 6 )

false

!

not

!( a == b )

true

 

Conditional Operators

JavaScript also contains a conditional operator that assigns a value to a variable based on some condition.

result = ( marks > 60 )? "Pass" : "Fail";

 

typeof Operators

The typeof operator returns the type of a variable (or an expression):

var x =  3;var y = "Name";
typeof x                      // Returns number
typeof y                      // Returns string
typeof 3.14                   // Returns number
typeof false                  // Returns boolean
typeof [1,2,3,4]              // Returns object

 

Delete Operators

The delete operator can be used to delete properties from objects:

var person = {firstName:"Ramesh", lastName:"Singh", age:32, eyeColor:"brown"};
delete person.age;

1 Comment Operator in JavaScript

Leave A Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.