What are logical assignment operators (&&=, ||=, ??=)?
Logical assignment operators in JavaScript include &&=, ||= and ??=, which are assigned when the variable is true, false, null, or undefined, respectively. 1. &&=: Assign value only when the variable is true, and it is suitable for updating its content when there is already a valid value; 2. ||=: Assign value only when the variable is false, it is often used to set default values or supplement missing data; 3. ??=: Assign value only when the variable is null or undefined, it is suitable to safely initialize variables without overwriting legal values. These operators are suitable for modern environments and are recommended for concise assignment scenarios to improve code clarity, but pay attention to readability and adaptability of teamwork.
Logical assignment operators ( &&=
, ||=
, ??=
) are newer features in JavaScript. They combine logical operations and assignment operations to make the code simpler and the intent clearer. These operators are particularly convenient when handling conditional assignments, and are especially suitable for further processing when the variable already has a value.
1. &&=
: Assign value only when the variable is true
The function of this operator can be understood as:
If the variable on the left is a "truthy", then assign the value on the right to it.
For example:
let x = 5; x &&= 10; console.log(x); // Output 10
Here, because x
is 5
(a true value), x
is assigned to 10
.
But if the initial value is a false value (such as 0
, null
, undefined
, false
, ''
etc.), the assignment will not be performed:
let y = 0; y &&= 7; console.log(y); // Output 0
Common uses:
- Modify a value if it exists.
- Avoid redundant operations on invalid values.
2. ||=
: Assign value only if the variable is false
This operator means:
If the variable on the left is a "false value", then assign the value on the right to it.
Example:
let a = null; a ||= 'default'; console.log(a); // Output 'default'
And if the variable already has a valid value, it will not be changed:
let b = 'already value'; b ||= 'alternative value'; console.log(b); // Output 'already value'
Common uses:
- Set default values, especially function parameters or configuration items.
- Supplement missing data fields.
3. ??=
: Assign value only if the variable is null or undefined
This is the most "strict" one, and its rules are:
The right value will only be assigned to it if the value on the left is
null
orundefined
.
Unlike the other two, it does not care about other false values (such as empty strings, 0, false), but only focuses on whether it is really "undefined".
Example:
let c = undefined; c ??= 'supplementary value'; console.log(c); // Output 'supplementary value' let d = ''; d ??= 'supplementary value'; console.log(d); // Output ''
Common uses:
- Handles possible undefined object properties.
- Safely initialize variables without overwriting legal values (including 0 and empty strings).
Recommendations and precautions for use
- These operators are suitable for modern browsers and Node.js environments (ES2020).
- Don't overuse, especially in teamwork, make sure everyone is familiar with these syntaxes.
- Try to use it in concise assignment scenarios rather than complex logical judgments.
For example, the following situations are very suitable:
- Set default values for variables (especially object properties)
- Decide whether to update based on whether the current value exists
- Clearly express the intention of "assigning values only under certain conditions"
Basically that's it. The rational use of logical assignment operators can make the code cleaner, but you should also pay attention to readability and avoid writing confusing "one-line code".
The above is the detailed content of What are logical assignment operators (&&=, ||=, ??=)?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

In the C language, there is no root operator. The built-in function "sqrt()" is used to open the root, and the syntax "sqrt(value x)" is used; for example, "sqrt(4)" is to perform the square root operation on 4. , the result is 2. sqrt() is a built-in root operation function in C language. Its operation result is the arithmetic square root of the function variable; this function can neither operate negative values nor output imaginary results.

For Golang developers, "invaliduseof...operator" is a common error. This error usually occurs when using variable-length parameter functions. It will be detected at compile time and indicate which parts have problems. This article will introduce how to solve this error. 1. What is a variable-length parameter function? A variable-length parameter function is also called a variable-parameter function. It is a function type in the Golang language. Using variable-length parameter functions, you can define multiple ones as follows

In PHP, the "==" symbol is a comparison operator that can compare whether two operands are equal. The syntax is "operand 1 == operand 2". The "==" operator compares and tests whether the variable on the left (expression or constant) has the same value as the variable on the right (expression or constant); it only compares the values of the variables, not the data types. If the two values are the same, it returns a true value; if the two values are not the same, it returns a false value.

Python is widely used in a wide range of fields with its simple and easy-to-read syntax. It is crucial to master the basic structure of Python syntax, both to improve programming efficiency and to gain a deep understanding of how the code works. To this end, this article provides a comprehensive mind map detailing various aspects of Python syntax. Variables and Data Types Variables are containers used to store data in Python. The mind map shows common Python data types, including integers, floating point numbers, strings, Boolean values, and lists. Each data type has its own characteristics and operation methods. Operators Operators are used to perform various operations on data types. The mind map covers the different operator types in Python, such as arithmetic operators, ratio

In Java, "%" means remainder. It is a binary arithmetic operator that can perform division operations and obtain the remainder. The syntax is "operand 1 % operand 2". The operand of the remainder operator "%" is usually a positive integer or a negative number or even a floating point number. If a negative number participates in this operation, the result depends on whether the previous number is positive or negative.

The += operator is used to add the value of the left operand to the value of the right operand and assign the result to the left operand. It is suitable for numeric types and the left operand must be writable.

In PHP, you can use the "%" and "==" operators to determine whether two numbers are divisible; you only need to use the "%" operator to divide the two numbers to get the remainder, and then use the "==" operator Just judge whether the obtained remainder is 0. The syntax is "Number 1 % Number 2 == 0". If it is 0, it can be divisible. If it is not 0, it cannot be divisible.

In Go language, operators are evaluated in order from high to low precedence. The priority order of common operators: 1. Parentheses: () (highest priority, used to force the order of operations); 2. Unary operators; 3. Multiplicative operators; 4. Additive operators; 5. Shift operator; 6. Bitwise operator; 7. Comparison operator; 8. Logical operator; 9. Conditional operator (ternary operator); 10. Assignment operator, etc.
