DEV Community

Mwansa Matimba
Mwansa Matimba

Posted on

are javascript arrays dynamic

are javascript arrays dynamic

Top comments (1)

Collapse
 
gilfewster profile image
Gil Fewster

Yes!

JavaScript arrays:

  • can be declared without an explicit length, and will automatically increase in length as items are added
  • can be resized at any time explicitly in your code, or dynamically at runtime
  • can contain elements with any combination of types
  • can replace an element of any type with an element of any other type
const myArray = new Array();
console.log(myArray.length); // 0
console.log(myArray); // []

myArray.push("a",9,{name: 'bob'});
console.log(myArray.length); // 3
console.log(myArray); // [ 'a', 9, { name: 'bob' } ]

myArray[0] = 42
console.log(myArray.length); // 3
console.log(myArray); // [ 42, 9, { name: 'bob' } ]

myArray[2] = undefined
console.log(myArray.length); // 3
console.log(myArray); // [ 42, 9, undefined ]

myArray.length = 2
console.log(myArray.length); // 2
console.log(myArray); // [ 42, 9 ]
Enter fullscreen mode Exit fullscreen mode