Home > Web Front-end > JS Tutorial > What does !! in js mean?

What does !! in js mean?

下次还敢
Release: 2024-05-01 09:15:27
Original
921 people have browsed it

The !! operator in JS: used to convert a value to a Boolean value. Operation: Perform logical NOT operation on the value. Then perform a logical NOT operation on the result. Result: true: when the value is not false, not the empty string, not null, not undefined. False: When the value is true, empty string, null, undefined. Purpose: Convert any value to a Boolean value. Forces an explicit conversion to a boolean value. Eliminate uncertain values.

What does !! in js mean?

!! in JS

In JavaScript, the "!!" operator is a logical negation ( NOT) operator, used to convert a value to a Boolean value. Its function is double negation, that is, after performing a logical NOT operation on the value, perform a logical NOT operation again.

How to use

Syntax:

<code>!!<value></code>
Copy after login

where can be any JavaScript value.

Operation result

  • If is true (true), then !! is true (true).
  • If is false, then !! is false.
  • If is an empty string, null, or undefined, then !! is false.

The purpose of double negation

Double negation operator!! Mainly used for the following purposes:

  • Convert any value to a Boolean value: It can convert any value (number, string, object, etc.) into a Boolean value, making it suitable for Boolean operations.
  • Force Boolean value: It can force the true value (true) or false value (false) to be explicitly converted to a Boolean value.
  • Eliminate undefined values: It can eliminate the concept of "undefined values" in JavaScript, because !! will always return true or false.

Code Example

<code>console.log(!!true); // true
console.log(!!false); // false
console.log(!!0); // false
console.log(!!1); // true
console.log(!!''); // false
console.log(!!'abc'); // true</code>
Copy after login

The above is the detailed content of What does !! in js mean?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template