Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

508
Views
How does the `indexOf` method on a string work in JavaScript?

The example below gets the last character of a string and calls indexOf on the character.

var sentence = "welcome to bootcamp prep";
var lastChar = sentence[sentence.length - 1];
console.log(lastChar); // "p"
console.log(sentence.indexOf(lastChar)); // 18

I understand that lastChar contains the last character in the string, and that indexOf returns a character that you are searching for.

Why does the call return 18? How does indexOf work?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

The indexOf function of a string does not return a character but rather returns the position of the first place that string or character is found, not the character itself.

In this question, the last character is "p", as found by this line:

var lastChar = sentence[sentence.length - 1];

However when looking at the string, you can see that the letter "p" is found more than once:

"welcome to bootcamp prep"
//                 ^ ^  ^
//                18 20 23

The numbers indicated in the comment (18, 20, and 23) represent the indexes (locations) of the character within the string.

What do these indexes mean? In JavaScript, indexes start at 0, so the first letter ("w") will be at index 0. In this string, the character "p" is found at places 19, 21, and 24, which translate to the indexes 18, 20, and 23.

The first instance of the searched string "p" is at index 18. Therefore, when running this line:

console.log(sentence.indexOf(lastChar)); // 18

The result will be 18, the index of the first instance of the last character.

(Side note: assuming you are a beginner when it comes to JavaScript - try not to use the var keyword whenever possible. If the variable needs to change, use let, or if the variable stays the same, use const.)

about 4 years ago · Juan Pablo Isaza Report

0

Because indexOf returns the first letter that is found matching the argument.

welcome to bootcam[p] <-- here prep is found.

JavaScript uses primitive data types so you string is just a string and holds no reference to the original value.

about 4 years ago · Juan Pablo Isaza Report

0

The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.

The indexOf() doesn't return the character, it return the position of the character which will be 18.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!