JavaScript – Operators

JavaScript Operators

An operator in javascript is a symbol or keyword that performs some sort of calculation. JavaScript uses different types of operators:

Arithmetic

Assignment

Comparison

Logical

Bitwise

Special

 

Arithmetic Operators:

Addition (+), subtraction (-), multiplication (*), Division (/), modulus (%), increment (++) pre and post, decrement (–) pre and post

Type coercion is attempting to change the data type when deemed necessary.

var x = 10; Number

var y = 1.23; Decimal

document.write(x+y) will display 11.23 result is decimal

 

var x = 10; Number

var y = “1”; String

document.write(x+y) will display 101 result is string

 

Unary +

var x = 10; Number

var y = “1”; String

var z = x + (+y); the y string is coerce in to number and result is 11

Assignment Operators

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

 

Comparison Operators

== example 4==3 false “4” == 4 is true coercion, 1 = true is true, “car” == “Car” false

!= not equal to

=== (strict equal to) true if both sides are equal and of same type (no coercion is performed)

!== (strict not equal to)

>

<

>=

<=

 

Logical Operators

&&

||

!

 

Bitwise Operators

&

|

^ XOR

~ not

<< Left shift

>> right shift

 

Special Operators

?: conditional   x<y?10:20