Operators in JavaScript

Operators in JavaScript, as in PHP, can involve mathematics, changes to strings, and comparison and logical operations (and, or, etc.). JavaScript mathematical operators look a lot like plain arithmetic; for instance, the following statement outputs 16:

document.write(14 + 2)

The following sections teach you about the various operators.

Arithmetic Operators

Operator Description Example
+ Addition j+12
Subtraction j-22
* Multiplication j*7
/ Division j/3.14
% Modulus (division remainder) j%6
++ Increment ++j
Decrement –j

Comparison Operators

Comparison operators are generally used inside a construct such as an if statement where you need to compare two items. For example, you may wish to know whether a variable you have been incrementing has reached a specific value, or whether another variable is less than a set value, and so on.

Operator Description Example
== Is equal to j == 42
!= Is not equal to j != 17
> Is greater than j > 0
< Is less than j < 100
>= Is greater than or equal to j >= 23
<= Is less than or equal to j <= 13
=== Is equal to (and of the same type) j === 56
!== Is not equal to (and of the same type) j !== ‘1’

Logical Operators

Unlike PHP, JavaScript’s logical operators do not include and and or equivalents to && and ||, and there is no xor operator.

Operator Description Example
&& And j == 1 && k == 2
|| Or j < 100 || j > 0
! Not ! (j == k)