JavaScript Operators

Sunday, April 8, 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"

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

Recommended Books & Training Resources

Professional JavaScript for Web Developers HTML CSS and JavaScript Editor JavaScript and jQuery: The Missing Manual