-
For of , For in 반복문JS 2023. 4. 13. 17:52
For of 반복문은 배열(Array)에서 많이 씀
const fruits = ['Apple', 'Banana', 'Cherry'] // For 사용 for(let i = 0; i < fruits.length; i += 1) { console.log(fruits[i]) } // For of 사용 for(const a of fruits) { // a는 내맘대로 이름짓기 console.log(a) }
이렇게 씀. 같은 결과가 나오는데, 코드는 좀 더 간결한듯
For in은 객체(Object)에서 많이 씀
const user = { name: 'Harim', age: 20, isValid: true, email: 'abc@gmail.com' } for (const key in user){ // key도 이름 마음대로 바꿀수이쑴 console.log(key) console.log(user[key]) }
결과
'JS' 카테고리의 다른 글
쿠키(Cookie) & 스토리지(Storage) (0) 2023.04.26 JSON (0) 2023.04.24 전개연산자(Spread Operator) (0) 2023.04.12 Nullish 병합 (??) (0) 2023.04.12 참과 거짓(If문 통과 못하는것?) (0) 2023.04.12