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
How to Handle WebSocket Load Balancing Without Losing the Connection Thread
Summary This guide explores effective strategies for handling WebSocket load balancing while preserving connection continuity. We cover key methods like sticky sessions, session storage solutions with Redis, and WebSocket authentication…
Mastering Databases: From Optimizing Queries to Distributed Systems
Databases are at the core of modern applications, from e-commerce to social platforms, powering billions of transactions every second. In this blog, we’ll explore key concepts that every software engineer…
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…
2024-09-24T20:20:18
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
7 Essential Caching Strategies to Boost Backend Performance and Scalability
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
Mastering WebSocket Load Balancing: Unlocking the Power of Sticky IPs and Session ID Routing for Seamless Connections
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…
Mastering Real-Time Scalability: Redis Solutions for Session Management and Load Balancing in High-Traffic Apps
Building a Load-Balanced Uber-like Application with Redis When building an application with similar requirements to Uber, seamless session management and real-time updates are essential. Redis can significantly enhance scalability and…
The Hidden Potential of Incremental Static Regeneration (ISR)
1. What is ISR? ISR (Incremental Static Regeneration) is a rendering strategy introduced by Next.js that enables you to build static pages and incrementally update them at runtime. This approach…
Mastering Node.js Performance: Unlock the Power of Worker Threads and Clustering
Node.js is known for being single-threaded, leveraging the event loop to handle asynchronous operations efficiently. However, handling CPU-intensive tasks or utilizing multiple CPU cores requires more advanced approaches: Worker Threads…
Master the Top 5 Essential JavaScript Design Patterns Every Developer Should Know
1. Singleton Pattern What is it? A design pattern that restricts the instantiation of a class to a single instance. When was it created? Introduced as part of the GoF…
Subscribe to our newsletter
Get the latest posts delivered right to your inbox