WEB DEVELOPER SITE
×

# JavaScript Operators Reference

JavaScript administrators are utilized to dole out qualities, analyze values, perform number-crunching tasks, and more.

## JavaScript Arithmetic Operators

Arithmetic administrators are utilized to perform number-crunching among factors or potentially values.

Given that y = 5, the table underneath clarifies the number juggling administrators:

Operator Description Example Result in y Result in x
+ Addition x = y + 2 y = 5 x = 7
- Subtraction x = y - 2 y = 5 x = 3
* Multiplication x = y * 2 y = 5 x = 10
/ Division x = y/2 y = 5 x = 2.5
% Modulus (division remainder) x = y % 2 y = 5 x = 1
++ Increment x = ++y y = 6 x = 6
x = y++ y = 6 x = 5
- - Decrement x = - y y = 4 x = 4
x = y- - y = 4 x = 5
JavaScript Arithmetic Tutorial.

## JavaScript Assignment Operators

Assignment administrators are utilized to allot esteems to JavaScript variables.

Given that x = 10 and y = 5, the table underneath clarifies the task operators:

Operator Example Same As Result in x Try it
= x = y x = y x = 5
+= x += y x = x + y x = 15
-= x - = y x = x - y x = 5
*= x *= y x = x * y x = 50
/= x/= y x = x/y x = 2
%= x %= y x = x % y x = 0
JavaScript Assignment Tutorial.

## JavaScript String Operators

The + administrator, and the += administrator can likewise be utilized to connect (include) strings.

Given that text1 = "Good ", text2 = "Morning", and text3 = "", the table underneath clarifies the operators:

Operator Example text1 text2 text3
+ text3 = text1 + text2 "Good " "Morning"  "Good Morning"
+= text1 += text2 "Good " "Morning" ""

## Comparison Operators

Comparison administrators are utilized in coherent proclamations to decide correspondence or distinction between factors or qualities.

Given that x = 5, the table beneath clarifies the correlation operators:

Operator Description Comparing Returns
== equal to x == 8 false
x == 5 true
=== equal esteem and equivalent type x === "5" false
x === 5 true
!= not equal x != 8 true
!== not break even with esteem or not equivalent type x !== "5" true
x !== 5 false
> greater than x > 8 false
< less than x < 8 true
>= greater than or parallel to x >= 8 false
<= less than or measure up to to x <= 8 true
JavaScript Examinations Tutorial.

## Conditional (Ternary) Operator

The restrictive administrator allocates an incentive to a variable dependent on a condition.

Syntax Example
variablename = (condition) ? value1:value2 voteable = (age < 18) ? "Too young":"Old enough";

Example explained: If the variable "age" is an incentive beneath 18, the estimation of the variable "voteable" will be "Excessively youthful", generally the estimation of voteable will be "Old enough".

## Logical Operators

Logical administrators are utilized to decide the rationale between factors or values.

Given that x = 6 and y = 3, the table underneath clarifies the consistent administrators:

Operator Description Example
&& and (x < 10 && y > 1) is true
|| or (x === 5 || y === 5) is false
! not !(x === y) is true

## JavaScript Bitwise Operators

Bit administrators deal with 32 bits numbers. Any numeric operand in the task is changed over into a 32 bit number. The outcome is changed over back to a JavaScript number.

Operator Description Example Same as Result Decimal
& AND x = 5 & 1 0101 & 0001 0001  1
| OR x = 5 | 1 0101 | 0001 0101  5
~ NOT x = ~ 5  ~0101 1010  10
^ XOR x = 5 ^ 1 0101 ^ 0001 0100  4
<< Left shift x = 5 << 1 0101 << 1 1010  10
>> Right shift x = 5 >> 1 0101 >> 1 0010   2

## The typeof Operator

The typeof administrator restores the sort of a variable, item, work or expression:

### Example

typeof "John"                 /Returns string
typeof 3.14                   /Returns number
typeof NaN                    /Returns number
typeof false                  /Returns boolean
typeof [1, 2, 3, 4]           //Returns object
typeof {name:'John', age:34}  /Returns object
typeof new Date()             /Returns object
typeof work () {}        /Returns function
typeof myCar                  /Returns vague (if myCar isn't declared)
typeof null                   /Returns object