Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript logical operation (a === true || a === false)

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;
}
like image 458
Merhawi Fissehaye Avatar asked Jan 15 '18 07:01

Merhawi Fissehaye


People also ask

What is === operators in JavaScript?

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.

What is == and === in JavaScript?

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.

What are the 3 logical operators in JavaScript?

Logic operators are used to find the logic between variables in JavaScript. There are three logical operators in JavaScript: || (OR), && (AND), ! (NOT).

What does && and || mean in JavaScript?

The logical and ( && ) and or ( || ) are logical operators in JavaScript. Normally, you're using these operators on booleans: true && true // => true. true && false // => false.


2 Answers

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.)

like image 96
T.J. Crowder Avatar answered Sep 23 '22 00:09

T.J. Crowder


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

like image 34
Faly Avatar answered Sep 24 '22 00:09

Faly