Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

javascript
communitycreator

What is the String.lastIndexOf method in JavaScript?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The lastIndexOf method returns the last index of a string that contains the search string that was entered.

let sourceStr = 'JavaScript JavaScript',
searchStr = 'JavaScript',
lastIndex = sourceStr.lastIndexOf(searchStr);
console.log(`The last index of ${searchStr} in ${sourceStr} is ${lastIndex}` );

If the search string is not found in the source string, -1 is returned.

let sourceStr = 'JavaScript JavaScript',
searchStr = 'Python',
lastIndex = sourceStr.lastIndexOf(searchStr);
console.log(`The last index of "${searchStr}" in "${sourceStr}" is ${lastIndex}` );

The lastIndexOf method takes two arguments:

  • search string
  • fromIndex: if we pass the fromIndex, lastIndexOf() will start searching from the fromIndex of the source string first, and then continue searching the source string backwards.
console.log("Using from Index");
let sourceStr = '0101';
let searchStr, lastIndex, searchFromIndex;
console.log("-----------")
searchStr = "0";
lastIndex = sourceStr.lastIndexOf(searchStr);
console.log(`The last index of "${searchStr}" in "${sourceStr}" is ${lastIndex}`);
console.log("\n-----------")
searchStr = "0";
searchFromIndex = 2;
lastIndex = sourceStr.lastIndexOf(searchStr, searchFromIndex);
console.log(`Searching The last index of "${searchStr}" from index ${searchFromIndex} in "${sourceStr}" is ${lastIndex}`);
console.log("\n-----------")
searchStr = "1";
searchFromIndex = 2;
lastIndex = sourceStr.lastIndexOf(searchStr, searchFromIndex);
console.log(`Searching The last index of "${searchStr}" from index ${searchFromIndex} in "${sourceStr}" is ${lastIndex}`);
console.log("\n-----------")
searchStr = "01";
searchFromIndex = 2;
lastIndex = sourceStr.lastIndexOf(searchStr, searchFromIndex);
console.log(`Searching The last index of "${searchStr}" from index ${searchFromIndex} in "${sourceStr}" is ${lastIndex}`);
console.log("\n")
console.log(`"0101".lastIndexOf("01") returns 2 not 0`);
console.log(`Because the search starts from 2nd index`);
console.log(`From 2nd index, 01 can be found. So the index 2 is returned`);
console.log(`If the 01 is not found from the 2nd index, then the search will happen backward from 1st index, then 0th index`);

lastIndexOfis case-sensitive.

let sourceStr = "JavaScript",
searchStr = "script",
lastIndex = sourceStr.lastIndexOf(searchStr);
console.log(`The last index of ${searchStr} in ${sourceStr} is ${lastIndex}` );
console.log(`we will get -1 because of case sensitivity` );

RELATED TAGS

javascript
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring