Explore best practices, tutorials, case studies, and insights on leveraging AWS’s vast ecosystem to build, deploy, and manage applications in the cloud
The Design Pattern category explores reusable solutions to common software design challenges, helping developers write efficient, maintainable, and scalable code
The Security category focuses on best practices, tools, and frameworks essential for protecting applications, data, and infrastructure in an increasingly digital world
Comprehensive Guide to Token-Based Authentication & Secure Access Control
Master token-based authentication, OAuth2, API security, and access control strategies like RBAC and ABAC for secure healthcare applications. Learn best practices for token storage, rotation, MFA, and more.
LIKE vs Full-Text Search: SQL Performance and Use Cases
Explore the differences between SQL’s LIKE operator and Full-Text Search. Learn their syntax, performance, use cases, and advanced features for optimizing database queries
10 Advanced JavaScript Tricks Every Developer Should Master
Discover 10 advanced JavaScript techniques, including tail call optimization, currying, proxies, debouncing, throttling, and more. Learn how to write efficient, maintainable, and optimized code for modern web development.
Mastering the Mysteries of JavaScript Syntax: Discover the Secrets Behind These Symbols
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…
2 min read
388 Views
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.
Related Posts
Learn how distributed caching with Redis can boost backend performance and scalability. This guide covers setup, caching strategies, and a step-by-step technical demo with benchmarks.
Discover 6 powerful caching strategies to enhance backend performance and scalability. From in-memory and distributed caching to hybrid solutions, learn how to implement effective caching in your backend architecture for faster response times and optimized resource use
Introduction In high-demand real-time applications like ride-hailing or booking platforms, maintaining a stable connection between the client and server is crucial. Load balancing for WebSocket connections presents unique challenges, especially…
Discover 10 advanced JavaScript techniques, including tail call optimization, currying, proxies, debouncing, throttling, and more. Learn how to write efficient, maintainable, and optimized code for modern web development.
Explore the 7 key types of loading in frontend development, including eager, lazy, code splitting, skeleton, progressive, infinite scrolling, and shimmer loading. Learn how each works, why they matter, best practices, and React.js code examples to optimize your web performance
Prepare for your Senior Website Frontend Engineer interview with these 10 advanced questions and answers covering component architecture, state management, performance optimization, accessibility, and more. Ace your next interview!
Subscribe to our newsletter
Get the latest posts delivered right to your inbox