Internet
Fact-checked

At EasyTechJunkie, we're committed to delivering accurate, trustworthy information. Our expert-authored content is rigorously fact-checked and sourced from credible authorities. Discover how we uphold the highest standards in providing you with reliable knowledge.

Learn more...

What is an Array in JavaScript®?

Vanessa Harvey
Vanessa Harvey

An array in JavaScript® is a special type of variable that can hold multiple pieces of information or data values and is a storage place in memory. Those data values can be of the same type or of different types, but having different types of data in an array in JavaScript® is unusual. It generally is easier to understand what these arrays are by comparing them to simple things such as a box of books. The box is the equivalent of the array, and the books that the box contains represent the multiple data values. In this example, each book is a piece of information known as a data value. The books can be different types of books and can contain very different content. This type of flexibility with an array in JavaScript® is not seen in what are known as strongly typed programming languages such as Java or C++. JavaScript® is a programming language, but it often is referred to as a scripting language because it frequently is employed by web developers to code client-side scripts or small programs in comparison with full-blown software applications. An array in JavaScript® permits the programmer to access a specific item in the invisible containing variable, because items or data values are indexed by a number or a word.

If a magic marker was used to write a unique number starting from zero on each of the books in the box, a specific number that refers to only one book could be used to retrieve that book without retrieving all of the other books in the array. This characteristic of an array in JavaScript® is known as the index of the particular data value. It might be thought of as the item name. Data values in an array are counted starting at zero; the count is the assignment of an index for organizational purposes. Counting begins at zero, not at one, so that the first item to be added to a new array is indexed at zero, the second item is indexed at one, and the addition of subsequent items follows this pattern.

Woman doing a handstand with a computer
Woman doing a handstand with a computer

Programmers in this language have the freedom to simply add an item at the position they want because the length of an array in JavaScript® can be dynamic, something that is not allowed in strongly typed programming languages. When items are indexed by a word instead of a number, it is known as an associative array in JavaScript®. Associative arrays come in handy when working with arrays that also are properties of the document object. An array in JavaScript® often is used with what is known as a "for loop" because of the frequency with which web developers must loop through all of the items in the variable for various purposes.

For example, a programmer might need to print out all of the capitals of the countries of Europe for a desired functionality of a script. He or she could use a for loop to loop, one by one, through each of the names of capital cities of various nations in Europe that are held in the array and identified by their index so that as they are "called," they come forth. The use of an array in JavaScript® is determined by the purpose of the script. Not every script, particularly if it is very small, needs to contain an array in the code.

Discuss this Article

Post your comments
Login:
Forgot password?
Register:
    • Woman doing a handstand with a computer
      Woman doing a handstand with a computer