#1 Using
Example:
instanceof
operator #Example:
Additional
Checking object #
This operator can also check if the input is an object, by usingObject
built-in constructor. As such:
Or, we can usetypeof
method.
For example:typeof
will return "object" (string) keyword for eitherobject
orarray
type input. Therefore, this operator can't be used for checking "array" input.
Checking function #
We can check whether an object is a function:
Or, usingtypeof
operator.
For example:
Checking string and number #
We can usetypeof
for those.
As such:
Checking element #
We can useinstanceof
for that.
Example:
In conclusion #
There are 2 ways we can choose to check the "array"-ness.
the_array instanceof Array
Array.isArray(the_array)
(supported on IE9+ and other current browsers)
instanceof
operator, we can create a custom constructor for a particular object we wanna test. More documentation at MDN
Comments
Post a Comment