| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf | [Back] [Original] |
Get to know MDN better
This page was translated from English by the community. Learn more and join the MDN Web Docs community.
This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 7.
indexOf() String . -1 .
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"
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 .
"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 .
"Blue Whale".indexOf("blue"); // returns -1
'0' true , -1 false . , .
"Blue Whale".indexOf("Blue") !== -1; // true
"Blue Whale".indexOf("Bloe") !== -1; // false
indexOf() "Brave new world" indexOf() lastIndexOf() .
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" .
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 :
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 |
String.prototype.charAt()String.prototype.lastIndexOf()String.prototype.split()Array.prototype.indexOf()This page was last modified on 2025 10 9 by MDN contributors.
StringString.prototype.anchor()String.prototype.at()String.prototype.big()String.prototype.blink()String.prototype.bold()String.prototype.charAt()String.prototype.charCodeAt()String.prototype.codePointAt()String.prototype.concat()String.prototype.endsWith()String.prototype.fixed()String.prototype.fontcolor()String.prototype.fontsize()String.prototype.includes()String.prototype.indexOf()String.prototype.isWellFormed()String.prototype.italics()String.prototype.lastIndexOf()String.prototype.link()String.prototype.localeCompare()String.prototype.match()matchAll()String.prototype.normalize()String.prototype.padEnd()String.prototype.padStart()String.prototype.repeat()String.prototype.replace()String.prototype.replaceAll()String.prototype.search()String.prototype.slice()String.prototype.small()String.prototype.split()String.prototype.startsWith()String.prototype.strike()String.prototype.sub()String.prototype.substr()String.prototype.substring()String.prototype.sup()String.prototype.toLocaleLowerCase()String.prototype.toLocaleUpperCase()String.prototype.toLowerCase()String.prototype.toString()String.prototype.toUpperCase()String.prototype.toWellFormed()String.prototype.trim()String.prototype.trimEnd()String.prototype.trimStart()String.prototype.valueOf()String.prototype[@@iterator]()Object/FunctionObject.prototype.__defineGetter__()Object.prototype.__defineSetter__()Object.prototype.__lookupGetter__()Object.prototype.__lookupSetter__()Object.prototype.hasOwnProperty()Object.prototype.isPrototypeOf()Object.prototype.propertyIsEnumerable()Object.prototype.toLocaleString()Object.prototype.toString()Object.prototype.valueOf()Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |