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?