Array Methods Summary
Array Methods Summary
See Also:
Search Methods
Sort Methods
Iteration Methods Array shift()
Array unshift()
Array delete()
Array concat()
Array copyWithin()
Array flat()
Array splice()
Array toSpliced()
Array slice()
JavaScript Array length
The length property returns the length (size) of an array:
Example
const fruits = ["Banana", "Orange", "Apple", "Mango"];
let size = fruits.length;
JavaScript Array toString()
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 1/74
7/10/24, 12:53 PM Array Methods Summary
Example
const fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits.toString();
Result:
Banana,Orange,Apple,Mango
JavaScript Array at()
ES2022 intoduced the array method at():
Examples
Get the third element of fruits using at():
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 2/74
7/10/24, 12:53 PM Array Methods Summary
Apr 2021 Jul 2021 Jul 2021 Mar 2022 Aug 2021
Note
Many languages allows negative bracket indexing like [-1] to access
elements from the end of an object / array / string.
It behaves just like toString(), but in addition you can specify the
separator:
Example
const fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits.join(" * ");
Result:
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 3/74
7/10/24, 12:53 PM Array Methods Summary
ADVERTISEMENT
Example
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.pop();
The pop() method returns the value that was "popped out":
Example
const fruits = ["Banana", "Orange", "Apple", "Mango"];
let fruit = fruits.pop();
JavaScript Array push()
The push() method adds a new element to an array (at the end):
Example
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Kiwi");
The push() method returns the new array length:
Example
const fruits = ["Banana", "Orange", "Apple", "Mango"];
let length = fruits.push("Kiwi");
Shifting Elements
Shifting is equivalent to popping, but working on the first element
instead of the last.
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 4/74
7/10/24, 12:53 PM Array Methods Summary
Example
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.shift();
The shift() method returns the value that was "shifted out":
Example
const fruits = ["Banana", "Orange", "Apple", "Mango"];
let fruit = fruits.shift();
JavaScript Array unshift()
The unshift() method adds a new element to an array (at the
beginning), and "unshifts" older elements:
Example
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.unshift("Lemon");
The unshift() method returns the new array length:
Example
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.unshift("Lemon");
Changing Elements
Array elements are accessed using their index number:
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 5/74
7/10/24, 12:53 PM Array Methods Summary
Example
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits[0] = "Kiwi";
JavaScript Array length
The length property provides an easy way to append a new element
to an array:
Example
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits[fruits.length] = "Kiwi";
JavaScript Array delete()
Warning !
Using delete() leaves undefined holes in the array.
Example
const fruits = ["Banana", "Orange", "Apple", "Mango"];
delete fruits[0];
Merging Arrays (Concatenating)
In programming languages, concatenation means joining strings end-
to-end.
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 6/74
7/10/24, 12:53 PM Array Methods Summary
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 7/74
7/10/24, 12:53 PM Array Methods Summary
Examples
Copy to index 2, all elements from index 0:
The copyWithin() method does not change the length of the array.
Flattening an Array
Flattening an array is the process of reducing the dimensionality of an
array.
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 8/74
7/10/24, 12:53 PM Array Methods Summary
Example
const myArr = [[1,2],[3,4],[5,6]];
const newArr = myArr.flat();
Browser Support
JavaScript Array flat() is supported in all modern browsers since
January 2020:
Example
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2, 0, "Lemon", "Kiwi");
The first parameter (2) defines the position where new elements
should be added (spliced in).
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 9/74
7/10/24, 12:53 PM Array Methods Summary
The rest of the parameters ("Lemon" , "Kiwi") define the new elements
to be added.
Example
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2, 2, "Lemon", "Kiwi");
Using splice() to Remove Elements
With clever parameter setting, you can use splice() to remove
elements without leaving "holes" in the array:
Example
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(0, 1);
The first parameter (0) defines the position where new elements
should be added (spliced in).
The difference between the new toSpliced() method and the old
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 10/74
7/10/24, 12:53 PM Array Methods Summary
splice() method is that the new method creates a new array, keeping
the original array unchanged, while the old method altered the
original array.
Example
const months = ["Jan", "Feb", "Mar", "Apr"];
const spliced = months.toSpliced(0, 1);
JavaScript Array slice()
The slice() method slices out a piece of an array into a new array:
Example
Slice out a part of an array starting from array element 1 ("Orange"):
The slice() method does not remove any elements from the source
array.
Example
Slice out a part of an array starting from array element 3 ("Apple"):
The method then selects elements from the start argument, and up to
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 11/74
7/10/24, 12:53 PM Array Methods Summary
Example
const fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
const citrus = fruits.slice(1, 3);
If the end argument is omitted, like in the first examples, the slice()
method slices out the rest of the array.
Example
const fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
const citrus = fruits.slice(2);
Automatic toString()
JavaScript automatically converts an array to a comma separated
string when a primitive value is expected.
Example
const fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits.toString();
Example
const fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits;
Note
All JavaScript objects have a toString() method.
Searching Arrays
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 12/74
7/10/24, 12:53 PM Array Methods Summary
Sorting Arrays
Sorting arrays covers the methods used to sort arraysg.
Iterating Arrays
Iterating arrays covers methods that operate on all array elements.
make short notes including all the code provided of the topics
Sure, here are concise notes including all the provided code examples for the mentioned JavaScript
array methods:
Array length
Array toString()
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 13/74
7/10/24, 12:53 PM Array Methods Summary
Array join()
Array pop()
Array push()
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 14/74
7/10/24, 12:53 PM Array Methods Summary
push() adds elements to the end of an array and returns the new length.
Shifting Elements
Array shift()
shift() removes and returns the first element, shifting other elements down.
Array unshift()
unshift() adds elements to the beginning of an array and returns the new length.
Changing Elements
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 15/74
7/10/24, 12:53 PM Array Methods Summary
Array concat()
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 16/74
7/10/24, 12:53 PM Array Methods Summary
const myGirls = ["Cecilie", "Lone"]; const myBoys = ["Emil", "Tobias", "Linus"]; const
myChildren = myGirls.concat(myBoys); // ["Cecilie", "Lone", "Emil", "Tobias", "Linus"]
Array splice()
Array slice()
Automatic toString()
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 17/74
7/10/24, 12:53 PM Array Methods Summary
These notes cover the basic methods for manipulating arrays in JavaScript. Each method serves a
specific purpose, from accessing elements to modifying the array structure.
See Also:
Basic Methods
Sort Methods
Iteration Methods Array find()
Array findIndex()
Array findLast()
Array findLastIndex()
JavaScript Array indexOf()
The indexOf() method searches an array for an element value and
returns its position.
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 18/74
7/10/24, 12:53 PM Array Methods Summary
Note: The first item has position 0, the second item has position 1, and
so on.
Example
Search an array for the item "Apple":
If the item is present more than once, it returns the position of the
first occurrence.
Example
Search an array for the item "Apple":
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 19/74
7/10/24, 12:53 PM Array Methods Summary
array.lastIndexOf(item, start)
item Required. The item to search for
start Optional. Where to start the search. Negative values will start at
the given position counting from the end, and search to the beginning
JavaScript Array includes()
ECMAScript 2016 introduced Array.includes() to arrays. This allows us
to check if an element is present in an array (including NaN, unlike
indexOf).
Example
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.includes("Mango"); // is true
Syntax
array.includes(search-item)
Array.includes() allows to check for NaN values. Unlike Array.indexOf().
Browser Support
includes() is an ECMAScript 2016 feature.
ADVERTISEMENT
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 20/74
7/10/24, 12:53 PM Array Methods Summary
The find() method returns the value of the first array element that
passes a test function.
This example finds (returns the value of) the first element that is larger
than 18:
Example
const numbers = [4, 9, 16, 25, 29];
let first = numbers.find(myFunction);
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 21/74
7/10/24, 12:53 PM Array Methods Summary
This example finds the index of the first element that is larger than 18:
Example
const numbers = [4, 9, 16, 25, 29];
let first = numbers.findIndex(myFunction);
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 22/74
7/10/24, 12:53 PM Array Methods Summary
Example
const temp = [27, 28, 30, 40, 42, 35, 30];
let high = temp.findLast(x => x > 40);
Browser Support
findLast() is an ES2023 feature.
Example
const temp = [27, 28, 30, 40, 42, 35, 30];
let pos = temp.findLastIndex(x => x > 40);
Sure, here are concise notes including all the provided code examples for the mentioned JavaScript
array search methods:
Array indexOf()
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 23/74
7/10/24, 12:53 PM Array Methods Summary
indexOf() searches an array for a value and returns its first position, or -1 if not found.
Array lastIndexOf()
lastIndexOf() searches for a value and returns its last position, or -1 if not found.
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 24/74
7/10/24, 12:53 PM Array Methods Summary
Syntax: array.includes(search-item)
const numbers = [4, 9, 16, 25, 29]; let first = numbers.find(myFunction); function
myFunction(value, index, array) { return value > 18; }
const numbers = [4, 9, 16, 25, 29]; let first = numbers.findIndex(myFunction); function
myFunction(value, index, array) { return value > 18; }
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 25/74
7/10/24, 12:53 PM Array Methods Summary
const temp = [27, 28, 30, 40, 42, 35, 30]; let high = temp.findLast(x => x > 40); // 42
const temp = [27, 28, 30, 40, 42, 35, 30]; let pos = temp.findLastIndex(x => x > 40);
// 4
These notes cover various methods for searching within arrays in JavaScript. Each method serves a
specific purpose, from checking for the presence of an element to finding elements that meet
specific conditions.
Array toReversed()
Sorting Objects
See Also:
Basic Methods
Search Methods
Iteration Methods
Numeric Sort
Numeric Sort
Random Sort
Math.min()
Math.max()
Home made Min()
Home made Max()
Sorting an Array
The sort() method sorts an array alphabetically:
Example
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();
Reversing an Array
The reverse() method reverses the elements in an array:
Example
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.reverse();
By combining sort() and reverse(), you can sort an array in descending
order:
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 27/74
7/10/24, 12:53 PM Array Methods Summary
Example
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();
fruits.reverse();
JavaScript Array toSorted() Method
ES2023 added the toSorted() method as a safe way to sort an array
without altering the original array.
The difference between toSorted() and sort() is that the first method
creates a new array, keeping the original array unchanged, while the
last method alters the original array.
Example
const months = ["Jan", "Feb", "Mar", "Apr"];
const sorted = months.toSorted();
JavaScript Array toReversed() Method
ES2023 added the toReversed() method as a safe way to reverse an
array without altering the original array.
Example
const months = ["Jan", "Feb", "Mar", "Apr"];
const reversed = months.toReversed();
Numeric Sort
By default, the sort() function sorts values as strings.
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 28/74
7/10/24, 12:53 PM Array Methods Summary
If numbers are sorted as strings, "25" is bigger than "100", because "2"
is bigger than "1".
Because of this, the sort() method will produce incorrect result when
sorting numbers.
Example
const points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return a - b});
Use the same trick to sort an array descending:
Example
const points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return b - a});
ADVERTISEMENT
The Compare Function
The purpose of the compare function is to define an alternative sort
order.
function(a, b){return a - b}
When the sort() function compares two values, it sends the values to
the compare function, and sorts the values according to the returned
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 29/74
7/10/24, 12:53 PM Array Methods Summary
If the result is 0, no changes are done with the sort order of the two
values.
Example:
The compare function compares all the values in the array, two values
at a time (a, b).
When comparing 40 and 100, the sort() method calls the compare
function(40, 100).
The function calculates 40 - 100 (a - b), and since the result is negative
(-60), the sort function will sort 40 as a value lower than 100.
You can use this code snippet to experiment with numerically and
alphabetically sorting:
<p id="demo"></p>
<script>
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 30/74
7/10/24, 12:53 PM Array Methods Summary
function myFunction1() {
points.sort();
document.getElementById("demo").innerHTML = points;
}
function myFunction2() {
points.sort(function(a, b){return a - b});
document.getElementById("demo").innerHTML = points;
}
</script>
Sorting an Array in Random Order
Using a sort function, like explained above, you can sort an numeric
array in random order
Example
const points = [40, 100, 1, 5, 25, 10];
points.sort(function(){return 0.5 - Math.random()});
The most popular correct method, is called the Fisher Yates shuffle,
and was introduced in data science as early as 1938!
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 31/74
7/10/24, 12:53 PM Array Methods Summary
Example
const points = [40, 100, 1, 5, 25, 10];
Sort Ascending:
Example
const points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return a - b});
// now points[0] contains the lowest value
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 32/74
7/10/24, 12:53 PM Array Methods Summary
Example
function myArrayMin(arr) {
return Math.min.apply(null, arr);
}
Example
function myArrayMax(arr) {
return Math.max.apply(null, arr);
}
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 33/74
7/10/24, 12:53 PM Array Methods Summary
The fastest code to find the lowest number is to use a home made
method.
This function loops through an array comparing each value with the
lowest value found:
The fastest code to find the highest number is to use a home made
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 34/74
7/10/24, 12:53 PM Array Methods Summary
method.
This function loops through an array comparing each value with the
highest value found:
Example
const cars = [
{type:"Volvo", year:2016},
{type:"Saab", year:2001},
{type:"BMW", year:2010}
];
Even if objects have properties of different data types, the sort()
method can be used to sort the array.
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 35/74
7/10/24, 12:53 PM Array Methods Summary
Example
cars.sort(function(a, b){return a.year - b.year});
Comparing string properties is a little more complex:
Example
cars.sort(function(a, b){
let x = a.type.toLowerCase();
let y = b.type.toLowerCase();
if (x < y) {return -1;}
if (x > y) {return 1;}
return 0;
});
Stable Array sort()
ES2019 revised the Array sort() method.
Example
const myArr = [
{name:"X00",price:100 },
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 36/74
7/10/24, 12:53 PM Array Methods Summary
{name:"X01",price:100 },
{name:"X02",price:100 },
{name:"X03",price:100 },
{name:"X04",price:110 },
{name:"X05",price:110 },
{name:"X06",price:110 },
{name:"X07",price:110 }
];
In the example above, when sorting on price, the result is not allowed
to come out with the names in an other relative position like this:
X01 100
X03 100
X00 100
X03 100
X05 110
X04 110
X06 110
X07 110
Certainly! Here are concise notes for JavaScript sorting arrays, including code examples for each
mentioned method:
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 37/74
7/10/24, 12:53 PM Array Methods Summary
Alphabetic Sort
Reversing an Array
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 38/74
7/10/24, 12:53 PM Array Methods Summary
Numeric Sort
Default sort() function sorts values as strings, producing incorrect results for numbers. Provide
a compare function for numeric sorting.
Ascending Order
const points = [40, 100, 1, 5, 25, 10]; points.sort(function(a, b){ return a - b }); //
[1, 5, 10, 25, 40, 100]
Descending Order
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 39/74
7/10/24, 12:53 PM Array Methods Summary
const points = [40, 100, 1, 5, 25, 10]; points.sort(function(a, b){ return b - a }); //
[100, 40, 25, 10, 5, 1]
const points = [40, 100, 1, 5, 25, 10]; for (let i = points.length - 1; i > 0; i--) {
let j = Math.floor(Math.random() * (i + 1)); [points[i], points[j]] = [points[j],
points[i]]; }
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 40/74
7/10/24, 12:53 PM Array Methods Summary
Find Min
function myArrayMin(arr) { let min = Infinity; for (let i = 0; i < arr.length; i++) {
if (arr[i] < min) { min = arr[i]; } } return min; }
Find Max
function myArrayMax(arr) { let max = -Infinity; for (let i = 0; i < arr.length; i++) {
if (arr[i] > max) { max = arr[i]; } } return max; }
const cars = [ {type: "Volvo", year: 2016}, {type: "Saab", year: 2001}, {type: "BMW",
year: 2010} ]; cars.sort(function(a, b){ return a.year - b.year }); // Sorts by year
const cars = [ {type: "Volvo", year: 2016}, {type: "Saab", year: 2001}, {type: "BMW",
year: 2010} ]; cars.sort(function(a, b){ let x = a.type.toLowerCase(); let y =
b.type.toLowerCase(); if (x < y) { return -1; } if (x > y) { return 1; } return 0; });
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 41/74
7/10/24, 12:53 PM Array Methods Summary
const myArr = [ {name: "X00", price: 100}, {name: "X01", price: 100}, {name: "X02",
price: 100}, {name: "X03", price: 100}, {name: "X04", price: 110}, {name: "X05", price:
110}, {name: "X06", price: 110}, {name: "X07", price: 110} ]; // Sort by price,
maintaining relative order for same price myArr.sort(function(a, b){ return a.price -
b.price });
These notes cover various methods for sorting arrays and finding min/max values in JavaScript,
including handling numeric and object arrays.
2/2
Array forEach
Array map()
Array flatMap()
Array filter()
Array reduce()
Array reduceRight()
See Also:
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 42/74
7/10/24, 12:53 PM Array Methods Summary
Example
const numbers = [45, 4, 9, 16, 25];
let txt = "";
numbers.forEach(myFunction);
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 43/74
7/10/24, 12:53 PM Array Methods Summary
Example
const numbers = [45, 4, 9, 16, 25];
let txt = "";
numbers.forEach(myFunction);
function myFunction(value) {
txt += value + "<br>";
}
JavaScript Array map()
The map() method creates a new array by performing a function on
each array element.
The map() method does not execute the function for array elements
without values.
Example
const numbers1 = [45, 4, 9, 16, 25];
const numbers2 = numbers1.map(myFunction);
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 44/74
7/10/24, 12:53 PM Array Methods Summary
Example
const numbers1 = [45, 4, 9, 16, 25];
const numbers2 = numbers1.map(myFunction);
function myFunction(value) {
return value * 2;
}
JavaScript Array flatMap()
ES2019 added the Array flatMap() method to JavaScript.
The flatMap() method first maps all elements of an array and then
creates a new array by flattening the array.
Example
const myArr = [1, 2, 3, 4, 5, 6];
const newArr = myArr.flatMap((x) => x * 2);
Browser Support
JavaScript Array flatMap() is supported in all modern browsers since
January 2020:
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 45/74
7/10/24, 12:53 PM Array Methods Summary
This example creates a new array from elements with a value larger
than 18:
Example
const numbers = [45, 4, 9, 16, 25];
const over18 = numbers.filter(myFunction);
Example
const numbers = [45, 4, 9, 16, 25];
const over18 = numbers.filter(myFunction);
function myFunction(value) {
return value > 18;
}
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 46/74
7/10/24, 12:53 PM Array Methods Summary
The reduce() method works from left-to-right in the array. See also
reduceRight().
Example
const numbers = [45, 4, 9, 16, 25];
let sum = numbers.reduce(myFunction);
Example
const numbers = [45, 4, 9, 16, 25];
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 47/74
7/10/24, 12:53 PM Array Methods Summary
Example
const numbers = [45, 4, 9, 16, 25];
let sum = numbers.reduce(myFunction, 100);
Example
const numbers = [45, 4, 9, 16, 25];
let sum = numbers.reduceRight(myFunction);
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 48/74
7/10/24, 12:53 PM Array Methods Summary
Example
const numbers = [45, 4, 9, 16, 25];
let sum = numbers.reduceRight(myFunction);
This example checks if all array values are larger than 18:
Example
const numbers = [45, 4, 9, 16, 25];
let allOver18 = numbers.every(myFunction);
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 49/74
7/10/24, 12:53 PM Array Methods Summary
Example
const numbers = [45, 4, 9, 16, 25];
let allOver18 = numbers.every(myFunction);
function myFunction(value) {
return value > 18;
}
JavaScript Array some()
The some() method checks if some array values pass a test.
This example checks if some array values are larger than 18:
Example
const numbers = [45, 4, 9, 16, 25];
let someOver18 = numbers.some(myFunction);
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 50/74
7/10/24, 12:53 PM Array Methods Summary
Example
Create an Array from a String:
Array.from("ABCDEFG");
Browser Support
from() is an ES6 feature (JavaScript 2015).
Example
Create an Array Iterator object, containing the keys of the array:
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 51/74
7/10/24, 12:53 PM Array Methods Summary
for (let x of f) {
document.getElementById("demo").innerHTML += x;
}
The entries() method returns an Array Iterator object with key/value
pairs:
[0, "Banana"]
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 52/74
7/10/24, 12:53 PM Array Methods Summary
[1, "Orange"]
[2, "Apple"]
[3, "Mango"]
Browser Support
entries() is an ES6 feature (JavaScript 2015).
Example
const months = ["Januar", "Februar", "Mar", "April"];
const myMonths = months.with(2, "March");
JavaScript Array Spread (...)
The ... operator expands an iterable (like an array) into more elements:
Example
const q1 = ["Jan", "Feb", "Mar"];
const q2 = ["Apr", "May", "Jun"];
const q3 = ["Jul", "Aug", "Sep"];
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 53/74
7/10/24, 12:53 PM Array Methods Summary
Array forEach()
Array map()
Array flatMap()
Array filter()
Array reduce()
Array reduceRight()
See Also:
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 54/74
7/10/24, 12:53 PM Array Methods Summary
Array every()
Array some()
Array from()
Array keys()
Array entries()
Array with()
const numbers = [45, 4, 9, 16, 25]; let txt = ""; numbers.forEach(myFunction); function
myFunction(value, index, array) { txt += value + "<br>"; }
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 55/74
7/10/24, 12:53 PM Array Methods Summary
const numbers = [45, 4, 9, 16, 25]; let txt = ""; numbers.forEach(myFunction); function
myFunction(value) { txt += value + "<br>"; }
Example:
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 56/74
7/10/24, 12:53 PM Array Methods Summary
Example:
Example:
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 57/74
7/10/24, 12:53 PM Array Methods Summary
Example:
const numbers = [45, 4, 9, 16, 25]; let sum = numbers.reduce(myFunction, 100); function
myFunction(total, value) { return total + value; }
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 58/74
7/10/24, 12:53 PM Array Methods Summary
The reduceRight() method runs a function on each array element to produce a single value, working
from right to left.
Example:
Example:
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 59/74
7/10/24, 12:53 PM Array Methods Summary
Example:
JavaScript Array.from()
The Array.from() method returns an Array object from any object with a length property or any
iterable object.
Example:
Array.from("ABCDEFG");
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 60/74
7/10/24, 12:53 PM Array Methods Summary
Example:
const fruits = ["Banana", "Orange", "Apple", "Mango"]; const keys = fruits.keys(); for
(let x of keys) { text += x + "<br>"; }
Example:
Example:
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 61/74
7/10/24, 12:53 PM Array Methods Summary
Example:
const q1 = ["Jan", "Feb", "Mar"]; const q2 = ["Apr", "May", "Jun"]; const q3 = ["Jul",
"Aug", "Sep"]; const q4 = ["Oct", "Nov", "May"]; const year = [...q1, ...q2, ...q3,
...q4];
Browser Support
Most of the mentioned methods are ES6 features (JavaScript 2015) and are fully supported in all
modern browsers since June 2017. Here's a summary:
Chrome 69
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 62/74
7/10/24, 12:53 PM Array Methods Summary
Edge 79
Firefox 62
Safari 12
Opera 56
JavaScript Array.from()
Chrome 51
Edge 15
Firefox 54
Safari 10
Opera 38
Chrome 51
Edge 15
Firefox 54
Safari 10
Opera 38
Chrome 51
Edge 15
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 63/74
7/10/24, 12:53 PM Array Methods Summary
Firefox 54
Safari 10
Opera 38
Chrome 51
Edge 15
Firefox 54
Safari 10
Opera 38
Note that some of these methods are not supported in Internet Explorer.
Example
const cars = ["Saab", "Volvo", "BMW"];
Cannot be Reassigned
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 64/74
7/10/24, 12:53 PM Array Methods Summary
Example
const cars = ["Saab", "Volvo", "BMW"];
cars = ["Toyota", "Volvo", "Audi"]; // ERROR
Arrays are Not Constants
The keyword const is a little misleading.
Example
// You can create a constant array:
const cars = ["Saab", "Volvo", "BMW"];
The following table defines the first browser versions with full support
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 65/74
7/10/24, 12:53 PM Array Methods Summary
Example
This will not work:
const cars;
cars = ["Saab", "Volvo", "BMW"];
Arrays declared with var can be initialized at any time.
Example
This is OK:
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 66/74
7/10/24, 12:53 PM Array Methods Summary
Example
const cars = ["Saab", "Volvo", "BMW"];
// Here cars[0] is "Saab"
{
const cars = ["Toyota", "Volvo", "BMW"];
// Here cars[0] is "Toyota"
}
// Here cars[0] is "Saab"
An array declared with var does not have block scope:
Example
var cars = ["Saab", "Volvo", "BMW"];
// Here cars[0] is "Saab"
{
var cars = ["Toyota", "Volvo", "BMW"];
// Here cars[0] is "Toyota"
}
// Here cars[0] is "Toyota"
You can learn more about Block Scope in the chapter: JavaScript
Scope.
ADVERTISEMENT
Redeclaring Arrays
Redeclaring an array declared with var is allowed anywhere in a
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 67/74
7/10/24, 12:53 PM Array Methods Summary
program:
Example
var cars = ["Volvo", "BMW"]; // Allowed
var cars = ["Toyota", "BMW"]; // Allowed
cars = ["Volvo", "Saab"]; // Allowed
Redeclaring or reassigning an array to const, in the same scope, or in
the same block, is not allowed:
Example
var cars = ["Volvo", "BMW"]; // Allowed
const cars = ["Volvo", "BMW"]; // Not allowed
{
var cars = ["Volvo", "BMW"]; // Allowed
const cars = ["Volvo", "BMW"]; // Not allowed
}
Redeclaring or reassigning an existing const array, in the same scope,
or in the same block, is not allowed:
Example
const cars = ["Volvo", "BMW"]; // Allowed
const cars = ["Volvo", "BMW"]; // Not allowed
var cars = ["Volvo", "BMW"]; // Not allowed
cars = ["Volvo", "BMW"]; // Not allowed
{
const cars = ["Volvo", "BMW"]; // Allowed
const cars = ["Volvo", "BMW"]; // Not allowed
var cars = ["Volvo", "BMW"]; // Not allowed
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 68/74
7/10/24, 12:53 PM Array Methods Summary
Example
const cars = ["Volvo", "BMW"]; // Allowed
{
const cars = ["Volvo", "BMW"]; // Allowed
}
{
const cars = ["Volvo", "BMW"]; // Allowed
}
In 2015, JavaScript introduced an important new keyword: const. It has become a common practice
to declare arrays using const.
Example:
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 69/74
7/10/24, 12:53 PM Array Methods Summary
Cannot be Reassigned
Example:
const cars = ["Saab", "Volvo", "BMW"]; cars = ["Toyota", "Volvo", "Audi"]; // ERROR
The keyword const is a little misleading. It does NOT define a constant array. It defines a constant
reference to an array. Because of this, we can still change the elements of a constant array.
Example:
// You can create a constant array: const cars = ["Saab", "Volvo", "BMW"]; // You can
change an element: cars[0] = "Toyota"; // You can add an element: cars.push("Audi");
Browser Support
The const keyword is not supported in Internet Explorer 10 or earlier. The following table defines the
first browser versions with full support for the const keyword:
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 70/74
7/10/24, 12:53 PM Array Methods Summary
JavaScript const variables must be assigned a value when they are declared. An array declared with
const must be initialized when it is declared. Using const without initializing the array is a syntax
error.
Example:
const cars; // This will not work cars = ["Saab", "Volvo", "BMW"];
Arrays declared with var can be initialized at any time. You can even use the array before it is
declared.
Example:
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 71/74
7/10/24, 12:53 PM Array Methods Summary
An array declared with const has Block Scope. An array declared in a block is not the same as an
array declared outside the block.
Example:
const cars = ["Saab", "Volvo", "BMW"]; // Here cars[0] is "Saab" { const cars =
["Toyota", "Volvo", "BMW"]; // Here cars[0] is "Toyota" } // Here cars[0] is "Saab"
Example:
var cars = ["Saab", "Volvo", "BMW"]; // Here cars[0] is "Saab" { var cars = ["Toyota",
"Volvo", "BMW"]; // Here cars[0] is "Toyota" } // Here cars[0] is "Toyota"
Redeclaring Arrays
Example:
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 72/74
7/10/24, 12:53 PM Array Methods Summary
var cars = ["Volvo", "BMW"]; // Allowed var cars = ["Toyota", "BMW"]; // Allowed cars =
["Volvo", "Saab"]; // Allowed
Redeclaring or reassigning an array to const, in the same scope, or in the same block, is not allowed.
Example:
var cars = ["Volvo", "BMW"]; // Allowed const cars = ["Volvo", "BMW"]; // Not allowed {
var cars = ["Volvo", "BMW"]; // Allowed const cars = ["Volvo", "BMW"]; // Not allowed }
Redeclaring or reassigning an existing const array, in the same scope, or in the same block, is not
allowed.
Example:
const cars = ["Volvo", "BMW"]; // Allowed const cars = ["Volvo", "BMW"]; // Not allowed
var cars = ["Volvo", "BMW"]; // Not allowed cars = ["Volvo", "BMW"]; // Not allowed {
const cars = ["Volvo", "BMW"]; // Allowed const cars = ["Volvo", "BMW"]; // Not allowed
var cars = ["Volvo", "BMW"]; // Not allowed cars = ["Volvo", "BMW"]; // Not allowed }
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 73/74
7/10/24, 12:53 PM Array Methods Summary
Example:
const cars = ["Volvo", "BMW"]; // Allowed { const cars = ["Volvo", "BMW"]; // Allowed }
{ const cars = ["Volvo", "BMW"]; // Allowed }
https://chatgpt.com/c/d16487fb-ae44-42df-ace7-9437ed30768e 74/74