How does a jQuery instance appear as an array when called in console.log?

When entered into a JavaScript console, a jQuery object appears as an array. However, it's still an instance of the jQuery object.

var j = jQuery();
=> []
=> []
console.log('test with string concat: ' + j);
=> test with string concat: [object Object]
j instanceof Array
=> false
j instanceof jQuery
=> true

How could one duplicate this with their own object?

--------- EDIT ---------

Thanks to ZER0 for figuring it out. Here's some example code to create an object that works just like jQuery in the console:

var Foo = function() {
  this.splice = Array.prototype.splice;
  Array.prototype.push.apply(this, arguments);

  return this;

var f = new Foo();
=> []
=> []
console.log('test with string concat: ' + f);
=> test with string concat: [object Object]
f instanceof Array
=> false
f instanceof Foo
=> true

Very cool.



I believe they have something like that:

// Adding items to an object like an Array:
var myObject = {splice : Array.prototype.splice};, 1, 10, "foo");

// Getting items from an object like an Array:

alert(, 0));



The console makes it look like an array because it has array-like properties. It has length and n keys using integers.


When you call jQuery() you aren't passing any arguments to the function so it is returning an empty object. WHen you pass a selector as argument it creates an object of the elements that match the selector


It returns an empty array, because it did nothing - array of elements affected.

Proper way to inspect would be


if you would do


you will see that it returns array of elements.


I think what you're trying to see are your object's properties. If that's the case, you're inadvertently converting the object into a string when you append it to another string. Use the , operator instead of + to show the object properties instead of the "stringified" version of the object:

console.log('test with string concat: ', j);


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.