메소드 : 명령어. immutable과 mutable 두 종류가 있다.
immutable : 원본이 변하지 않는 메소드.
mutable : 원본이 변하는 메소드.
※ 모든 sting method는 immutable method이지만 array method는 immutable과 mutable이 섞여있다.
문자열의 특징
문자열 메소드(immutable method)
1) str.indexOf(찾을요소searchValue)
arguments : 찾고자 하는 문자열
return value : 처음으로 일치하는 index. 없으면 -1을 반환
lastIndexOf(searchValue) : 문자열의 뒤에서 부터 찾는다.
str.includes(searchValue) : true or false로 리턴 값의 유무를 알 수 있다.
2) str.split(기준seperator) <=> str.join(기준seperator)
arguments : 분리기준이 될 문자열
return value : 분리된 문자열이 포함된 배열
csv형식을 처리할 때 유효하다. 컴마,나 줄바꿈연산자 \n으로도 나눌 수 있다.
ex)
let str = 'Hello from the other world'
str.split(' '); // ▶ (5) ["Hello", "from", "the", "other", "world"]
*str; // 'Hello from the other world'
3) str.join('') <=> str.split(기준seperator)
arguments : 문자 사이에 넣을 값. 없을 경우 컴마,로 묶인다
return value : 묶인 하나의 문자열
ex)
let str = ["Hello", "from", "the", "other", "world"]
str.join(''); // "Hellofromtheotherworld"
str.join(' '); // "Hello from the other world"
str.join(); // "Hello,from,the,other,world"
4) str.substring(start, end) : 문자열의 부분 문자열을 알려준다
arguments : 시작index, 끝index
return value : 시작과 끝 사이의 끝을 제외한 문자열
ex)
let str = 'abcde'
str.substring(0, 3); // 'abc'
str.substring(3, 0); // 'abc' -> strat와 end는 위치를 바꿔도 상관없다.
str.substring(2); // 'cde' -> 끝index를 지정하지 않으면 시작점부터 끝까지 알려준다.
str.substring(1, 1); // '' -> 시작index === 끝index일 경우 빈 string 알려준다.
str.substring(-1, 10); // 'abcde' -> 시작index < 0 일경우 0으로 취급, 끝index가 문자열의 길이보다 길 경우 문자열의 길이(=최대값)으로 취급
str.substring(NaN, 1); // 'a' -> NaN은 0으로 취급
*str.substring(start, end)와 str.slice(start, end)는 유사하나 몇가지 차이점이 있다.
5) str.slice(start, end) : 문자열의 일부를 추출하면서 새로운 문자열을 반환
arguments : 시작index, 끝index
return value : 시작과 끝 사이의 끝을 제외한 문자열
ex)
let str = 'abcde'
str.slice(0, 3); // 'abc'
str.slice(3, 0); // '' -> strat > end일 경우 빈 문자열이 나온다.
str.slice(2); // 'cde' -> 끝index를 지정하지 않으면 시작점부터 끝까지 알려준다.
str.slice(1, 1); // '' -> 시작index === 끝index일 경우 빈 string 알려준다.
str.slice(-1, 10); // 'e' -> 시작index < 0 일경우 strLength(문자열 길이) + 시작index(음수)으로 취급, 끝index가 문자열의 길이보다 길 경우 문자열의 길이(=최대값)으로 취급. 한마디로 뒤에서부터 1을 시작으로 센다는 뜻.
str.slice(-10); // 'abcde'
str.slice(NaN, 1); // 'a' -> NaN은 0으로 취급
6) str.toLowerCase() / str.toUpperCase()
arguments : 없음
return value : 대/소문자로 변환된 문자열
ex)
let str = 'apple'
str.toUpperCase(); // 'APPLE'
*str; // 'apple'
1) Array.isArray(arr) : 변수가 배열인지 아닌지 판별하는 메소드. true or false
*typeof()는 변수의 타입을 알려주는 메소드.
typeof('word')// 'string'
typeof(123) // 'number
typeof(arr[]) // 'object'
typeof(obj{}) // 'object'
typeof()에서는 배열과 객체 둘다 object로 나오기때문에 배열을 알기위해서는 Array.isArray를 써야한다.
2) arr.indexOf(element) : 배열에 element의 존재여부 확인. 대소문자 구분.
ex)
let words = ['a', 'b', 'c', 'b']
words.indexOf('b') // 1 -> element가 있는 첫번째 인덱스를 반환. 없다면 -1 반환
따라서 word.indexOf(element) !== -1 // true 일 때, element는 arr에 있다는 뜻
3) arr.includes(element) : 배열에 element의 존재여부 확인. 대소문자 구분.
ex)
let words = ['a', 'b', 'c', 'b']
words.includes('b') // true -> element가 있으면 true반환. 없다면 -1 반환
즉, word.indexOf(element) !== -1 와 words.includes(element) 는 같은 역할을 한다.
.indexOf()는 인덱스 정보까지 알 수 있고 브라우저 호환성면에서 .includes()보다 상위이기 때문에 가급적이면 .indexOf()를 사용한다.
4) push, pop, shift, unshift : 배열에 요소 넣고 빼기
array.push() : 배열의 끝에 요소를 추가. 실행 후의 배열의 길이 반환.
array.pop() : 배열의 끝 요소 삭제. 삭제되는 요소 반환.
array.shift() : 배열의 첫번째 요소 삭세. 삭제되는 요소 반환.
array.unshift() : 배열의 첫번째에 요소 추가. 실행 후의 배열의 길이 반환.
console.table(array) // 배열을 표로 볼 수 있다.