Arrays in JavaScript

Walden Systems Geeks Corner Arrays in JavaScript Programming tutorial developer Rutherford NJ New Jersey NYC New York North Bergen County
a high-level, interpreted programming language that conforms to the ECMAScript specification. It is a language that is also characterized as dynamic, weakly typed, prototype-based and multi-paradigm. Alongside HTML and CSS, JavaScript is one of the three core technologies of the World Wide Web.[9] JavaScript enables interactive web pages and thus is an essential part of web applications. The vast majority of websites use it,[10] and all major web browsers have a dedicated JavaScript engine to execute it.

Arrays are an easy way to store continuous items in memory in a single variable. It stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. You can access elements in the array by its index. The index of the array starts with 0. Arrays in JavaScript are denoted by square bracket []. To get the length of array or count of elements in the array we can use the length property. One thing that you can do with an Array constructor that you cannot do with an array literal created with bracket notation is that you can set an initial length.

Array properties

To create an array, we simply assign values to a newly declared array with the statement : var someArray = ["some","values"]. Bot there is more to it then meets the eye. The statement above triggers a constructor that returns a reference to the array function that creates the array object. The newly created array object has properties such as index and length.


Array access methods

What makes arrays useful is not only that we can group similar data in a single variable but that it allows us to manipulate and access items in the array. JavaScript gives us methods to access the data as well as manipulating the data. Here are some of the more common methods of an array object in JavaScript:

concat(list1,list2) - Returns a new array that consists of the 2 lists combined.

pop() - Removes the last element of the array and returns the last element.

push(list1) - Adds one or more elements to the end of the array and returns the new length of the array.

shift() - Removes the first element of the array and returns the first element.

unshift(list1) - Adds one or more elements to the beginning of the array and returns the new length of the array. sort() - Sorts the elements of an array.

Inner workings of an Array

An array is a special kind of object. The square brackets used to access a property arr[0] actually come from the object syntax. Numbers are used as keys. They extend objects providing special methods to work with ordered collections of data and also the length property. But at the core it’s still an object. There are only 7 basic types in JavaScript. Array is an object and thus behaves like an object.

What makes arrays special is their internal representation. JavaScript tries to store its elements in the contiguous memory area, one after another to make arrays fast. But they all break if we quit working with an array as with an ordered collection and start working with it as if it were a regular object. For example, the code below is possible since arrays are objects but JavaScript will turn off array specific optimizations since we are using an array as a regular object.

let myArr = [] ;

myArr[25] = 25;

myArr.myProp = 10 ;

Some of common misuses of arrays include adding non-numeric properties like myArr.myProp = 5 in the code above. Another common mistake is filling in elements of the array with gaps such as myArr[25] = 25 and then myArr[50] = 50 with nothing between them. A third common mistake is filling the array in reverse order. Think of arrays as special structures to work with the ordered data. JavaScript arrays provide special methods for that. Arrays are carefully tuned inside JavaScript engines to work with contiguous, ordered data. if you need arbitrary keys, chances are high that you actually require a regular object {}.