JavaScript has a rich set of operators that make it both powerful and versatile. Some of these operators are well known, while others may be new or lesser-used. Explaining what they are, how to use them, and why they are essential.
!=
|#=
??`
&=
~~
1. ?= (Safe Assignment Operator)
What is It?
The safe assignment operator ?=
assigns a value only if the variable is null or undefined.
How to Use It?
let x;
x ?= 10;
console.log(x); // Output: 10 (since x was undefined)
Why Use It?
This operator avoids unnecessary overwriting of variables that already hold a valid value.
Best Practice
Use ?= when you want to safely assign default values without overwriting existing ones.
2. ??= (Nullish Assignment Operator)
What is It?
The assignment operator = assigns a value to a variable.
How to Use It?
let z = null;
z ??= 15;
console.log(z); // Output: 15 (since z was null)
Why Use It?
It ensures that you only assign a value when the variable is nullish, avoiding the pitfalls of falsy values like 0 or “”.
Best Practice
Use ??= when you want to set a fallback value for a potentially null or undefined variable.
3. &= (Bitwise AND Assignment Operator)
What is It?
The &= operator performs a bitwise AND operation and assigns the result back to the variable.
How to Use It?
let num = 5; // (101 in binary)
num &= 3; // (011 in binary)
console.log(num); // Output: 1 (bitwise AND result)
Why Use It?
Bitwise operations are fast and useful in low-level programming, for tasks like managing flags or working with binary data.
Best Practice
Use &= for bitwise operations only when necessary, typically in performance-sensitive environments.
4. ~~ (Double NOT Bitwise Operator)
What is It?
The ~~ operator is a shorthand for converting a floating-point number to an integer.
How to Use It?
let decimal = 4.8;
let integer = ~~decimal;
console.log(integer); // Output: 4
Why Use It?
It’s a faster alternative to Math.floor() when you need to truncate a number without rounding.
Best Practice
Use ~~ when you need a quick and efficient way to truncate numbers, particularly in performance-critical code.
5. |= (Bitwise OR Assignment Operator)
What is It?
The |= operator performs a bitwise OR operation and assigns the result to the variable.
How to Use It?
let a = 5; // (101 in binary)
a |= 3; // (011 in binary)
console.log(a); // Output: 7 (bitwise OR result)
Why Use It?
It’s useful when manipulating bits in low-level tasks like flag management.
Best Practice
Use |= in performance-critical applications that require binary operations.
6. ||= (Logical OR Assignment Operator)
What is It?
The ||= operator assigns a value to a variable only if the existing value is falsy (like null, 0, false).
How to Use It?
let b = 0;
b ||= 10;
console.log(b); // Output: 10 (since b was falsy)
Why Use It?
It simplifies assigning fallback values without using long if conditions.
Best Practice
Use ||= for assigning defaults to variables that may have falsy values, enhancing readability.
© 2024 HoaiNho — Nick, Software Engineer. All rights reserved.