
// 기존 함수 표현식
const add = function(x, y) {
return x + y;
};
// 화살표 함수
const add = (x, y) => {
return x + y;
};
// 화살표 함수 축약
const add = (x, y) => x + y ;
// -----------------------------------
// 기존 함수 표현식
const add10 = function(x) {
return x + 10;
};
// 화살표 함수
const add10 = (x) => {
return x + 10;
};
// 화살표 함수 축약
const add10 = x => x + 10;
const player = ['손흥민', '이강인', '김민재'];
const [one, two, three] = player;
console.log(one, two, three); // 손흥민, 이강인, 김민재
객체 구조 분해
const so = { userName: '소혜림', userAge: 34 };
const {userName, userAge: age} = so;
console.log(userName, age); // 소혜림 34
function fn(a, b, ...args) {
console.log(a, b, args);
}
fn(); // undefined undefined []
fn(1); // 1 undefined []
fn(2, 3); / 2 3 []
fn(7, 8, 9, 10, 11); // 7, 8, [9, 10, 11]
const state = ['orange', 'yellow', 'green'];
const newState = [...state]; // ['ornage', 'yellow', 'green']
const state2 = {name: '하츄핑', age: 10};
const newState2 = {...state2}; // {name: '하츄핑', age: 10}
function sum(x, y) {
return x+ y;
}
const numbers = [1, 2];
console.log(sum(...numbers)); //3
조건 ? 참일 때의 값 : 거짓일 때의 값;const num = 10;
const result = num & 2 === 0 ? '짝수' : '홀수';
console.log(result); // '짝수'
1) push(...items): number
2) pop(): string|undefined
const fruits = ['사과', '바나나'];
const newLength = fruits.push('오렌지');
console.log(newLength, fruits); // 3 ['사과', '바나나', '오렌지']
fruits.push('딸기', '포도');
console.log(fruits); // ['사과', '바나나', '오렌지', '딸기', '포도']
let lastFruit = fruits.pop();
console.log(lastFruit, fruits); 포도 ['사과', '바나나', '오렌지', '딸기']
lastFruit = fruits.pop();
console.log(lastFruit, fruits); 딸기 ['사과', '바나나', '오렌지']
3) unshift(...items): number
4) shift():string|undefined
const fruits = ['사과', '바나나'];
const newLength = fruits.unshift('딸기');
console.log(newLength, fruits); // 3 ['딸기', '사과', '바나나']
fruits.unshift('딸기', '포도');
console.log(fruits); // ['딸기', '포도', '딸기', '사과', '바나나']
let firstFruit = fruits.shift();
console.log(firstFruit, fruits); // 딸기 ['포도', '딸기', '사과', '바나나']
firstFruit = fruits.shift();
consolo.log(firstFruit, fruits); // 포도 ['딸기', '사과', '바나나']
5) splice(start:number, deleteCount?:number, ...items):any[]
const arr1 = ['신라면', '열라면', '비빔면', '오징어짬뽕', '육개장', '왕뚜껑', '삼양라면'];
let arr2 = arr1.splice(1, 2); // 인덱스 1부터 2개 추출
console.log(arr1, arr2); // ['신라면', '오징어짬뽕', '육개장', '왕뚜껑', '삼양라면'] ['열라면', '비빔면']
arr2 = arr1.splice(2, 2); // 인덱스 2부터 2개 추출
console.log(arr1, arr2); // ['신라면', '오징어짬뽕', '삼양라면'] ['육개장', '왕뚜껑']
arr2= arr1.splice(2); // 인덱스 2부터 끝까지 추출
console.log(arr1, arr2); // ['신라면', '오징어짬뽕'] ['삼양라면']
arr2 = arr1.splice(1, 1, '진라면', '불닭볶음면'); // 인덱스 1부터 1개 추출하고 진라면, 불닭볶음면 추가
console.log(arr1, arr2); // ['신라면', '진라면', '불닭볶음면'] ['오징어짬뽕']
6) slice(start?:number, end?:number):any[]
const arr1 = ['토끼', '사자', '호랑이', '강아지', '고양이', '기니피그', '돼지'];
let arr2 = arr1.slice(1, 3); // 인덱스 1부터 3 앞까지 복사
console.log(arr2); // ['사자', '호랑이']
arr2 = arr1.slice(2, 2); // 인덱스 2부터 2 앞까지 복사
console.log(arr2); // []
arr2 = arr1.slice(5); // 인덱스 5부터 끝까지 복사
console.log(arr2); // ['기니피그', '돼지']
arr2 = arr.slice(-2); //인덱스 -2부터 끝가지 복사
console.log(arr2); // ['기니피그', '돼지']
console.log(arr1); // ['토끼', '사자', '호랑이', '강아지', '고양이', '기니피그', '돼지']
7) concat(...items): any[]
const arr = ['오렌지', '딸기', '레몬'];
const arr2 = arr.concat(['사과', '바나나'], ['포도']);
console.log(arr2.includes('사과')); // true
console.log(arr); // [ '오렌지', '딸기', '레몬' ]
console.log(arr2); // [ '오렌지', '딸기', '레몬', '사과', '바나나', '포도' ]
1) indexOf(searchElement, fromIndex?:number):number
fromIndex: 지정한 인덱스부터 탐색을 시작(기본값 0)fromIndex: 지정한 인덱스부터 탐색을 시작 (기본값 0)fromIndex: 지정한 인덱스부터 탐색을 시작 (기본값 0)const arr = [1, 3, 5, 8, 9, 3, 4, 5];
console.log('첫 번째 3의 위치', arr.indexOf(3)); // 첫 번째 3의 위치 1
console.log('마지막 3의 위치', arr.lastIndexOf(3)); // 마지막 3의 위치 5
const arr2 = ['오렌지', '딸기', '레몬'];
console.log(arr2.includes('레몬')); //true
console.log(arr2.includes('사과')); // false
1) forEach(callbackFn(currentValue, index, array), thisArg?): void
const arr = [10, 20, 30];
let newArr: number[] = [];
// 배열의 각 요소를 순회하며 실행
arr.forEach((elem, i) => {
newArr.push(elem **2);
});
console.log('forEach', newArr); // [100, 400, 900]
// 배열의 각 요소를 순회하며 반환받은 값으로 새로운 배열 생성
const newArr2 = arr.map(function(elem, i) {
// 요소의 제곱값 반환
return elem **2;
});
console.log('map', newArr2); // [100, 400, 900]
3) find(callbackFn, thisArg?):any|undefined
const arr = [1, 3, 5, 8, 9, 3, 4, 5];
console.log(arr.find(num => num % 2 === 0)); // 8
console.log(arr.findIndex(num => num % 2 === 0)); // 3
console.log(arr.filter(n => n % 2 === 0)); // [8, 4]
6) some(callbackFn, thisArg?):boolean
const arr = [1, 2, 3];
const hasEven = arr.some(n => n % 2 === 0); // true
const isAllEven = arr.every(n => n % 2 === 0); // false
console.log(hasEven, isAllEven); // true false
8) reduce(callbackFn(accumulator, currentValue, index, array), initialValue?):any
const arr = [1, 2, 3, 4];
const initialValue = 0;
const sum = arr.reduce(function(accumulator, currentValue){
return accumulator + currentValue
}, initialValue);
console.log(sum); // 0 + 1 + 2 + 3 + 4 = 10