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?

1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

You can simplify this and eliminate the ternary expression: !!objectValue
it would be better to compare this way: objectValue === null, because objectValue will be as false for every falsy value like 0, NaN etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.