Array Methods Part 2
Part of the Fundamentals section of Coddy's JavaScript journey — lesson 61 of 77.
Arrays have more built-in methods that can help us with common tasks. Here are some of them:
sort()- sorts the elements of an array.
reverse()- reverses the order of elements in an array.
indexOf(element)- returns the index of the first occurrence of an element in the array, or -1 if it's not found.
lastIndexOf(element)- returns the index of the last occurrence of an element in the array, or -1 if it's not found.
includes(element)- returnstrueif the array contains the specified element, otherwise returnsfalse.
Here is an example of how to use the sort method:
let myArray = ["orange", "banana", "apple"];
myArray.sort();
console.log(myArray);This will output ["apple", "banana", "orange"].
Example of the reverse method:
let myArray = [1, 2, 3, 4, 5];
myArray.reverse();
console.log(myArray);This will output [5, 4, 3, 2, 1].
Example of the indexOf method:
let myArray = [10, 20, 30, 40, 50, 30];
let index = myArray.indexOf(30);
console.log(index);This will output 2.
Example of the lastIndexOf method:
let myArray = [10, 20, 30, 40, 50, 30];
let index = myArray.lastIndexOf(30);
console.log(index);This will output 5.
Example of the includes method:
let myArray = ["apple", "banana", "orange"];
let hasBanana = myArray.includes("banana");
console.log(hasBanana);This will output true.
Challenge
EasyCreate a function named findElement that receives two arguments:
- An array of numbers
- A number to search for
The function should do the following:
- Check if the array includes the number using the
includesmethod. - If the number is found, return the index of its first occurrence using the
indexOfmethod. - If the number is not found, return -1.
For example:
findElement([1, 2, 3, 4, 5], 3)should return 2findElement([1, 2, 3, 4, 5], 6)should return -1
Cheat sheet
JavaScript arrays have several built-in methods for common operations:
sort()- sorts the elements of an arrayreverse()- reverses the order of elements in an arrayindexOf(element)- returns the index of the first occurrence of an element, or -1 if not foundlastIndexOf(element)- returns the index of the last occurrence of an element, or -1 if not foundincludes(element)- returnstrueif the array contains the element, otherwisefalse
Examples:
let fruits = ["orange", "banana", "apple"];
fruits.sort(); // ["apple", "banana", "orange"]
let numbers = [1, 2, 3, 4, 5];
numbers.reverse(); // [5, 4, 3, 2, 1]
let arr = [10, 20, 30, 40, 50, 30];
arr.indexOf(30); // 2 (first occurrence)
arr.lastIndexOf(30); // 5 (last occurrence)
arr.includes(30); // trueTry it yourself
function findElement(arr, num) {
// Write code here
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Type Coercion7Bill Split Calculator
Welcome MessageCalculating The Tip And Total2Variables
NumbersStringBooleanNaming ConventionsEmpty VariablesRecap - Initialize VariablesConstants11Arrays Part 1
Declaring an ArrayAccessing Array ElementsModifying ArraysArray Methods Part 1Array Methods Part 2Recap - Array Processor3Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsComparison OperatorsStrict vs Loose EqualityRecap - Simple Math6Basic IO
OutputOutput with VariablesType Conversion - Part 1Type Conversion - Part 2Recap - Till 120Recap - True or False