[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf [Back]  [Original]

String.prototype.indexOf() - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

String.prototype.indexOf()

Baseline
Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 7.

indexOf() String . -1 .

In this article

const paragraph = "I think Ruth's dog is cuter than your dog!";

const searchTerm = "dog";
const indexOfFirst = paragraph.indexOf(searchTerm);

console.log(`The index of the first "${searchTerm}" is ${indexOfFirst}`);
// Expected output: "The index of the first "dog" is 15"

console.log(
  `The index of the second "${searchTerm}" is ${paragraph.indexOf(
    searchTerm,
    indexOfFirst + 1,
  )}`,
);
// Expected output: "The index of the second "dog" is 38"

js
str.indexOf(searchValue[, fromIndex])

searchValue

. "undefined" .

fromIndex Optional

. . 0, . fromIndex . fromIndex >= str.length , -1 . searchValue , str.length .

searchValue . -1.

. 0 (index), stringName stringName.length -1 .

js
"Blue Whale".indexOf("Blue"); // returns  0
"Blue Whale".indexOf("Blute"); // returns -1
"Blue Whale".indexOf("Whale", 0); // returns  5
"Blue Whale".indexOf("Whale", 5); // returns  5
"Blue Whale".indexOf("Whale", 7); // returns -1
"Blue Whale".indexOf(""); // returns  0
"Blue Whale".indexOf("", 9); // returns  9
"Blue Whale".indexOf("", 10); // returns 10
"Blue Whale".indexOf("", 11); // returns 10

indexOf() . , -1 .

js
"Blue Whale".indexOf("blue"); // returns -1

'0' true , -1 false . , .

js
"Blue Whale".indexOf("Blue") !== -1; // true
"Blue Whale".indexOf("Bloe") !== -1; // false

indexOf()

"Brave new world" indexOf() lastIndexOf() .

js
var anyString = "Brave new world";

console.log(
  "The index of the first w from the beginning is " + anyString.indexOf("w"),
);
//  w   8
console.log(
  "The index of the first w from the end is " + anyString.lastIndexOf("w"),
);
//  w   10

console.log(
  'The index of "new" from the beginning is ' + anyString.indexOf("new"),
);
//  new   6
console.log(
  'The index of "new" from the end is ' + anyString.lastIndexOf("new"),
);
//  new   6

indexOf()

2 . . console.log() 19. , console.log() -1. , indexOf() myCapString "cheddar" .

js
var myString = "brie, pepper jack, cheddar";
var myCapString = "Brie, Pepper Jack, Cheddar";

console.log('myString.indexOf("cheddar") is ' + myString.indexOf("cheddar"));
//  19 .
console.log(
  'myCapString.indexOf("cheddar") is ' + myCapString.indexOf("cheddar"),
);
//  -1 .

indexOf()

str e :

js
var str = "To be, or not to be, that is the question.";
var count = 0;
var pos = str.indexOf("e"); //pos 4  .

while (pos !== -1) {
  count++;
  pos = str.indexOf("e", pos + 1); //   e   e .
}

console.log(count); //  4 .

Specification
ECMAScript 2027 LanguageSpecification
# sec-string.prototype.indexof


Web Proxy Viewer  |  New URL  |  Original Page