Did you find this useful?
Socialize it today.

JavaScript Operators

Sunday, April 08, 2012

javascript operatorsUsing 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.

%Modulus (division remainder)x=y%3y=7x=1

Assignment Operators

Assignment operators are used to assign values to JavaScript variables.

OperatorValuesExampleEquivalent toResult

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.

==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.

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


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"

Please help us spread the word by socializing it today!

email contact us

Did you find something wrong with the information on this page? Please take a moment to report it to us so that we can continue to improve the quality of the information on this site. Click here to report an issue with this page.

Recommended Books & Training Resources

Murachs JavaScript and DOM Scripting JavaScript and jQuery: The Missing Manual HTML CSS and JavaScript Editor