0

I have an object:

let myObject = {first: "", second: null};

And I have a function:

return Object.values(myObject).some(
   objectValue => objectValue !== "" && typeof objectValue !== "undefined" && typeof objectValue !== null
);

What this does is that it returns true if an object has a set value and returns false if an object doesn't have any set values. The thing is that when I pass a null value, this function returns true (as if the object has a set value).

On other occasions it works fine. What is wrong here?

HELPME
  • 714
  • 14
  • 35
  • 1
    Your check is incorrect: typeof null is "object", not "null". https://stackoverflow.com/questions/18808226/why-is-typeof-null-object. Compare with `null` literal instead. – justinas Jun 03 '19 at 13:42

1 Answers1

1

null and undefined are falsy so you can only write this

return Object.values(myObject).some(
  objectValue => objectValue?true:false;
 );

null will return false in this case.

Bellash
  • 7,560
  • 6
  • 53
  • 86
  • 2
    You can simplify this and eliminate the ternary expression: `!!objectValue` –  Jun 03 '19 at 14:06
  • it would be better to compare this way: `objectValue === null`, because `objectValue` will be as `false` for every falsy value like `0`, `NaN` etc. – marsibarsi Jun 03 '19 at 14:26