67

I have a simple question that I'm struggling with for some reason.

Why does the below return undefined?

var testvar={};
testvar[1]=2;
testvar[2]=3;
alert(testvar.length);

edit I originally typed testvar[1].length. I knew this to be an error. I meant testvar.length

Cadoiz
1,6942 gold badges25 silver badges36 bronze badges
asked Mar 15, 2011 at 19:59
1
  • What are you trying to accomplish? What do you expect the result to be? Commented Mar 15, 2011 at 20:01

7 Answers 7

118

Because 2 isn't an array, it's a number. Numbers have no length.

Perhaps you meant to write testvar.length; this is also undefined, since objects (created using the { ... } notation) do not have a length.

Only arrays have a length property:

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

Note that Javascript arrays are indexed starting at 0 and are not necessarily sparse (hence why the result is 3 and not 2 -- see this answer for an explanation of when the array will be sparse and when it won't).

answered Mar 15, 2011 at 20:00

Comments

5

testvar[1] is the value of that array index, which is the number 2. Numbers don't have a length property, and you're checking for 2.length which is undefined. If you want the length of the array just check testvar.length

answered Mar 15, 2011 at 20:04

Comments

3

Integer has no method length. Try string

var testvar={};
testvar[1]="2";
alert(testvar[1].length);
answered Mar 15, 2011 at 20:02

Comments

0

If length is undefined you can use:

function count(array){
 var c = 0;
 for(i in array) // in returns key, not object
 if(array[i] != undefined)
 c++;
 return c;
}
var total = count(array);
Douwe de Haan
6,7153 gold badges35 silver badges53 bronze badges
answered Aug 7, 2018 at 3:58

Comments

-2
 var mode = [];
 $("input[name='mode[]']:checked").each(function(i) {
 mode.push($(this).val());
 })
 if(mode.length == 0)
 {
 alert('Please select mode!')
 };
answered May 1, 2019 at 5:13

1 Comment

"Why does the below return 'undefined'?"
-2
obj={};
$.each(obj, function (key, value) {
 console.log(key+ ' : ' + value); //push the object value
});
for (var i in obj) {
 nameList += "" + obj[i] + "";//display the object value
}
$("id/class").html($(nameList).length);//display the length of object.
Douwe de Haan
6,7153 gold badges35 silver badges53 bronze badges
answered Aug 10, 2018 at 12:34

1 Comment

Please don't just post code without any explanation. Anyway, this code does not seem to have anything to do with what was beeing asked in the first place.
-3
var array=[];
array.push(array); //insert the array value using push methods.
for (var i = 0; i < array.length; i++) { 
 nameList += "" + array[i] + ""; //display the array value. 
}
$("id/class").html(array.length); //find the array length.
Ashley Mills
53.5k17 gold badges138 silver badges174 bronze badges
answered Aug 10, 2018 at 13:26

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.