JavaScript Operators

Types of Operators in JavaScript’s –This chapter describes JavaScript’s expressions and operators, including assignment, comparison, arithmetic, bitwise, logical, string, ternary and more.


Types of Operators in JavaScript’s

  • Assignment operators
  • Comparison operators
  • Arithmetic operators
  • Bitwise operators
  • Logical operators
  • String operators
  • Conditional (ternary) operator
  • Comma operator
  • Unary operators
  • Relational operators

JavaScript has both binary and unary operators, and one special ternary operator, the conditional operator. A binary operator requires two operands, one before the operator and one after the operator:


   4 + 2
   X * Y

A unary operator requires a single operand, either before or after the operator:


   X++
   ++X


JavaScript Assignment Operators

Assignment operators assign values to JavaScript variables.

OperatorExampleMeaning
Assignment (=)x = yx = y
Addition assignment (+=)x += yx = x + y
Subtraction assignment (-=)x -= yx = x - y
Multiplication assignment (*=)x *= yx = x * y
Division assignment (/=)x /= yx = x / y
Remainder assignment (%=)x %= yx = x % y
Left shift assignment (<<=)x <<= yx = x << y
Right shift assignment (>>=)x >>= yx = x >> y
Unsigned right shift assignment (>>>=)x >>>= yx = x >>> y
Bitwise AND assignment (&=)x &= yx = x & y
Bitwise XOR assignment (^=)x ^= yx = x ^ y
Bitwise OR assignment (|=)x |= yx = x | y
Exponentiation assignment (**=)x **= yx = x ** y
Logical AND assignment (&&=)x &&= yx && (x = y)
Logical OR assignment (||=)x ||= yx || (x = y)
Logical nullish assignment (??=)x ??= yx ?? (x = y)

JavaScript Comparison Operators

Comparison operators are used in logical statements to determine equality or difference between variables or values.

OperatorDescriptionExamples returning true
Equal (==)Returns true if the operands are equal.x == 3
Not equal (!=)Returns true if the operands are not equal.x != 4
Strict equal (===)Returns true if the operands are equal and of the same type. x === 3
Strict not equal (!==)Returns true if the operands are of the same type but not equal, or are of different type.x !== "lion"
Greater than (>)Returns true if the left operand is greater than the right operand.x > y
Greater than or equal (>=)Returns true if the left operand is greater than or equal to the right operand.x >= y
Less than (<)Returns true if the left operand is less than the right operand.x < y
Less than or equal (<=)Returns true if the left operand is less than or equal to the right operand.x <= y

JavaScript Arithmetic Operators

Arithmetic operators perform arithmetic on numbers (literals or variables).

OperatorDescription
+Addition
-Subtraction
*Multiplication
**Exponentiation (ES2016)
/Division
%Modulus (Remainder)
++Increment
--Decrement

let x = 100 + 50;
let x = a + b;


JavaScript Bitwise Operators

Bit operators work on 32 bits numbers.

Any numeric operand in the operation is converted into a 32 bit number. The result is converted back to a JavaScript number.

The following table summarizes JavaScript’s bitwise operators.

OperatorUsageDescription
Bitwise ANDa & bReturns a one in each bit position for which the corresponding bits of both operands are ones.
Bitwise ORa | bReturns a zero in each bit position for which the corresponding bits of both operands are zeros.
Bitwise XORa ^ bReturns a zero in each bit position for which the corresponding bits are the same. [Returns a one in each bit position for which the corresponding bits are different.]
Bitwise NOT~ aInverts the bits of its operand.
Left shifta << bShifts a in binary representation b bits to the left, shifting in zeros from the right.
Sign-propagating right shifta >> bShifts a in binary representation b bits to the right, discarding bits shifted off.
Zero-fill right shifta >>> bShifts a in binary representation b bits to the right, discarding bits shifted off, and shifting in zeros from the left.
OperatorDescriptionExampleSame asResultDecimal
&AND5 & 10101 & 00010001 1
|OR5 | 10101 | 00010101 5
~NOT~ 5 ~01011010 10
^XOR5 ^ 10101 ^ 00010100 4
<<left shift5 << 10101 << 11010 10
>>right shift5 >> 10101 >> 10010  2
>>>unsigned right shift5 >>> 10101 >>> 10010  2

JavaScript Logical Operators

Logical operators are used to determine the logic between variables or values.

Logical operators are typically used with Boolean (logical) values; when they are, they return a Boolean value. However, the && and || operators actually return the value of one of the specified operands, so if these operators are used with non-Boolean values, they may return a non-Boolean value.

OperatorDescriptionExample
&&AND( 5 > 1 && 1 < 5 ) True
||OR( 10==1 || 5 == 10 ) False
!NOT!( 5 == 10 ) True

JavaScript String Operators

In addition to the comparison operators, which can be used on string values, the concatenation operator (+) concatenates two string values together, returning another string that is the union of the two operand strings.


console.log('my ' + 'string'); // console logs the string "my string".

The shorthand assignment operator += can also be used to concatenate strings.


var mystring = 'alpha';
mystring += 'bet'; // evaluates to "alphabet" and assigns this value to mystring.


JavaScript Conditional (ternary) Operator

The conditional operator is the only JavaScript operator that takes three operands. The operator can have one of two values based on a condition.

Syntax :


variablename = (condition) ? value1:value2 

Example :


let voteable = (age < 18) ? "Too young" : "Old enough";

If the variable age is a value below 18, the value of the variable voteable will be “Too young”, otherwise the value of voteable will be “Old enough”.


JavaScript Unary operators

Unary operators work on one value.

Unary OperatorsNameMeaning
+xUnary PlusConvert a value into a number
-xUnary MinusConvert a value into a number and negate it
++xIncrement Operator (Prefix)Add one to the value
–xDecrement Operator (Prefix)Subtract one from the value
x++Increment Operator (Postfix)Add one to the value
x–Decrement Operator (Postfix)Subtract one from the value

I hope you like this Types of Operators in JavaScript’s article.

Also Read :