///////////////////////////////////////
// Basic Array Operations (Methods)
const friends = ['Michael', 'Steven', 'Peter'];
// Add elements
const newLength = friends.push('Jay');
console.log(friends);
console.log(newLength);
friends.unshift('John');
console.log(friends);
// Remove elements
friends.pop(); // Last
const popped = friends.pop();
console.log(popped);
console.log(friends);
friends.shift(); // First
console.log(friends);
console.log(friends.indexOf('Steven'));
console.log(friends.indexOf('Bob'));
friends.push(23);
console.log(friends.includes('Steven'));
console.log(friends.includes('Bob'));
console.log(friends.includes(23));
if (friends.includes('Steven')) {
console.log('You have a friend called Steven');
}
includes() - array안에 특정 값이 있는지 없는지 확인
pop - 맨 뒤에 있는 것 삭제
push - 맨 뒤에서 추가
'공부자료 > 자바스크립트' 카테고리의 다른 글
섹션 2 Javascript Fundamentals part 2 - Looping Arrays, Breaking and Continuing (0) | 2021.09.20 |
---|---|
섹션 2 Javascript Fundamentals part 2 - Iteration: The for Loop (0) | 2021.09.20 |
섹션 2 Javascript Fundamentals part 2 - Object Methods (0) | 2021.09.20 |
섹션 2 Javascript Fundamentals part 2 - Dot vs Bracket Notation (0) | 2021.09.20 |
섹션 2 Javascript Fundamentals part 2 - Introduction to Objects (0) | 2021.09.20 |