How to find out the type of a variable?

I am trying to learn Javascript debugging in the Chrome Javascript console. However, I do not understand how the console displays the data type of the object. For example, in the Javascript Console, it shows this:

enter image description here

In this figure, I am using jQuery. I tried to do console.logfor several variables, but how do I know if a particular variable is a jQuery object or a raw DOM object? Is it HTMLDivElementeither another that shows the tag divspecified in the console, a jQuery object, or a raw DOM object?

In general, how do I know the data type of an object or variable in Javascript in the debugger console, like the Chrome Javascript console? In languages ​​such as Java, the variable data type is clearly displayed in the debugger; I can find out from the debugger which object the variable is, be it an instance Class Aor an instance Class B, etc.

+3
source share
4 answers
if (variable instanceof jQuery) // Or variable.jquery 
    // jQuery object.

Live demo

instanceofdocs on MDN :

The instanceof operator checks to see if an object in its prototype chain has a constructor prototype property.


JQuery validation method for DOM elements nodeType::

// Handle $(DOMElement)
if ( selector.nodeType ) {

The way jQuery validates a jQuery object has a property jquery:

// HANDLE: $(expr, $(...))  
else if ( !context || context.jquery ) {
+4
source

These are both jQuery objects.

, DOM.

+3

You can view the type in the debugger if you go to the Scripts tab .
then on the right, click the + sign under Watch Expressions and add whatever you like.
inspector

0
source

JQuery objects are actually arrays of DOM elements, Wile DOM elements are just DOM elements.

0
source

All Articles