Operators "operate" on values. We deal with operators every day, you have probably just never heard them called that. For example, in a statement such as 1+1=2, the + sign is the operator. JavaScript offers many operators beyond the basic ones used in arithmetic.
Syntax
Though specific rules of syntax vary with some operators, the typical syntax is
variable = value operator value
where variable is the name of a variable used to store a value, value is a number, string, value, expression, or property of an object, and operator is one of the operators described here.
Description
The sections that follow group JavaScript into classes based on the type of operation performed.
Arithmetic operators act on numbers. Table 2 lists the arithmetic operators offered in JavaScript and provides examples of each.
Table 2: JavaScript's Arithmetic Operators.
| Operator | Used For | Example | Equals |
| + | Addition | 1+2 | 3 |
| - | Subtraction | 12-10 | 2 |
| * | Multiplication | 2*3 | 6 |
| / | Division | 10/3 | 3.3333333333 |
| % | Modulus | 10%3 | 1 |
| ++ | Increment | x=5 x++ |
x=6 |
| -- | Decrement | x=5 x-- |
x=4 |
| - | Unary negation | -20 | negative 20 |
The modulus of a number is the remainder after the first division. For example, 10/3 results in 3, remainder 1. The modulo (%) operator returns that remainder, 1.
The increment operator, ++, is just a shortcut way of saying variable + 1. For example, the two statements that follow mean exactly the same thing. The latter one is just shorter and quicker to type: x = x + 1 x++
The same holds true for the decrement (--) operator.
With the negation operator, if the minus sign is used in front of a value without being part of a larger expression, then JavaScript assumes it indicates a negative number, just as in day-to-day arithmetic. For example x= 5 y= -x results in x being equal to 5 and y being equal to -5.
Assignment operators are used to assign a value to a variable by using the simple equal symbol (=) operator with the syntax
variablename = value
For example:
x=15 y=20 z=x+y
After all three lines have been executed, the variable x contains the number 15, the variable y contains 20, and the variable z contains 35 (the sum of 15+20).
Some shorthand operators exist that you can use to do some arithmetic and assign the result to a value in one fell swoop. Those operators are shown in Table 3.
Table 3: JavaScript's Assignment Operators.
| Operator | Example | Means |
| += | x+=y | x=x+y |
| -= | x-=y | x=x-y |
| *= | x*=y | x=x*y |
| /= | x/=y | x=x/y |
| % | x%=y | x=x%y |
Bitwise operators treat their values as binary numbers (1's and 0's) rather than as numeric values. You may never need to use these so don't waste too much energy trying to memorize them. But just in case you do come across a bitwise operator, Table 4 shows what each one does.
Table 4: JavaScript's Bitwise Operators.
| Operator | Action | Example |
| & | bitwise AND | 10&3=2 |
| | | bitwise OR | 10|3=11 |
| ^ | bitwise exclusive OR | 10^3=9 |
| << | left shift | 10<<3=80 |
| >> | Sign-propagating right shift | 10>>3=1 |
| >>> | Zero-fill right shift | 10>>>3=1 |
Some shortcut operators also exist for bitwise assignments, as listed in Table 5.
Table 5: JavaScript's Bitwise Assignment Operators.
| Operator | Example | Means |
| <<= | x<<=y | x=x<<y |
| >>= | x>>=y | x=x>>y |
| >>>= | x>>>=y | x=x>>>y |
| &= | x&=y | x=x&y |
| ^= | x^=y | x=x^y |
| |= | x|=y | x=x|y |
Comparison and logical operators compare two values and return either true or false. Table 6 lists the comparison operators and Table 8 lists the logical operators.
Table 6: JavaScript's Comparison Operators.
| Operator | Meaning | Example |
| == | is equal to | 10==3 is false |
| != | does not equal | 10!=3 is true |
| > | is greater than | 10>3 is true |
| >= | is greater than or equal to | 10>=3 is true |
| < | is less than | 10<3 is false |
| <= | is less than or equal to | 10<=3 is false |
Table 8: JavaScript's Logical Operators.
| Operator | Meaning | If x=10 and y= 5 thenÖy |
| && | AND | (x = 10) && (y < 10) is true |
| || | OR | (x=10) || (y=10) is true |
| ! | NOT | x !=y is true |
JavaScript also contains a conditional operator that assigns a value to a variable based on some condition. The operators for a conditional expression are ? and : using this syntax:
myvar = (condition) ? value1 : value2
For example, the conditional expression that follows is a shorthand way of saying "If the variable named gender contains F, then put the string 'Ms.' in the variable named salutation. If the variable named gender does not contain F, then put the string 'Mr.' into the variable named salutation":
salutation = (gender=="F") ? "Ms." : "Mr."
JavaScript operators follow the standard order of precedence or operator precedence. But you can override natural precedence with parentheses. For example
5+3*10
equals 35 because the multiplication is naturally carried out first (according to the rules of precedence). That is, the machine first evaluates 10*3, which equals 30, and then adds 5 to that to get 35.
This expression has a different result:
(5+3)*10
This expression results in 80 because the parentheses force the addition to be carried out first (5+3 equals 8). That result then is multiplied by 10 to result in 80. Table 8 shows the order of precedence of the operators, from highest to lowest, when you do not use parentheses in an expression. Operators at the same level of precedence within an expression are carried out in left-to-right order.
Table 8: JavaScript Operators Order of Precedence.
Action Operator(s) call, member (),[] negation/increment ! ~- ++ -- multiply/divide * / % addition/subtraction + - bitwise shift << >> >>> comparison < <= > >= equality == != bitwiseAND & bitwise XOR ^ bitwise OR | logical AND && logical OR || conditional ?: assignment = += -= *= /= %= <<= >>= >>>= &= ^= |= comma ,
String operators are chunks of text, such as Hello (rather than a number such as 10). Unlike numbers, you cannot add, subtract, multiply, and divide strings. Example: 2*3 = 6 is fine. But what is "Hello" * "There"? The question makes no sense.
You can, however, concatenate strings, which is a fancy name for "stick them together." You use the + operator to concatenate strings. Here is an example where we create three variable, x, y, and z, all of which contains strings:
x="Hello"
y="There"
z=x+y
The result is that z now contains HelloThere.
Why is there no space between the words? Because to a computer, strings are just meaningless strings of characters. To a computer, the string hello in no more meaningful than the string bvhdsz. They are both just strings. If you want to add a space between the words, you can pop a space into the expression. A literal space would be a space enclosed in quotation marks (" "). Hence, this series of commands x="Hello" y="There"
z=x+" "+y
results in z containing Hello There. A shortcut operator also exists for string concatenation +=. For example
x="Hello" y="There"
x+=" "+y
in which case x equals itself with a space and y tacked on. Ergo, x then contains Hello There.

|
|
![]() |