0

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?

3 Answers3

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

1

=== 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;
Daemon Beast
  • 2,794
  • 3
  • 12
  • 29
-1

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

LoukasPap
  • 1,244
  • 1
  • 8
  • 17