
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Implementing the Array.prototype.lastIndexOf() function in JavaScript
The lastIndexOf() function in JS returns the index of the very last occurrence of the element, passed into it as an argument, in the array, if it exists. If it does not exist the function returns -1.
For example −
[3, 5, 3, 6, 6, 7, 4, 3, 2, 1].lastIndexOf(3) would return 7.
We are required to write a JavaScript function that have the same utility as the existing lastIndexOf() function.
And then we have to override the default lastIndexOf() function with the function we just created. We will simply iterate from the back until we find the element and return its index.
If we do not find the element, we return -1.
Example
Following is the code −
const arr = [3, 5, 3, 6, 6, 7, 4, 3, 2, 1]; Array.prototype.lastIndexOf = function(el){ for(let i = this.length - 1; i >= 0; i--){ if(this[i] !== el){ continue; }; return i; }; return -1; }; console.log(arr.lastIndexOf(3));
Output
This will produce the following output in console −
7
- Related Articles
- JavaScript Array prototype Constructor
- JavaScript function that lives on the prototype object of the Array class
- Implementing custom function like String.prototype.split() function in JavaScript
- Ints lastIndexOf() function in Java
- Implementing a custom function like Array.prototype.filter() function in JavaScript
- Importance of function prototype in C
- Implementing Math function and return m^n in JavaScript
- Accessing variables in a constructor function using a prototype method with JavaScript?
- Adding a function for swapping cases to the prototype object of strings - JavaScript
- What is function prototype in C language
- Implementing partial sum over an array using JavaScript
- How does JavaScript .prototype work?
- What is the purpose of a function prototype in C/C++?
- Implementing Priority Sort in JavaScript
- Implementing Linear Search in JavaScript

Advertisements