0

I noticed that Javascript allows you to add a name/value (or key/value) pair inside already-made regular arrays (not objects). Why is this allowed? Is there an intention behind this? Is this another data type? I understand this is neither an object nor an associative array (which is really an object). I know arrays are objects too, and in this case JS is allowing me to use a string instead of a number as a key, but this would put the data in an odd category which is neither a regular array nor an object.

array1 = ['a', 'b', 'c']
array1.propertyName = 'property value'
console.log(array1);  // [ 'a', 'b', 'c', propertyName: 'property value' ]

This question was already addressed in How to create an associative array in JavaScript literal notation, but no real answer was given rather than "You need to use an object". I already use objects in JS, I am just curious about this behaviour in arrays.

Thank you!

Alejandro
  • 37
  • 9

1 Answers1

0

You can assume pair is just a value. Value inside a value is normal unless if you are using static typed languange.

Why is that allowed? computer take any argument u pass as a value despite it having a pair, array or an object. Doesn't matter.

  • Thanks for your comment. The problem is that the value is actually being interpreted as an object's property. Also, your answer seems to be generic to computer behaviour and not to how JS works. The answer and link on the post indicated as originally addressing this question were really clarifying to me: adding key/value pairs arrays is allowed because everything in JS is an object, and, furthermore, doing so is considered a bad practice. – Alejandro Nov 08 '22 at 12:23
  • why do you think everything in javascript is an object? Not everything in pair is an object. – Rais Helmy Nov 08 '22 at 12:33