I found this piece of code inside moment.js. Why would we have this kind of check?
if (locale === true || locale === false) {
strict = locale;
locale = undefined;
}
The strict equality operator ( === ) checks whether its two operands are equal, returning a Boolean result. Unlike the equality operator, the strict equality operator always considers operands of different types to be different.
The main difference between the == and === operator in javascript is that the == operator does the type conversion of the operands before comparison, whereas the === operator compares the values as well as the data types of the operands.
Logic operators are used to find the logic between variables in JavaScript. There are three logical operators in JavaScript: || (OR), && (AND), ! (NOT).
The logical and ( && ) and or ( || ) are logical operators in JavaScript. Normally, you're using these operators on booleans: true && true // => true. true && false // => false.
This is used to ensure that locale
is only used as the strict
variable/parameter if it's actually a boolean value. Looking at that code, it looks like it's probably shuffling function parameters around based on whether optional ones have been specified. (In this case, locale
would be the optional one prior to strict
.)
It checks if locale is exactly true or false instead of any other falsy (undefined, null, '', NaN, 0) or truthy values
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With