-
Array.prototype.indexOf() vs String.prototype.indexOf()concept/javascript 2020. 3. 31. 16:38
Array.prototype.indexOf()
const beasts = ['ant', 'bison', 'camel', 'duck', 'bison']; console.log(beasts.indexOf('bison')); // expected output: 1 // start from index 2 console.log(beasts.indexOf('bison', 2)); // ** index 2에서부터 찾아라! // expected output: 4 console.log(beasts.indexOf('giraffe')); // expected output: -1
String.prototype.indexOf()
const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?'; const searchTerm = 'dog'; const indexOfFirst = paragraph.indexOf(searchTerm); console.log('The index of the first "' + searchTerm + '" from the beginning is ' + indexOfFirst); // expected output: "The index of the first "dog" from the beginning is 40" console.log('The index of the 2nd "' + searchTerm + '" is ' + paragraph.indexOf(searchTerm, (indexOfFirst + 1))); // expected output: "The index of the 2nd "dog" is 52"
'concept > javascript' 카테고리의 다른 글
for 문 / while 문의 차이 (0) 2020.03.31 .bind 를 이용한 템플릿 만들기 (0) 2020.03.31 재귀 함수(recursion) (0) 2020.03.30 함수에서 헷갈릴 수 있는 개념 (0) 2020.03.29 숫자 활용 메소드(Number/Math method) (0) 2020.03.29