Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

502
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda