๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
JAVASCRIPT

[JAVASCRIPT] ๋ฌธ์ž์—ด ๋ฉ”์„œ๋“œ : includes( )

by _ํ† ๋งคํ†  2022. 8. 18.
728x90

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ : ๋ฌธ์ž์—ด ๋ฉ”์„œ๋“œ : includes()

์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” ๋ฌธ์ž์—ด ๋ฉ”์„œ๋“œ์ธ includes()์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค! ๐Ÿ˜


#1. includes( )

includes() ๋ฉ”์„œ๋“œ๋Š” ๋ฌธ์ž์—ด ํฌํ•จ ์—ฌ๋ถ€๋ฅผ ๊ฒ€์ƒ‰ํ•˜์—ฌ, ๋ถˆ๋ฆฐ(true, false)์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰ ๋ฌธ์ž์—ด์ด ํŠน์ • ๋ฌธ์ž์—ด์„ ํฌํ•จํ•˜๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ผ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!

! includes( ) ์‚ฌ์šฉ ๋ฐฉ๋ฒ• !
string.includes( searchString, length )
"๋ฌธ์ž์—ด".includes(๊ฒ€์ƒ‰ํ•  ๋ฌธ์ž์—ด);
"๋ฌธ์ž์—ด".includes(๊ฒ€์ƒ‰ํ•  ๋ฌธ์ž์—ด, ๊ฒ€์ƒ‰์„ ์‹œ์ž‘ํ•  ์œ„์น˜); == indexOF()์™€ ๊ฑฐ์˜ ๊ฐ™๋‹ค.

const str1 = "javascript reference";

const currentStr1 = str1.includes("javascript");    // true
const currentStr2= str1.includes("j");              // true
const currentStr3= str1.includes("b");              // false : b๋Š” ํฌํ•จ๋˜์–ด ์žˆ์ง€ ์•Š์œผ๋ฏ€๋กœ false
const currentStr4= str1.includes("reference");      // true
const currentStr5 = str1.includes("reference", 1);    // true
const currentStr6 = str1.includes("reference", 11);   // true
const currentStr7 = str1.includes("reference", 12);   // false : ๋ฌธ์ž์—ด ์ž์ฒด์— ์œ„์น˜๊ฐ’์ธ 12๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— false

! point !
'abzcd'.includes( 'z' )
includes() ๋ฉ”์„œ๋“œ๊ฐ€ ๋ฌธ์ž์—ด์ธ 'abzcd'์—์„œ z๋ฅผ ํฌํ•จํ•˜๋Š”์ง€ ๊ฒ€์‚ฌํ•œ๋‹ค. ์œ„ ์‹์€ z๋ฅผ ํฌํ•จํ•˜๋ฏ€๋กœ true๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค!
'abZcd'.includes( 'z', 3 )
includes() ๋ฉ”์„œ๋“œ๊ฐ€ ๋ฌธ์ž์—ด์ธ 'abZcd'์˜ 3๋ฒˆ์งธ ์œ„์น˜๋ถ€ํ„ฐ z๋ฅผ ํฌํ•จํ•˜๋Š”์ง€ ๊ฒ€์‚ฌํ•œ๋‹ค. includes()๋Š” ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜๊ธฐ ๋•Œ๋ฌธ์— z๋ฅผ ํฌํ•จํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ false๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค!

728x90

๋Œ“๊ธ€


Lucky Charms Rainbow
js
html
css