Arrays in JavaScript!!!

Table of contents

No heading

No headings in the article.

Arrays are one of the main concepts to be mastered in the programming world. Since it can hold many values under a single name and can access values by referring to an index number.

  • Arrays are collection of elements. -Arrays are usually declared in two ways. 1st is the very straightforward and it is the best practice to declare an Array. image.png

image.png There is certainly another way to declare an array which is below but not recommended since it does not create an array literal if there is only one element using this type of declaration.

image.png

image.png

image.png

  • In Arrays usually the elements are represented with index which generally start with 0.
  • To calculate how many elements are present in an array literal. We can simply use the length property.
  • For example in the below fruits example we have 4 fruits so the length is 4 but the apple is at index "0".
  • Since we all know Arrays are indexed with '0'. image.png
  • The data type of an Array is Object.
  • We can also verify to see if which element is at index 0 and index 1 from an array.

image.png

  • We can also assign new elements to an existing array.

image.png