  # JavaScript Operators

Sunday, April 08, 2012 JavaScript Using operators in JavaScript is very similar to how they are used in other programming languages. There are different types of operators you can use in JavaScript. For example, there are operators used for arithmetic, assignment , comparison, and also concatenation.

## Arithmetic Operators

Arithmetic operators are used to perform basic arithmetic between variables and/or values.

OperatorDescriptionExample>y=?x=?
-Subtractionx=y-3y=5x=2
*Multiplicationx=y*3y=5x=15
/Divisionx=y/3y=6x=2
%Modulus (division remainder)x=y%3y=7x=1
++Incrementx=++yy=5x=6
--Decrementx=--yy=5x=4

## Assignment Operators

Assignment operators are used to assign values to JavaScript variables.

OperatorValuesExampleEquivalent toResult
=x=2,y=3x=yx=yx=3
+=x=2,y=4x+=yx=x+yx=6
-=x=2,y=5x-=yx=x-yx=-3
*=x=2,y=6x*=yx=x*yx=12
/=x=20,y=5x/=yx=x/yx=4
%=x=7,y=3x%=yx=x%yx=1

## Comparison Operators

Comparisons are used to check the relationship between variables and/or values. Comparison operators are used inside conditional statements and for evaluation. The result is either true or false.

OperatorDescriptionValuesExampleResult
==Equal Tox=2,y=3x == yFALSE
===Equal To in Value and Typex=2,y=3x === yFALSE
!=Not Equal Tox=2,y=4x != yTRUE
!==Not Equal To in Value nor Typex=2,y=4x !== yTRUE
<Less Thanx=2,y=5x < yTRUE
>Greater Thanx=2,y=6x > yFALSE
<=Less Than or Equal Tox=2,y=7x <= yTRUE
>=Greater Than or Equal Tox=2,y=8x >= yFALSE

## Logical Operators

Logical operators are used to determine the logic between variables or values. Assume in the following example that x=2 and y=3.

OperatorDescriptionExampleResult
&&and(x < 10 && y > 1)TRUE
||or(x==5 || y==4)FALSE
!not!(x==y)TRUE

## Concatenation

The + symbol can be used to concatenate two stings or a number and a string. The result of concatenation is a string value. In the following example, assume that string1 has a value of "John" and that string2 has a value of "Smith".

String #1String #2ExampleResult
string1string2string1 + string2"John Smith"
"John"string2"John" + string2"John Smith"
string1"Smith"string1 + "Smith""John Smith"
string1string2string1 + "A." + string2"John A. Smith"

Did you find the page informational and useful? Share it using one of your favorite social sites.

Recommended Books & Training Resources