먼저 in 연산자는 객체안에 특정 프로퍼티가 존재하는지 여부를 확인하는 연산자 이다. 특정 프로퍼티가 존재한다면 true를 반환하고 존재하지 않으면 false를 반환한다.
const person = {
name : "hi",
age : 26
}
console.log('name' in person);
//TRUE
console.log('age' in person);
//TRUE
console.log('adress' in person);
//FALSE
이렇게 간단하게 in연산자를 이용하면 객체안에 어떤키가 존재하는지 여부를 판단할 수 있다.
for in 문을 이용하면 객체의 모든 프로퍼티를 다 돌면서 키와 값을 꺼내올 수 있다.
const person = {
name: 'Kim',
adress: 'Seoul',
age: 27
}
for (const key in person) {
console.log(key + ': ' + person[key]);
}
//name: Kim
adress: Seoul
age: 27