revision:
Array.from() : converts an array-like object or iterable object to an array.
syntax: Array.from('hello'); // ['h', 'e', 'l', 'l', 'o']
Array.of() : creates a new array with variable number of arguments.
syntax: Array.of(1, 2, 3); // [1, 2, 3]
concat() : returns a new array comprised of this array joined with other array(s) and/or value(s).
syntax: const arr1 = [1, 2]; const arr2 = [3, 4]; const merged = arr1.concat(arr2); // [1, 2, 3, 4]
copyWithin() : copies a part of the same array within the same calling array.
syntax: [1, 2, 3, 4, 5].copyWithin(0, 3); // [4, 5, 3, 4, 5]
entries() : returns a new Array Iterator object that contain the key/value pairs for each index in the array.
syntax: const arr = ['a', 'b', 'c']; const iterator = arr.entries(); console.log(iterator.next().value); // [0, 'a'] console.log(iterator.next().value); // [1, 'b'] console.log(iterator.next().value); // [2, 'c']
every() : returns true if every element in this array satisfies the provided testing function.
syntax: [1, 2, 3].every(x => x > 0); // true
fill() : returns a modified array by filling a specified index with the specified value.
syntax: [1, 2, 3].fill(0); // [0, 0, 0]
filter() : creates a new array with all of the elements of this array for which the provided filtering function returns true.
syntax: const evens = [1, 2, 3, 4].filter(x => x % 2 === 0); // [2, 4]
find() : returns the first value of the array elements which satisfies the provided condition.
syntax: const found = [5, 12, 8, 13].find(x => x > 10); // 12
findIndex() : returns the index value of the element, not the value itself.
syntax: const index = [5, 12, 8, 13].findIndex(x => x > 10); // 1
flat() : flattens nested arrays.
syntax: [1, [2, [3, 4]]].flat(); // [1, 2, [3, 4]] [1, [2, [3, 4]]].flat(2); // [1, 2, 3, 4]
flatMap() : maps each element, then flattens the result into a new array.
syntax: [1, 2, 3].flatMap(x => [x, x * 2]); // [1, 2, 2, 4, 3, 6]
forEach() : calls a function for each element in the array.
syntax: const arr = [1, 2, 3]; arr.forEach((num) => console.log(num)); // Output: 1 2 3 (each on a new line)
includes() : determines whether certain values exist in the array or not. If the value exists in the array then the method returns true else return false.
syntax: console.log([1, 2, 3].includes(2)); // true
indexOf() : returns the first (least) index of an element within the array equal to the specified value, or -1 if none is found.
syntax: [1, 2, 3, 2].indexOf(2); // 1
isArray() : returns true if the passed argument is an array else returns false.
syntax: console.log(Array.isArray([1, 2, 3])); // true console.log(Array.isArray("Hello")); // false console.log(Array.isArray({})); // false console.log(Array.isArray(null)); // false console.log(Array.isArray(undefined)); // false console.log(Array.isArray(new Array())); // true console.log(Array.isArray(Array.prototype)); // true (since prototype is also an array)
join() : joins all elements of an array into a string.
syntax: ['a', 'b', 'c'].join('-'); // "a-b-c"
lastIndexOf() : returns the last (greatest) index of an element within the array equal to the specified value, or -1 if none is found.
syntax: [1, 2, 3, 2].lastIndexOf(2); // 3
map() : creates a new array with the results of calling a provided function on every element in this array.
syntax: const doubled = [1, 2, 3].map(x => x * 2); // [2, 4, 6]
pop() : removes the last element from an array and returns that element.
syntax: let arr = [1, 2]; arr.push(3); // [1, 2, 3] arr.pop(); // returns 3, arr becomes [1, 2]
push() : adds one or more elements to the end of an array and returns the new length of the array.
syntax: let arr = [1, 2]; arr.push(3); // [1, 2, 3] arr.pop(); // returns 3, arr becomes [1, 2]
reduce() : applies a function simultaneously against two values of the array (from left-to-right) as to reduce it to a single value.
syntax: const sum = [1, 2, 3].reduce((acc, curr) => acc + curr, 0); // 6
reduceRight() : applies a function simultaneously against two values of the array (from right-to-left) as to reduce it to a single value.
syntax: const numbers = [1, 2, 3, 4]; const sum = numbers.reduceRight((acc, curr) => acc + curr); console.log(sum); // 10 const letters = ['a', 'b', 'c']; const result = letters.reduceRight((acc, curr) => acc + curr); console.log(result); // "cba"
reverse() : reverses the order of the elements of an array -- the first becomes the last, and the last becomes the first.
syntax: [1, 2, 3].reverse(); // [3, 2, 1]
shift() : removes the first element from an array and returns that element.
syntax: let arr = [1, 2]; arr.unshift(0); // [0, 1, 2] arr.shift(); // returns 0, arr becomes [1, 2]
slice() : extracts a section of an array and returns a new array.
syntax: const arr = [1, 2, 3, 4]; const sub = arr.slice(1, 3); // [2, 3]
some() : returns true if at least one element in this array satisfies the provided testing function.
syntax: [1, 2, 3].some(x => x > 2); // true
sort() : sorts the elements of an array.
syntax: [3, 1, 2].sort(); // [1, 2, 3] [3, 1, 2].sort((a, b) => a - b); // ascending [3, 1, 2].sort((a, b) => b - a); // descending
splice() : adds and/or removes elements from an array.
syntax: let arr = [1, 2, 3, 4]; arr.splice(1, 2); // removes elements starting at index 1, remove count 2 → [2, 3] // arr now is [1, 4]
toSource() : represents the source code of an object.
syntax: const arr = [1, 2, 3]; console.log(arr.toSource()); // Outputs: "[1, 2, 3]"
toString() : returns a string representing the array and its elements.
syntax: const fruits = ['apple', 'banana', 'cherry']; console.log(fruits.toString()); // "apple,banana,cherry"
unshift() : adds one or more elements to the front of an array and returns the new length of the array.
syntax: let arr = [1, 2]; arr.unshift(0); // [0, 1, 2] arr.shift(); // returns 0, arr becomes [1, 2]
values() : returns a new array iterator that contains values of each index in the array.
syntax: const fruits = ['apple', 'banana', 'cherry']; const iterator = fruits.values(); console.log(iterator.next()); // { value: 'apple', done: false } console.log(iterator.next()); // { value: 'banana', done: false } console.log(iterator.next()); // { value: 'cherry', done: false } console.log(iterator.next()); // { value: undefined, done: true }
<div> <a id="array1"></a><br><br> <a id="array2"></a><br><br> <a id="array3"></a><br><br> <a id="array4"></a><br><br> <a id="array5"></a><br><br> <a id="array6"></a><br><br> <a id="array7"></a><br><br> <a id="array8"></a><br><br> <a id="array9"></a><br><br> <a id="array10"></a><br><br> <a id="array11"></a><br><br> <a id="array12"></a><br><br> </div> <script> var students = [ 'Jack', ' James', ' Robert', ' John', ' Lea', ' Christine', ' Thea']; document.getElementById("array1").innerHTML = "students array : " + students; students.pop(); document.getElementById("array2").innerHTML = "pop() effect: " + students; students.shift(); document.getElementById("array3").innerHTML = "shift() effect: " + students; students.push(' Oscar', ' Brigitte'); document.getElementById("array4").innerHTML = "push() effect: " + students; students.unshift("Peter"); document.getElementById("array5").innerHTML = "unshift() effect: " + students; var length = students.length; document.getElementById("array6").innerHTML = "length property: " + length; students.splice(3,2, ' Teddy', ' Eric') document.getElementById("array7").innerHTML = "splice() effect: " + students; var newStudent = students.slice(4,6) document.getElementById("array8").innerHTML = "slice() effect: " + newStudent; var others = ['Wim', 'Alice'] var all = students.concat(others); document.getElementById("array9").innerHTML = "concat() effect: " + all; document.getElementById("array10").innerHTML = "toString() effect: " + students.toString(); var minus = delete students[0]; document.getElementById("array11").innerHTML = "delete() effect: " + students; </script>