Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
17
rated 0 times [  19] [ 2]  / answers: 1 / hits: 68727  / 11 Years ago, sat, january 25, 2014, 12:00:00

Could anyone help explain why length is returning 0? Thank you VERY much in advance!



var errors = [];
errors[''] = Monday Morning slot already taken. Would you like to replace it?;
console.log(errors);
document.write(errors.length);

More From » arrays

 Answers
14

Because that is not how you add an item since Arrays only accept numeric keys. You do it like this:



errors.push(blah);  /*or*/  errors[0] = blah;

//Now if you check the length:
errors.length; //1


Also, if you are using it as an Object, '' isn't a valid name either.



*Correction: Looks like you can use (empty string) as a key.


[#72941] Friday, January 24, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lara

Total Points: 462
Total Questions: 100
Total Answers: 102

Location: Jersey
Member since Mon, Jun 14, 2021
3 Years ago
lara questions
;