Comments (2)
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.
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)
- Global Variable. HOT 1
- Object Magnets - Page : 221 || Mistake HOT 1
- Why can't I set an attribute to an element through JS ? HOT 2
- Missing resources for rock.png and cookies.js HOT 1
- Question re Battleship game in Ch. 8: why functions are not part of our objects? HOT 1
- What is the license for the source in this repository? HOT 5
- Object Magnets Solution (pg. 221) HOT 1
- Chapter 13 Robot game solution (p. 583 in pdf book) HOT 2
- For the Blur/zeroBlur image exercise
- object name HOT 2
- Chapter 8 - Battleship HOT 17
- How/where do we call the isSunk method in chapter 8 ?
- Does anybody understand the thingamajig code? HOT 4
- Chapter 8 - Battleship game.
- Chapter 4. Bubbles 2. HOT 2
- Exercise forwards and backwards. HOT 4
- Appendix RegExp, Recursion and JS Algorithms Practice HOT 2
- battleship game in chapter 2 is flawed !!! HOT 4
- JavaScript - Pass-by-value || Page 92 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from head-first-javascript-programming.