Skip to content

JavaScript Array

The variables that are created to store multiple data are called arrays. The basic idea of ​​creating arrays is to access multiple data under a single variable.
Each data in the variables is called an element. Array elements are called with index values. The index shows where an element is located in the array.

var names = ["john","alice","rick"]

The indexes start from 0 as in most programming languages. The reason for starting from 0 is that the programming languages ​​are fully human logic.

names[0] // john
names[1] // alice
names[2] // rick

There are three methods used to describe elements in the array.

The first method to define the array:

        var names = new Array("john", "alice", "rick");

We have created a 3-element array named “names”.

The second method to define the array:

        var names = ["john", "alice", "rick"];

The third method to define the array:

        var names = new Array();
        names[0] = "john";
        names[1] = "alice";
        names[2] = "rick";


The “length” property is used to find the number of elements of the array.

    var names = ["john", "alice", "rick"];
    document.write(names.length); // output: 3

It depends on the length property and numeric properties of a JavaScript array. When a set of predefined array methods (eg, join, slice, indexOf, etc.) are called, it takes into account the length property value of the array. Other methods (eg, push, splice, etc.) cause the array’s length property to be updated.

        var country = [];
        country.push("Turkey", "USA", "France", "Russia");

If an array element is updated with an appropriate index value and the relevant index number is outside the range limits; the array size will grow to accommodate the given index, and the JavaScript engine will update the length property of the array accordingly.

        var country = [];
        country.push("Turkey", "USA", "France", "Russia");
        console.log(country[0]) // Turkey
        console.log(country[1]) // USA
        console.log(country[2]) // France
        console.log(country[3]) // Russia
        console.log(country.length) // 4

We can assign “length” separately.

        var country = [];
        country.push("Turkey", "USA", "France", "Russia");
        country.length = 10;

Array Methods

Array from()Creates a new array instance from a series of similar or from a renewable object.
Array isArray()Checks the array.
Array of() Creates a new Array instance with a variable number of arguments, regardless of number or type of the arguments.
Array copyWithin() Copies a sequence of array elements within the array.
Array fill() Fills all the elements of an array from a start index to an end index with a static value.
Array pop()Removes the last element from an array and returns that element.
Array push() Adds one or more elements to the end of an array and returns the new length of the array.
Array reverse() Reverses the order of the elements of an array in place — the first becomes the last, and the last becomes the first.
Array shift() Removes the first element from an array and returns that element.
Array sort() Sorts the elements of an array in place and returns the array.
Array splice() Adds and/or removes elements from an array.
Array concat() Returns a new array comprised of this array joined with other array(s) and/or value(s).
Array includes() Determines whether an array contains a certain element, returning true or false as appropriate.
Array slice() Extracts a section of an array and returns a new array.
Array indexOf()Returns the first (least) index of an element within the array equal to the specified value, or -1 if none is found.
Array lastIndexOf()Returns the last (greatest) index of an element within the array equal to the specified value, or -1 if none is found.
Array entries()“entries()”method returns a new Array Iterator object. The object contains the entries for each item in the Array, and can be advanced with next().
Array every()“Every” function checks whether all the elements of the array satisfy the given condition or not that is provided by a function passed to it as the argument.
Array filter()
filter () returns data that satisfies the given conditions. The item argument is a reference to the current element in the array as filter() checks it against the condition. This is useful for accessing properties, in the case of objects.
Array find() The find () method searches for the specified value in the array. Returns the first providing value.
Array findIndex()FindIndex () returns the order of the first value that provides the condition in the array. In this respect, it is very similar to the find () method. The only difference between them: one turns the order and the other returns the value. Returns -1 if no value is entered.
Array flat()The flat() method available on the Array prototype returns a new array that’s a flattened version of the array it was called on. The default value(if no value is entered) is 1 .
Array forEach()The forEach () method is used to execute a function on each element in an array. In other words: forEach (), the ForEach () method runs a function once provided for each array element.
Array join()The Array.join () method combines elements in the array. The elements of the array are separated by the mark we give. If we don’t give any signs, it throws the “,” sign.
Array keys()The keys() method returns a new Array Iterator object that contains the keys for each index in the array.
Array flatMap()The flatMap () method maps each value to a new value, and then returns the resulting array to a maximum depth of 1.
Array map()The Map () method scans through each element of the array. It returns a new array with the elements it scans. The map() method calls the provided function once for each element in an array, in order.
Array reduce()The reduce () method reduces the array to a single value. executes a function provided from left to right for each value of the array. reduce() does not execute the function for array elements without values.
Array some()The some () method scans each element in the array, respectively. Ends scanning when the desired condition is met. Returns true. Returns false if the condition is not satisfied until the last element.

JavaScript Array Examples: Click



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: