- JavaScript Introduction
- JavaScript Where To
- JavaScript Output
- JavaScript Statements
- JavaScript Syntax
- JavaScript Comments
- JavaScript Variables
- JavaScript Let
- JavaScript Const
- JavaScript Operators
- JavaScript Assignment
- JavaScript Data Types
- JavaScript Functions
- JavaScript Objects
- JavaScript Events
- JavaScript Strings
- JavaScript String Methods
- JavaScript Numbers
- JavaScript Number Methods
- JavaScript Arrays
- JavaScript Array Const
- JavaScript Array Methods
- JavaScript Sorting Arrays
- JavaScript Array Iteration
- JavaScript Date Objects
- JavaScript Date Formats
- JavaScript Get Date Methods
- JavaScript Set Date Methods
- JavaScript Math Object
- JavaScript Random
- JavaScript Booleans
- JavaScript Comparison And Logical Operators
- JavaScript If Else And Else If
- JavaScript Switch Statement
- JavaScript For Loop
- JavaScript Break And Continue
- JavaScript Type Conversion
- JavaScript Bitwise Operations
- JavaScript Regular Expressions
- JavaScript Errors
- JavaScript Scope
- JavaScript Hoisting
- JavaScript Use Strict
- The JavaScript This Keyword
- JavaScript Arrow Function
- JavaScript Classes
- JavaScript JSON
- JavaScript Debugging
- JavaScript Style Guide
- JavaScript Common Mistakes
- JavaScript Performance
- JavaScript Reserved Words
- JavaScript Versions
- JavaScript History
- JavaScript Forms
- JavaScript Validation API
- JavaScript Objects
- JavaScript Object Properties
- JavaScript Function Definitions
- JavaScript Function Parameters
- JavaScript Function Invocation
- JavaScript Closures
- JavaScript Classes
- Java Script Async
- JavaScript HTML DOM
- The Browser Object Model
- JS Ajax
- JavaScript JSON
- JavaScript Web APIs
- JS Vs JQuery
JavaScript Array Iteration
Array.forEach()
The forEach()
method calls a function (a callback function) once for each array element.
Example
let txt = "";
numbers.forEach(myFunction);
function myFunction(value, index, array) {
txt += value + "<br>";
}
Note that the function takes 3 arguments:
- The item value
- The item index
- The array itself
The example above uses only the value parameter. The example can be rewritten to:
Example
let txt = "";
numbers.forEach(myFunction);
function myFunction(value) {
txt += value + "<br>";
}
Array.forEach()
is supported in all browsers except Internet Explorer 8 or earlier. Practice Excercise Practice now
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.
The map()
method does not change the original array.
This example multiplies each array value by 2:
Example
const numbers2 = numbers1.map(myFunction);
function myFunction(value, index, array) {
return value * 2;
}
Note that the function takes 3 arguments:
- The item value
- The item index
- The array itself
When a callback function uses only the value parameter, the index and array parameters can be omitted:
Example
const numbers2 = numbers1.map(myFunction);
function myFunction(value) {
return value * 2;
}
Array.map()
is supported in all browsers except Internet Explorer 8 or earlier. Practice Excercise Practice now
Array.filter()
The filter()
method creates a new array with array elements that passes a test.
This example creates a new array from elements with a value larger than 18:
Example
const over18 = numbers.filter(myFunction);
function myFunction(value, index, array) {
return value > 18;
}
Note that the function takes 3 arguments:
- The item value
- The item index
- The array itself
In the example above, the callback function does not use the index and array parameters, so they can be omitted:
Example
const over18 = numbers.filter(myFunction);
function myFunction(value) {
return value > 18;
}
Array.filter()
is supported in all browsers except Internet Explorer 8 or earlier. Practice Excercise Practice now
Array.reduce()
The reduce()
method runs a function on each array element to produce (reduce it to) a single value.
The reduce()
method works from left-to-right in the array. See also reduceRight()
.
The reduce()
method does not reduce the original array.
This example finds the sum of all numbers in an array:
Example
let sum = numbers.reduce(myFunction);
function myFunction(total, value, index, array) {
return total + value;
}
Note that the function takes 4 arguments:
- The total (the initial value / previously returned value)
- The item value
- The item index
- The array itself
The example above does not use the index and array parameters. It can be rewritten to:
Example
let sum = numbers.reduce(myFunction);
function myFunction(total, value) {
return total + value;
}
The reduce()
method can accept an initial value:
Example
let sum = numbers.reduce(myFunction, 100);
function myFunction(total, value) {
return total + value;
}
Array.reduce()
is supported in all browsers except Internet Explorer 8 or earlier. Practice Excercise Practice now
Array.reduceRight()
The reduceRight()
method runs a function on each array element to produce (reduce it to) a single value.
The reduceRight()
works from right-to-left in the array. See also reduce()
.
The reduceRight()
method does not reduce the original array.
This example finds the sum of all numbers in an array:
Example
let sum = numbers1.reduceRight(myFunction);
function myFunction(total, value, index, array) {
return total + value;
}
Note that the function takes 4 arguments:
- The total (the initial value / previously returned value)
- The item value
- The item index
- The array itself
The example above does not use the index and array parameters. It can be rewritten to:
Example
let sum = numbers1.reduceRight(myFunction);
function myFunction(total, value) {
return total + value;
}
Array.reduceRight()
is supported in all browsers except Internet Explorer 8 or earlier. Practice Excercise Practice now
Array.every()
The every()
method check if all array values pass a test.
This example check if all array values are larger than 18:
Example
let allOver18 = numbers.every(myFunction);
function myFunction(value, index, array) {
return value > 18;
}
Note that the function takes 3 arguments:
- The item value
- The item index
- The array itself
When a callback function uses the first parameter only (value), the other parameters can be omitted:
Example
let allOver18 = numbers.every(myFunction);
function myFunction(value) {
return value > 18;
}
Array.every()
is supported in all browsers except Internet Explorer 8 or earlier. Practice Excercise Practice now
Array.some()
The some()
method check if some array values pass a test.
This example check if some array values are larger than 18:
Example
let someOver18 = numbers.some(myFunction);
function myFunction(value, index, array) {
return value > 18;
}
Note that the function takes 3 arguments:
- The item value
- The item index
- The array itself
Array.some()
is supported in all browsers except Internet Explorer 8 or earlier.
Practice Excercise Practice now
Array.indexOf()
The indexOf()
method searches an array for an element value and returns its position.
Note: The first item has position 0, the second item has position 1, and so on.
Example
Search an array for the item "Apple":
let position = fruits.indexOf("Apple") + 1;
Array.indexOf()
is supported in all browsers except Internet Explorer 8 or earlier.
Syntax
array.indexOf(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 end. |
Array.indexOf()
returns -1 if the item is not found.
If the item is present more than once, it returns the position of the first occurrence.
Practice Excercise Practice now
Array.lastIndexOf()
Array.lastIndexOf()
is the same as Array.indexOf()
, but returns the position of the last occurrence of the specified element.
Example
Search an array for the item "Apple":
let position = fruits.lastIndexOf("Apple") + 1;
Array.lastIndexOf()
is supported in all browsers except Internet Explorer 8 or earlier.
Syntax
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 |
Practice Excercise Practice now
Array.find()
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
let first = numbers.find(myFunction);
function myFunction(value, index, array) {
return value > 18;
}
Note that the function takes 3 arguments:
- The item value
- The item index
- The array itself
Array.find()
is not supported in older browsers. The first browser versions with full support is listed below.
Practice Excercise Practice now
Array.findIndex()
The findIndex()
method returns the index of the first array element that passes a test function.
This example finds the index of the first element that is larger than 18:
Example
let first = numbers.findIndex(myFunction);
function myFunction(value, index, array) {
return value > 18;
}
Note that the function takes 3 arguments:
- The item value
- The item index
- The array itself
Array.findIndex()
is not supported in older browsers. The first browser versions with full support is listed below.
Practice Excercise Practice now
Products
Partner
Copyright © RVR Innovations LLP 2024 | All rights reserved - Mytat.co is the venture of RVR Innovations LLP