Giter Site home page Giter Site logo

Comments (2)

matthill82 avatar matthill82 commented on August 11, 2024

Array is a type of Object in JavaScript, the array is a mixed type array.

Sent from my iPhone
So may be short.

On 24 Jul 2015, at 16:24, adityathebe [email protected] wrote:

var x = ["abc", 123];
var y = typeof x;
console.log(y);

I was expecting the output "array" but it resulted as an object. How come array be an object ?


Reply to this email directly or view it on GitHub.

from head-first-javascript-programming.

AdiChat avatar AdiChat commented on August 11, 2024

JavaScript is an OOP language. An array of objects is an object. One can check if a variable is an array in several ways such as : var Arr = data instanceof Array;
var Arr = Array.isArray(data);
The most reliable way is : isArr = Object.prototype.toString.call(data) == '[object Array]';
In jQuery, we can use : var isArr = $.isArray(data);

Array objects give special treatment to a certain class of property names. A property name P (in the form of a String value) is an array index if and only if ToString(ToUint32(P)) is equal to P and ToUint32(P) is not equal to 2^32-1. A property whose property name is an array index is also called an element. Every Array object has a length property whose value is always a nonnegative integer less than 2^32. The value of the length property is numerically greater than the name of every property whose name is an array index; whenever a property of an Array object is created or changed, other properties are adjusted as necessary to maintain this invariant. Specifically, whenever a property is added whose name is an array index, the length property is changed, if necessary, to be one more than the numeric value of that array index; and whenever the length property is changed, every property whose name is an array index whose value is not smaller than the new length is automatically deleted. This constraint applies only to own properties of an Array object and is unaffected by length or array index properties that may be inherited from its prototypes.

The following code returns true :
var a = new Array(1,2,3);
a['key'] = 'experiment';
Array.isArray(a);

The following code returns false :
var a = {1:1, 2:2, 3:3,'key':'experiment'};
Array.isArray(a)

from head-first-javascript-programming.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.