0

I have the following method

const isPrivate = (input) => {
  return input && input.type && input.type === "private";
}

console.log(isPrivate());

Why is it returning undefined and not a boolean?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Wee Kian
  • 23
  • 3
  • Input is not defined _if you don't pass it in as a parameter_. – Lewis Mar 01 '19 at 17:31
  • Could not replicate, it logs `undefined` as expected when I run it locally. – jonrsharpe Mar 01 '19 at 17:32
  • @Lewis Shouldn't the first condition evaluate to false and return false? Why does if(input) evaluate to false – Wee Kian Mar 01 '19 at 17:32
  • 2
    `&&` in JavaScript returns operands, not booleans, unless the returned operand is a boolean. – Teemu Mar 01 '19 at 17:34
  • Possible duplicate of [Why don't logical operators (&& and ||) always return a boolean result?](https://stackoverflow.com/questions/5417969) – adiga Mar 01 '19 at 17:36

3 Answers3

2

Logical operators don't coerce or return booleans.

!!input will ensure that input is a truthy value and return a boolean. input.type === "private" will also return a boolean. Since both sides of the operator evaluate to booleans, you'll get the value you're expecting.

const isPrivate = (input) => {
  return !!input && input.type === "private";
}

console.log(isPrivate());
console.log(isPrivate({}));
console.log(isPrivate(''));
console.log(isPrivate({ type: 'public' }));
console.log(isPrivate({ type: 'private' }));
sellmeadog
  • 7,437
  • 1
  • 31
  • 45
0

The forced cast of the evaluation of the existence of input to Boolean is not guaranteed. Test for existence explicitly. This also makes clearer the intent of the evaluation. Additionally, leverage the interpreter to check for errors of intent by reversing order of comparison operands for the === operator. Comparing input.type to literal "private", the interpreter won't let the mistake ["private" = input.type] slide but would be fine with [input.type = "private"]. Finally, there's very little cost for employing parentheses to enhance salience of the delineation of phrases.

const isPrivate = (input) => { return ("undefined" !== typeof input) && ("undefined" !== typeof input.type) && ("private" === input.type); };

Richard Uie
  • 141
  • 4
-1

There's an issue with your input variable. The error just says that input is undefined, meaning you never gave it a value. Javascript won't try to resolve an undefined value as false, but rather will just throw the error.

If you want to test for undefined first, change it to

return input != null && input && input.type && input.type === "private";

This way it will check if it's null first, and if it's valid, will evaluate as true and move on to the next calculation.

el toro
  • 532
  • 4
  • 11
  • It doesn't ask for it to be `true`, but *truth-y* - non-empty strings are truth-y. – jonrsharpe Mar 01 '19 at 17:33
  • @adiga Are you sure? I just tested `var input; input != null && input && input.type && input.type === "private";` in my browser's console and it returned `false` properly. – el toro Mar 01 '19 at 17:41