Home > Web Front-end > JS Tutorial > body text

What does !! in js mean?

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

The !! operator in JavaScript converts any value to a boolean: true for non-zero, non-empty strings, and objects, and false for 0, null, undefined, NaN, and empty strings. It is used to cast to boolean, invert boolean and implement lazy loading.

What does !! in js mean?

The meaning of !! operator in JS

!! operation in JS operator is called the double negation operator in JavaScript. It does the following:

Convert any value to a boolean

Take any value as input and the !! operator will convert it Is a Boolean value:

  • For non-zero, non-empty strings and any object, the result is true.
  • For 0, null, undefined, NaN, and the empty string, the result is false.

Usage Examples

Here are some examples of usage of the !! operators:

console.log(!!0); // false
console.log(!!1); // true
console.log(!!''); // false
console.log(!!"Hello"); // true
console.log(!!null); // false
console.log(!!undefined); // false
console.log(!!NaN); // false
Copy after login

Purpose

!! operator is mainly used for the following purposes:

  • Forcing to a Boolean value: can be used Forces any value to be converted to a boolean for use in situations where an explicit boolean value is required.
  • Invert a Boolean value: can be used to invert a Boolean value, such as converting true to false and vice versa.
  • Implementing lazy loading: can be used to implement lazy loading, where the content of an element is only loaded when it is displayed.

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!