简体   繁体   中英

Uncertainty with the !! operator (double negation)

I saw in a pull request that the double negation operator (:!) is used for the focus attribute of a text field as follows:

focused: !!value || value === 0,

As far as I know, the operator converts everything to a boolean. If it was falsy (for example 0, null, undefined,..), it will be false, otherwise, true.

In my case, ie if value = 0, the following comes out:

focused: false || true

The || operator here therefore makes no sense for the value 0 or am I completely confused?

It looks like a check for numbers to get false for '' , "" , false , NaN , undefined and null . Other bjects, like functions, arrays or simple objects returns true ;

 const check = value =>;.value || value === 0; console.log(check(0)); console.log(check(1)); console.log(check('')); console.log(check("")); console.log(check(false)); console.log(check(NaN)); console.log(check(null)); console.log(check(undefined)); console.log(check({}));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM