Other Types of Operators
We are going to read about the other types of operators which play an important role in JavaScript. These operators are assignment, bitwie, ternary, and type operators. Also, all the concepts will be thoroughly explained with examples to help you have a better understanding of the concepts.
1. Assignment Operators
The next operator is assignment operators in JavaScript. When you assign a value to a operand, then that is known as assignment operators.
For example:
let x = 10;
where x has been assigned a value of 10; and
let y = 20;
where x has been assigned a value of 20, and
let z = x + y;
where z has been assigned the value of addition of x and y
2. Bitwise Operators
Bitwise operators in JavaScript are used to perform bit-level operations like addition, subtraction, multiplication, and division in bit-level.
& (AND)
It is a binary operator which returns 1(true) when both the operands are 1(true) else 0 (false). For example:
A = 1;
B = 0;
Y = A & B;
| (OR)
It is a binary operator which returns 1(true) when anyone of the operand is 1(true) else 0 (false). For example:
A = 1;
B = 0;
Y = A | B;
~ (NOT)
It is a binary operator which returns opposite of what is given. For example:
A = 1;
Y =~ A;
^ (XOR)
It is a binary operator which returns 1(true) when both the operands are different and 0 (false) when both are same. For example:
A = 1;
B = 0;
Y = A ^ B;
<< (Left Shift)
The << operator moves all the bits in the first operand to the left by the number given in the second operand.
>> (Right Shift)
The >> operator moves the left operands’s value by the number specified by the right operand.
>>> (Zero Fill Right Shift)
The >>> operator functions the same as right shift operator, except that the bits are shifted on the left are always zero.
3. Ternary Operators
Ternary Operator in JavaScript are another type of operator which contains three operands. It is the simplified operator of if/else. It is denoted by “?”. It evaluates a condition and executes a block of code based on the given condition.
Syntax:
condition ? expression1 : expression 2
For Y = ? A : B, if the condition is true then Y=A else Y=B.
For example:
let x = 5;
let y = (x != 6)? “true” : “false”;
Returns true since x whose value is 5 is not equal to 6
4. Type Operators
Type operator in JavaScript tells about the data type of an object.
Typeof
Typeof operator in JavaScript returns the data type of an object, where the objet can be any object, function, or variable.
Syntax:
typeof variable;
Example:
let name = "John";
let age = 30;
let isStudent = true;
console.log(typeof name); //output: "string"
console.log(typeof age); // output: "number"
console.log(typeof isStudent); // output: "boolean"
Instanceof
Instanceof operator in JavaScript returns true if an object is an instance of an object type.
Syntax:
instanceof variable;
let str = "hello";
let arr = [1, 2, 3];
let obj = { foo: "bar" };
console.log(str instanceof String); // false
console.log(arr instanceof Array); // true
console.log(obj instanceof Object); // true
Conclusion
In this module, we have discussed about the other types operators in JavaScript and sub-types. Also, every operator type is explained with the help of examples to make it more clear to understand. Moving ahead, you’ll be reading about the loops in JavaScript.