includes
- includes 란?
문자열이나 배열이 특정값 또는 요소를 포함하고 있는지를 확인하는데 사용된다.
이 메서드는 주로 두가지 용도로 사용된다.
1.문자열에서 특정 문자열 찾기
2.배열에서 특정 요소 찾기
- 문자열 에서의 includes
기본문법
str.includes(searchStirng)[,Position]
- searchString
찾고자 하는 문자열 - position(선택적)
검색을 시작할 위치(기본값 0 ) - 반환값
주어진 문자열이 포함되어 있으면 true 포함되지 않았다면 false를 반환한다. - 대소문자를 구분한다
Hi => Hi=true ,hi =false
예시
const str = "Hello, world!";
console.log(str.includes("world")); // true
console.log(str.includes("hello")); // false (대소문자 구분)
console.log(str.includes("o", 5)); // true (5번째 인덱스 이후에서 검색)
- 배열 에서의 includes
기본문법
arr.includes(value[,fromIndex])
- value
찾고자 하는 요소 - fromIndex
검색을 시작할 인덱스 (기본값 0 ) - 반환값
배열에 주어진 요소에 포함되어 있으면 true 포함되지 않았다면 false를 반환한다.
예시
const arr = [1, 2, 3, 4, 5];
console.log(arr.includes(3)); // true
console.log(arr.includes(6)); // false
console.log(arr.includes(2, 2)); // false (2번째 인덱스 이후에서 검색)
최댓값 최솟값
- JS에서 최댓값 최솟값을 구하는 메소드
- Math.max()
인자로 전달된 값들 중에서 최댓값을 반환한다. - Math.min()
인자로 전달된 값들 중에서 최솟값을 반환한다. - 공통사항
배열을 직접 사용할수는 없으므로 스프레드 연산자(...) 을 사용하여
배열을 인자로 전달해야 한다.
예시
const nums = [5,3,8,1,2]
//최댓값 구하기
const max = Math.max(...nums)
console.log("최댓값:",max) // 최댓값:8
//최솟값 구하기
const min = Math.min(...nums)
console.log("최솟값:",min) // 최솟값:1
'JavaScript' 카테고리의 다른 글
변수 선언과 데이터 할당 /가변값과 가변성 (0) | 2024.08.13 |
---|---|
JS 메서드 toUpperCase/toLowerCase 와 replace /메모리와 데이터 (0) | 2024.08.12 |
css 자주 쓰는 명령어 (0) | 2024.07.31 |
slice 메서드 (0) | 2024.07.30 |
HTML 구조 (0) | 2024.07.29 |