Learn JavaScript
  • Introduction
  • Basics
    • Comments
    • Variables
    • Types
    • Equality
  • Numbers
    • Math
    • Basic Operators
    • Advanced Operators
  • Strings
    • Creation
    • Replace
    • Length
    • Concatenation
  • Conditional Logic
    • If
    • Else
    • Switch
    • Comparators
    • Concatenate
  • Arrays
    • Unshift
    • Map
    • Spread
    • Shift
    • Pop
    • Join
    • Length
    • Push
    • For Each
    • Sort
    • Indices
  • Loops
    • For
    • While
    • Do...While
  • Functions
    • Higher Order Functions
  • Objects
    • Properties
    • Mutable
    • Reference
    • Prototype
    • Delete
    • Enumeration
    • Global footprint
  • Linked List
    • Add
    • Pop
    • Prepend
    • Shift
  • Browser Object Model (BOM)
    • Window
    • Popup
    • Screen
    • Navigator
    • Cookies
    • History
    • Location
  • Date and Time
  • JSON
  • Error Handling
    • try...catch...finally
  • Events
  • Regular Expression
  • Modules
  • Debugging
  • Classes
    • Static
    • Inheritance
    • Access Modifiers
  • Promise, async/await
    • Async/Await
  • Miscellaneous
    • Hoisting
    • Currying
    • Polyfills and Transpilers
  • Exercises
    • Console
    • Multiplication
    • User Input Variables
    • Constants
    • Concatenation
    • Functions
    • Conditional Statements
    • Objects
    • FizzBuzz Problem
    • Get the Titles!
Powered by GitBook
On this page

Was this helpful?

Arrays

PreviousConcatenateNextUnshift

Last updated 2 years ago

Was this helpful?

Arrays are a fundamental part of programming. An array is a list of data. We can store a lot of data in one variable, which makes our code more readable and easier to understand. It also makes it much easier to perform functions on related data.

The data in arrays are called elements.

Here is a simple array:

// 1, 1, 2, 3, 5, and 8 are the elements in this array
let numbers = [1, 1, 2, 3, 5, 8];

Arrays can be created easily using array literals or with a new keyword.

const cars = ["Saab", "Volvo", "BMW"]; // using array literals
const cars = new Array("Saab", "Volvo", "BMW"); // using the new keyword

An index number is used to access the values of an array. The index of the first element in an array is always 0 as array indexes start with 0. The index number can also be used to change the elements of an array.

const cars = ["Saab", "Volvo", "BMW"];
console.log(cars[0]); 
// Result: Saab

cars[0] = "Opel"; // changing the first element of an array
console.log(cars);
// Result: ['Opel', 'Volvo', 'BMW']

Arrays are a special type of object. One can have in an array.

The length property of an array returns the count of numbers elements. Methods supported by Arrays are shown below:

Name
Description

concat()

Returns two or more combined arrays

join()

Joins all elements in an array into a string

push()

Adds one or more elements at the end of the array and returns the length

pop()

Removes the last element of an array and returns that element

shift()

Removes the first element of an array and returns that element

unshift()

Adds one or more elements at the front of an array and returns the length

slice()

Extracts the section of an array and returns the new array

at()

Returns element at the specified index or undefined

splice()

Removes elements from an array and (optionally) replaces them, and returns the array

reverse()

Transposes the elements of an array and returns a reference to an array

flat()

Returns a new array with all sub-array elements concatenated into it recursively up to the specified depth

sort()

Sorts the elements of an array in place, and returns a reference to the array

indexOf()

Returns the index of the first match of the search element

lastIndexOf()

Returns the index of the last match of the search element

forEach()

Executes a callback in each element of an array and returns undefined

map()

Returns a new array with a return value from executing callback on every array item.

flatMap()

Runs map() followed by flat() of depth 1

filter()

Returns a new array containing the items for which callback returned true

find()

Returns the first item for which callback returned true

findLast()

Returns the last item for which callback returned true

findIndex()

Returns the index of the first item for which callback returned true

findLastIndex()

Returns the index of the last item for which callback returned true

every()

Returns true if callback returns true for every item in the array

some()

Returns true if callback returns true for at least one item in the array

reduce()

Uses callback(accumulator, currentValue, currentIndex, array) for reducing purpose and returns the final value returned by callback function

reduceRight()

Works similarly lie reduce() but starts with the last element

objects