Javascript Logical Operation (a === true || a === false)

I found this piece of code inside moment.js . Why do we have such a check?

if (locale === true || locale === false) { strict = locale; locale = undefined; } 
+5
source share
4 answers

This is used to ensure that locale is only used as a strict variable / parameter, if it is actually a boolean. Looking at this code, it looks like it probably shuffles the parameters of the function depending on whether additional ones were specified. (In this case, locale will be optional until strict .)

+8
source

It checks if locale is true or false instead of any other falsifications (undefined, null, '', NaN, 0) or right values

+3
source

Perhaps I do not understand your context. But, based on the code, I think they want to verify that the locale value is logical.

 if (locale === true || locale === false) {...} 

So you can see that they use triple equal to validation (the locale may be of a different type).

0
source

"==" means equal in value, but not type, but "===" means equal in value and type.

 var x = 7; x == "7" // returns true x === "7" // returns false as x is not a string x === 7 // returns true as they are both equal in value and type 

For the code you provided, it looks like it checks to see if the locale value and type are boolean, so triple comparison is used in the same way, and also why it contains both true and false.

0
source

Source: https://habr.com/ru/post/1274705/


All Articles