loading

JS Operators

Javascript operators are used to perform different types of mathematical and logical computations.

Examples:

The Assignment Operator = assigns values

The Addition Operator + adds values

The Multiplication Operator * multiplies values

The Comparison Operator > compares values

JavaScript Assignment

The Assignment Operator (=) assigns a value to a variable:

Assignment Examples

				
					let x = 10;
				
			
				
					// Assign the value 5 to x
let x = 5;
// Assign the value 2 to y
let y = 2;
// Assign the value x + y to z:
let z = x + y;
				
			

JavaScript Addition

The Addition Operator (+) adds numbers:

Adding

				
					let x = 5;
let y = 2;
let z = x + y;
				
			

JavaScript Multiplication

The Multiplication Operator (*) multiplies numbers:

Multiplying

				
					let x = 5;
let y = 2;
let z = x * y;
				
			

Types of JavaScript Operators

There are several types of JavaScript operators:

  • Arithmetic Operators
  • Assignment Operators
  • Comparison Operators
  • String Operators
  • Logical Operators
  • Bitwise Operators
  • Ternary Operators
  • Type Operators

JavaScript Arithmetic Operators

Arithmetic Operators perform arithmetic on numbers.

Arithmetic Operators Example

				
					let a = 3;
let x = (100 + 50) * a;
				
			
OperatorDescription
+Addition
Subtraction
*Multiplication
**Exponentiation (ES2016)
/Division
%Modulus (Division Remainder)
++Increment
Decrement

JavaScript Assignment Operators

Assignment operators set values for JavaScript variables.

The Addition Assignment Operator (+=) adds a value to a variable.

Assignment

				
					let x = 10;
x += 5;
				
			
OperatorExampleSame As
=x = yx = y
+=x += yx = x + y
-=x -= yx = x – y
*=x *= yx = x * y
/=x /= yx = x / y
%=x %= yx = x % y
**=x **= yx = x ** y

JavaScript Comparison Operators

OperatorDescription
==equal to
===equal value and equal type
!=not equal
!==not equal value or not equal type
>greater than
<less than
>=greater than or equal to
<=less than or equal to
?ternary operator

JavaScript String Comparison

All of the comparison operators mentioned above can also be applied to strings:

Example

				
					let text1 = "A";
let text2 = "B";
let result = text1 < text2;
				
			

Strings are compared alphabetically:

Example

				
					let text1 = "20";
let text2 = "5";
let result = text1 < text2;
				
			

JavaScript String Addition

The + operator can also be used to add (concatenate) strings:

Example

				
					let text1 = "John";
let text2 = "Doe";
let text3 = text1 + " " + text2;
				
			

You can also use the += assignment operator to add (concatenate) strings:

Example

				
					let text1 = "What a very ";
text1 += "nice day";
				
			

The output of text1 will be:

				
					What a very nice day
				
			

Adding Strings and Numbers

Adding two integers returns the sum; however, adding a number and a string returns a string:

Example

				
					let x = 5 + 5;
let y = "5" + 5;
let z = "Hello" + 5;
				
			

The result of x, y, and z will be:

				
					10
55
Hello5
				
			

JavaScript Logical Operators

OperatorDescription
&&logical and
||logical or
!logical not

JavaScript Type Operators

OperatorDescription
typeofReturns the type of a variable
instanceofReturns true if an object is an instance of an object type

 

JavaScript Bitwise Operators

Bit operators function with 32-bit numbers.

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

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
Share this Doc

JS Operators

Or copy link

Explore Topic