1

why does myArray.length is 3 here? I have added a new property in it, to my guess, it should be 4.

    var myArray = [ "foo", 42, "bar" ];
    
    myArray.baz = "baz";
    
    myArray.length; // 3
7
  • 2
    you're adding a property to the myArray object - this is valid, but doesn't change the length of the array (unless the property was numeric, of course), to add to the "list" use a numeric index or push or unshift array methods - developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Aug 10, 2021 at 18:06
  • my question is why the length is not changed? technically I have 4 items in the array. Commented Aug 10, 2021 at 18:09
  • 2
    my answer tells you why ... an array has the property called length and the property called map, and find, and reduce .... many many properties, but these are not included in length either ... the Array "length" property (I linked to some useful documentation you can read) is not a count of properties, it is always numerically greater than the highest index in the array. index being a NUMBER - and that number is 2, therefore index is (at least) 3, because there are entries at 0, 1 and 2 only Commented Aug 10, 2021 at 18:12
  • 1
    If you want to add a new Item in array you can use myArray[3] = "baz", it's not an object, to do like that myArray.baz Commented Aug 10, 2021 at 18:17
  • 1
    @Md.RajuAhmed - no, they CAN, an Array is an Object ... the only difference is that length is unaffected by non-numeric properties, and the array methods I mentioned before will not iterate over those non-numeric properties ... but it's perfectly OK (not advisable) to add properties to an array Commented Aug 10, 2021 at 18:27

2 Answers 2

1

You need to use Array.prototype.push() to add new elements to array. In your code myArray.baz = "baz" doesn't do anything.

var myArray = [ "foo", 42, "bar" ];

// your approch
myArray.baz = "baz"; 

console.log(myArray.length); // 3
console.log(myArray) // [ "foo", 42, "bar" ]


// correct 
myArray.push("baz")

console.log(myArray.length); // 4
console.log(myArray) // [ "foo", 42, "bar", "baz" ]

Sign up to request clarification or add additional context in comments.

3 Comments

or unshift, or splice, or myArray[3] = 'baz' ... have I missed any?
myArray.baz = 'baz' certainly DOES do something. It adds the property baz with the value baz to myArray
I know the correct approach, But I needed to know why.
1
  1. In the first line, you create a variable myArray and you add a value to it. Every variable in Javascript is an object, that has keys and properties (see more: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)

Between [] you create an array type, that stores data and labels them with numbers. (see more: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)

  1. In the second line, you add a baz property to myArray with "baz" value
  1. In the third line, you get the length property of the array

If you want to add a(n) "baz" element to the end of the array, you can use the .push() method. For example: myArray.push("baz");

5 Comments

yes, good old .push always gets a mention, but what about .unshift, or .splice, or myArray[3] = 'baz' - why are they ignored options?
I needed to know why array length does not change adding named property to it.
@Bravo that's why I add links, to learn more about this topic. I saw the problem of the question is adding elements to the array, and the simplest method is the .push().
Sure, but push gets all the love :p I wasn't criticizing :p
@Md.RajuAhmed because the length property is counting how many elements are in the array, not how many js object properties have it. In other languages, you can not add plus property to an array. Js is a high abstraction language, therefore you can do this strange thing.

Your Answer

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.