Bin's Blog

includes 메서드 본문

JavaScript

includes 메서드

hotIce 2023. 5. 26. 09:09
728x90

includes 메서드는 주로 배열이나 문자열에서 특정 항목이나 문자가 포함되어 있는지 확인하는데 사용됩니다. 쉽게 말해 초코릿 쿠키를 만들고 있는데, 우유가 재료에 포함되어 있는지 확인하고 싶다? 그러면 사용하면 된다.

 

1. 배열에서 사용할 경우

let fruits = ['apple', 'banana', 'mango', 'grape'];

// true
console.log(fruits.includes('mango'));
// false
console.log(fruits.includes('pineapple'));

배열에서 위처럼 우리가 찾는 요소가 포함되어 있으면 true, 아니면 false를 출력한다. 

 

2. 문자열에서 사용할 경우 

let sentence = 'The quick brown fox jumps over the lazy dog';

// true
console.log(sentence.includes('fox'));
// false
console.log(sentence.includes('cat'));

문자열에서 위처럼 우리가 찾는 요소가 포함되어 있으면 true, 아니면 false를 출력한다. 

728x90

'JavaScript' 카테고리의 다른 글

every 메서드  (0) 2023.05.30
filter 메서드  (0) 2023.05.27
some 메서드  (0) 2023.05.25
call, apply, bind 메서드  (0) 2023.05.24
Symbol  (0) 2023.05.19