Loading...
Loading...
00:00:00

Array in Javascript

In JavaScript, an array is an ordered collection of values, which can be of any type (including other arrays). Arrays are a fundamental data structure in programming, and they provide a convenient way to store and manipulate data in a single variable.

In JavaScript, there are two types of arrays:

  1. Indexed arrays: An indexed array is the most common type of array in JavaScript. It consists of a list of values, each of which is identified by an index (i.e. a numerical position in the array). The first element in the array has an index of 0, the second element has an index of 1, and so on. You can access elements in an indexed array using square brackets and the index number.

Here's an example of an indexed array:

const fruits = ["apple", "banana", "orange"];
console.log(fruits[0]); // "apple"
console.log(fruits[1]); // "banana"
console.log(fruits[2]); // "orange"
  1. Associative arrays: An associative array (also known as an object) is a collection of key-value pairs, where the keys are strings and the values can be of any type. Unlike indexed arrays, the order of elements in an associative array is not guaranteed. You can access elements in an associative array using square brackets and the key name.

How to Create Array 

In JavaScript, you can create an array using the Array() constructor, which creates a new array and returns a reference to it. The Array() constructor can take one or more arguments, which can be used to initialize the elements of the array.

Here's an example of how to create an array using the Array() constructor:

// create an empty array
let myArray = new Array();

// create an array with a specific length
let myArray2 = new Array(5); // creates an array with length 5

// create an array with initial values
let myArray3 = new Array('apple', 'banana', 'orange'); // creates an array with 3 elements

In the first example, we create an empty array by calling the Array() constructor without any arguments. The resulting myArray variable will be an empty array with no elements.

In the second example, we create an array with a specific length of 5. The resulting myArray2 variable will be an array with 5 empty slots, which can be populated later.

In the third example, we create an array with 3 elements, initialized to the values 'apple', 'banana', and 'orange'. The resulting myArray3 variable will be an array with 3 elements.

Note that the array literal notation [] is the more common and often preferred way of creating arrays in JavaScript. However, the Array() constructor can be useful in certain situations, such as when you need to create an array with a specific length, or when you need to create an array with a variable number of elements.

Array Object & Constructor

In JavaScript, the Array object is a global object that represents an ordered collection of elements, which can be of any data type, such as numbers, strings, or other objects. Arrays are one of the most commonly used data structures in JavaScript, as they provide a way to store and manipulate sets of related data.

An array in JavaScript can be created using the Array() constructor, or by using the array literal notation, which is a comma-separated list of values enclosed in square brackets []. For example, the following code creates an array of strings using the array literal notation:

let fruits = ['apple', 'banana', 'orange'];

Once an array is created, you can access its elements using their index, which starts from 0. For example, to access the first element in the fruits array above, you can use fruits[0].

The Array object also provides a set of built-in methods that can be used to manipulate arrays, such as adding or removing elements, sorting and searching the array, and iterating over the elements. These methods make it easy to work with arrays in JavaScript and are an important part of the language.

Arrays can be used in a wide variety of applications, such as storing lists of items, representing matrices and other mathematical structures, and as a building block for more complex data structures like trees and graphs.

Here's an example of an associative array:

const person = {
  name: "John",
  age: 30,
  email: "john@example.com"
};
console.log(person["name"]); // "John"
console.log(person["age"]); // 30
console.log(person["email"]); // "john@example.com"

Note that in JavaScript, arrays are actually a specific type of object. This means that you can add properties and methods to an array, just like you would with any other object. However, it's generally not recommended to add non-numeric properties to an array, as it can cause confusion and unexpected behavior.

In addition to the basic syntax for creating and accessing arrays, JavaScript provides a wide range of methods for working with arrays. These include methods for adding, removing, and modifying elements, as well as methods for sorting, searching, and iterating over arrays.

Array Functions and Uses

Here are all of the functions provided by the JavaScript Array object:

  1. concat(): Combines two or more arrays and returns a new array.

  2. copyWithin(): Copies a sequence of array elements within the array, overwriting existing elements.

  3. entries(): Returns a new array iterator object that contains the key/value pairs for each index in the array.

  4. every(): Checks if every element in the array passes a certain test.

  5. fill(): Fills all elements in an array with a static value.

  6. filter(): Creates a new array with all elements that pass a certain test.

  7. find(): Returns the value of the first element in the array that satisfies a certain test.

  8. findIndex(): Returns the index of the first element in the array that satisfies a certain test.

  9. forEach(): Executes a function for each element in the array.

  10. includes(): Checks if an array contains a certain element, returning true or false.

  11. indexOf(): Returns the index of the first occurrence of a specified element in the array, or -1 if not found.

  12. join(): Joins all elements of an array into a string.

  13. keys(): Returns a new array iterator object that contains the keys for each index in the array.

  14. lastIndexOf(): Returns the index of the last occurrence of a specified element in the array, or -1 if not found.

  15. map(): Creates a new array with the results of calling a provided function on every element in the array.

  16. pop(): Removes the last element from the array and returns that element.

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

  18. reduce(): Applies a function to each element in the array, reducing the array to a single value.

  19. reduceRight(): Applies a function to each element in the array, reducing the array from right to left to a single value.

  20. reverse(): Reverses the order of the elements in the array.

  21. shift(): Removes the first element from the array and returns that element.

  22. slice(): Returns a shallow copy of a portion of an array into a new array object.

  23. some(): Checks if at least one element in the array passes a certain test.

  24. sort(): Sorts the elements of an array in place and returns the array.

  25. splice(): Adds or removes elements from an array.

  26. toLocaleString(): Returns a string representing the elements of the array.

  27. toString(): Returns a string representing the elements of the array.

  28. unshift(): Adds one or more elements to the beginning of the array and returns the new length of the array.

  29. values(): Returns a new array iterator object that contains the values for each index in the array.

These functions can be used to add, remove, modify, and search for elements in an array, as well as to manipulate the order and structure of the array itself. Using these functions effectively can make your code more concise and easier to read and maintain.