Introduction to Javascript Operators
Javascript is a Very Popular Language due to its beauty and ease of use. It is easy to get started with javascript. But it is very difficult to master the language. For Example You don't know all of the JS operators. Do you?
Types of Operators
There are many types of operators in Javascript.
According to MDN Web Docs Those are:
- Assignment operators
- Comparison operators
- Arithmetic operators
- Bitwise operators
- Logical operators
- String operators
- Conditional/Ternary operator
- Comma operator
- Unary operators
- Relational operators
Now you know Most of them, So I will tell you about the most unknown operators.
1. Binary Shift Operators
If You have ever coded in C/C++
then you are likely you have used or at least heard about binary shift operators.
Binary Shift Operators are used while dealing with Binary Numbering System. May be that is the reason you didn't knew about existence of these operators. All
These Operators also exists in Javascript. These are Left Shift (<<
), Sign-propagating Right Shift (>>
) and Zero-fill Right Shift (>>>
) Operators in JS.
Left Shift (<<
)
Lets Take A Number Like 212 for Example.
Javascript stores numbers in 64 bits.
Bitwise Operators use 32 bits.
π
Maybe Taking 32 Bits is Not An Good Idea. So Lets Just Work With 16 Bits
212 = 0000 0000 1101 0100
Now if We Do 212 << 1
Then it will shift the 11010100
towards left by an adding an 0
at last. So It Will be 0000 0001 1010 1000
which is 424.
console.log(212 << 1); //Prints 424
console.log(123 << 2); //It Prints 492
/* You Can Try it Now in Your Browser's Developer Console */
Sign-propagating Right Shift (>>
)
It works same as the previous one but it shifts the bits towards right.
let a = 212; // 212 = 0000 0000 1101 0100
a = a >> 1; // 0000 0000 0110 1010 [106]
console.log(a); // 106
console.log(343 >> 2); //85 works like this too.
console.log(-132 >> 2); //-33
/*it keeps the sign, for which it is called "sign-propagating"*/
Zero-fill Right Shift (>>
)
It is same as Sign-propagating Right Shift only difference being that it also shifts the sign bit. For that reason it works same in positive number but gives different result in negative numbers.
console.log(343 >>> 2); //85 works same
console.log(-132 >>> 2); //1073741791 it doesn't work same as >>
You Can Read More About it at MDN Docs.
2. Binary Bitwise Operators
Again These Operator is used in Binary Calculations. We Have Bitwise AND (&
), Bitwise OR (|
) and Bitwise XOR (^
).
These all calculates the bits at each place of the given number.
0000 0001 0011 0100
0000 0100 0010 1001
The Top Digit and The Bottom digit at same place is taken. And Calculated According to the Operator. These follows the boolean algebra rules.
Like For AND (&
)
0 & 0 = 0
1 & 0 = 0
0 & 1 = 0
1 & 1 = 1
For OR (|
)
0 | 0 = 0
1 | 0 = 1
0 | 1 = 1
1 | 1 = 1
For XOR (^
)
0 ^ 0 = 0
1 ^ 0 = 1
0 ^ 1 = 1
1 ^ 1 = 0
After These Calculations we get :
let a = 15; //1111
let b = 9; //1001
console.log(a & b); // 1111 & 1001 = 1001 (9)
console.log(a | b); // 1111 | 1001 = 1111 (15)
console.log(a ^ b); // 1111 ^ 1001 = 0110 (6)
3. Bitwise Not Operator
Bitwise Not Operator (~
) Changes the 1
to 0
and vice-versa for any given number. It Changes all 32 bits.
let a = 12; //1100
console.log(~a); //(-13)
/*
You Expected 1100 to be 0011 but after altering all 32 bits we get
1111 1111 ... 1111 0011
which is -13
*/
Conclusion
All Of These Unknown Operators come From Binary System. So It is a good excuse to learn binary at this moment. Because binary is the thing that makes the computers possible.
Hope You Liked My First Blog Post.
Will See You Later.
Bye π
Top comments (0)