javascript - Find length of an array in jquery getting wrong result -


i have javascript code below size of array,

var testvar = [  ];  testvar[1] = 2;  testvar[200] = 3;  alert(testvar.length);    //201

fiddle demo

but code alerts 201 insted of getting size 2,why?

i need count of total elements in array.

if want find length of array despite doing such things :

alert(object.keys(testvar).length); 

https://jsfiddle.net/4lqsf45a/

note object.keys() not avaiable in ie < 9. see https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/object/keys workarounds


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -