JavaScript Operators
JavaScript is a client side scripting language so it has operators to do different operations. There are a few JavaScript Operators used like JavaScript Arithmetic Operators, JavaScript Assignment Operators, JavaScript String Operators, JavaScript Bitwise Operators, JavaScript Type Operators, JavaScript Logical Operators, and JavaScript Comparison Operators. Here are the definitions of JavaScript Operators with examples:
JavaScript Operators for assigning values to variables and adding them together:
var x = 5; // assigning the value 5 to variable x
var y = 2; // assigning the value 2 to variable y
var z = x + y; // assigning the value 7 to variable z (x + y)
In the above example two operators are used. The (=) sign is called the assignment operator and it is used to assign values to a variable. The (+) sign is used to add two numbers as in the above example we are adding x and y. It is an arithmetic operator because it is used for an arithmetic operation.
JavaScript Arithmetic Operators
Arithmetic operators take numerical values (literals or variables) as their operands and return a single numerical value. Here is the list of JavaScript Arithmetic operators For example:
Operator | Description | |
+ | Addition JavaScript Operator | |
– | Subtraction Operator | |
* | Multiplication Operator | |
/ | Division Operator | |
% | Modulus (Remainder) Operator | |
++ | JavaScript Increment Operator | |
— | Decrement Operator |
JavaScript Assignment Operators
The other JavaScript operator type is called JavaScript Assignment operators are used to assign values to JavaScript variables. Here is the list of Assignment operators:
Operator | Example | Same As | ||||
= | x = y | x = y (Assignment) | ||||
+= | x += y | x = x + y (Addition Assignment) | ||||
-= | x -= y | x = x – y (Subtraction Assignment) | ||||
*= | x *= y | x = x * y (Multiplication Assignment) | ||||
/= | x /= y | x = x / y (Division Assignment) | ||||
%= | x %= y | x = x % y (Modulus Assignment) | ||||
The addition assignment operator += is used to add a value to a variable.
The subtraction assignment operator –= used to subtract a value from a variable.
The multiplication assignment operator *= is used to multiply a variable.
The division assignment operator /= is used to divide by a variable.
The modulus assignment operator %= assigns a remainder to a variable.
JavaScript String Operators
JavaScript String Operator is used how to concatenate strings in JavaScript. The + (Plus) operator can also be used to concatenate strings.
Example
var txt1 = “HowTo”;
var txt2 = “Code”;
var txt3 = txt1 + ” ” + txt2;
The result of txt3 will be:
HowToCode
The += assignment operator can also be used to concatenate or add two strings:
Example
var txt1 = “Learn JavaScript as “;
txt1 += “a code academy”;
The result of txt1 will be:
Learn JavaScript as a code academy
The + (Plus) operator is also called the concatenation operator in JavaScript.
JavaScript Comparison Operators
JavaScript has another operator as comparison operators. The JavaScript comparison operators are used in logical statements to find out equality or difference among variables or values.
Operator | Description |
== | To check equal to |
=== | To check equal value and equal type |
!= | To check not equal |
!== | To check not equal value or not equal type |
> | To check greater than |
< | To check less than |
>= | To check greater than or equal to |
<= | To check less than or equal to |
? | It is a JavaScript ternary operator |
JavaScript Logical Operators
JavaScript logical operators are another type of operators that JavaScript has. JavaScript logical operators are used to find out the logic among variables or values.
Operator | Description |
&& | logical and |
|| | logical or |
! | logical not |
JavaScript Type Operators
JavaScript type operators are used to find out type of a variable or to find out instance of an object.
Operator | Description |
typeof | Returns the type of a variable |
instanceof | Returns true if an object is an instance of an object type |
JavaScript Bitwise Operators
JavaScript bitwise operators are another type of operators that are used in JavaScript. Although JavaScript stores numbers as 64 bits floating point numbers, however all bitwise operations are made on 32 bits binary numbers. All the 64 bit numbers are converted to 32 bit signed integers for performing any kind of bitwise operation. After the bitwise operation is performed the 32 bit signed number is converted back to a 64 bit number or a JavaScript number in JavaScript.
Here is a list of JavaScript bitwise operators with example code:
Operator | Description | Example | Same as | Result | Decimal |
& | AND | 5 & 1 | 0101 & 0001 | 0001 | 1 |
| | OR | 5 | 1 | 0101 | 0001 | 0101 | 5 |
~ | NOT | ~ 5 | ~0101 | 1010 | 10 |
^ | XOR | 5 ^ 1 | 0101 ^ 0001 | 0100 | 4 |
<< | Zero fill left shift | 5 << 1 | 0101 << 1 | 1010 | 10 |
>> | Signed right shift | 5 >> 1 | 0101 >> 1 | 0010 | 2 |
>>> | Zero fill right shift | 5 >>> 1 | 0101 >>> 1 | 0010 | 2 |