user13294667
user13294667

Reputation:

Object does not equal to true or false

So my uploaded media file (event.target.files[0]) does not equal to true or false. It has a typeof object.

It's part of some form state and I'd like to check the object whether all fields are not empty "".

I thought a JS object should always === true, but maybe this is different for 'files' objects?

Upvotes: 0

Views: 442

Answers (3)

Daemon Beast
Daemon Beast

Reputation: 2909

=== checks for strict equality, so the two values must be exactly the same.

An object is truthy, but does not equal true, so what you are really doing is { ... } === true, which is false.


If you want to check if none of the object's values are empty, you can filter for empty values:

const empty = Object.keys(theObject).length === 0 || Object.values(theObject).filter(value => {
  return value.trim() === '';
}).length > 0;

Upvotes: 1

LoukasPap
LoukasPap

Reputation: 1381

To check the type of a value, you must write

if( (typeof <your value>) == ("<expected type>")){
   ...
}

For example, a statement like this:

if( (typeof 42)=="number" )

is true.

Reference for most cases of typeof

Upvotes: -1

=== tests for equal value and equal type (ref). typeof(true) is boolean but a file is not a boolean. So the comparison will never yield true.

See also https://stackoverflow.com/a/8511350/4640820

Upvotes: 1

Related Questions