string.includes
includes()
๋ฉ์๋๋ ํ๋์ ๋ฌธ์์ด์ด ๋ค๋ฅธ ๋ฌธ์์ด์ ํฌํจ๋์ด ์๋์ง์ ํ๋ณํ๊ณ ์ด๋ฅผ true
ํน์ false
๋ก ๋ฐํํ๋ค.
const sentence = '๊ณ ์์ด๊ฐ ์งฑ์ด๋ท!';
const word = '๊ณ ์์ด';
`์ด ๋ฌธ์ฅ์ ${word}๊ฐ ํฌํจ๋์ด ${sentence.includes(word) ? '์๋ค' : '์์ง ์๋ค'}`; // "์ด ๋ฌธ์ฅ์ ๊ณ ์์ด๊ฐ ํฌํจ๋์ด ์๋ค"
Syntax
str.includes(searchString[, position])
Parameters
searchString
str
์์ ์ฐพ์ ๋ฌธ์์ด
position (Optional)
๋ฌธ์์ด ์์ ์ฐพ๊ธฐ ์์ํ ์์น (๊ธฐ๋ณธ๊ฐ์ 0์ด๋ค)
Return value
์ฃผ์ด์ง ๋ฌธ์์ด ๋ด์์ ๊ฒ์ ๋ฌธ์๊ฐ ์กด์ฌํ๋ฉด true
๊ทธ๋ ์ง ์๋ค๋ฉด false
Description
Case-sensitivity
๋์๋ฌธ์๋ฅผ ๊ตฌ๋ถํ๋ค.
'Cats are the best!'.includes('cats'); // false
Examples
const sentence = '๊ณ ์์ด๊ฐ ์งฑ์ด๋ค!';
sentence.includes('๊ณ ์์ด'); // true
sentence.includes('๊ณ ์์ด', 1); // false
sentence.includes('๊ฐ์์ง'); // false
sentence.includes(''); // true
Polyfill
IE๋ ์ง์๋์ง ์๋๋ค.
ECMAScript 2015 ์คํ์ ์ถ๊ฐ๋จ
if (!String.prototype.includes) {
String.prototype.includes = function(search, start) {
'use strict';
if (search instanceof RegExp) { // ์ค? instanceof RegExp๋ก?
throw TypeError('first argument must not be a RegExp');
}
if (start === undefined) start = 0;
return this.indexOf(search, start) !== -1;
}
}
Last updated
Was this helpful?