3

I ran into a boolean test variable === Object(variable) but couldn't find anything that describes it.

Is it testing that variable is the same as Object(variable) and does Object(variable) cast this variable into object? Or does it do something else?

If it matches it will loop for (var key in variable) and uses key and variable[key] as parameters for another function. If it fails it uses just that variable as is.

1 Answer 1

3

It checks that

  1. the variable is defined
  2. its value is an object
  3. its value isn't null (be careful: typeof null is "object")

This is probably the simplest way to check these 3 conditions and it looks like a reasonable test to run before to loop on keys in a very polymorphic function.

Another one would have been typeof variable === "object" && variable.

From the MDN:

The Object constructor creates an object wrapper for the given value. If the value is null or undefined, it will create and return an empty object, otherwise, it will return an object of a Type that corresponds to the given value. If the value is an object already, it will return the value.

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

1 Comment

Perfect! "If the value is an object already, it will return the value." I was on the wrong page in MDN: Object.protoype. So, close but not close enough :)

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.