--- title: in 연산자 slug: Web/JavaScript/Reference/Operators/in tags: - JavaScript - Operator - Relational Operators - 관계형 연산자 - 연산자 - 자바스크립트 translation_of: Web/JavaScript/Reference/Operators/in ---
in 연산자는 명시된 속성이 명시된 객체에 존재하면 true를 반환합니다.
속성 in 객체명
속성객체명 다음 예제들은 in 연산자의 용도를 보여 줍니다.
// 배열
var trees = new Array("redwood", "bay", "cedar", "oak", "maple");
0 in trees // true를 반환합니다.
3 in trees // true를 반환합니다.
(1 + 2) in trees // true를 반환합니다. 연산자 우선 순위에 의하여 이 구문의 괄호는 없어도 됩니다.
6 in trees // false를 반환합니다.
"bay" in trees // false를 반환합니다. 당신은 배열의 내용이 아닌, 인덱스 값을 명시하여야 합니다.
"length" in trees // true를 반환합니다. length는 Array(배열) 객체의 속성입니다.
// 미리 정의된 객체
"PI" in Math // true를 반환합니다.
"P" + "I" in Math // true를 반환합니다.
// 사용자가 정의한 객체
var myCar = {company: "Lamborghini", model: "Lamborghini Veneno Roadster", year: 2014};
"company" in myCar // true를 반환합니다.
"model" in myCar // true를 반환합니다.
당신은 반드시 in 연산자의 오른쪽에 객체를 명시하여야 합니다. 예컨대 당신은 String 생성자로 만들어진 문자열을 명시할 수 있지만 문자열 리터럴은 명시할 수 없습니다.
var color1 = new String("green");
"length" in color1 // true를 반환합니다.
var color2 = "coral";
"length" in color2 // color2는 String 객체가 아니기에 오류를 냅니다.
in 연산자 사용하기 in 연산자는 delete 연산자로 제거된 속성에 대하여 false를 반환합니다.
var myCar = {company: "Lamborghini", model: "Lamborghini Veneno Roadster", year: 2014};
delete myCar.company;
"company" in myCar; // false를 반환합니다.
var trees = new Array("redwood", "bay", "cedar", "oak", "maple");
delete trees[3];
3 in trees; // false를 반환합니다.
만약 당신이 속성을 {{jsxref("Global_Objects/undefined", "undefined")}}로 설정하였는데 그것을 제거하지 않으면, in 연산자는 그 속성에 대하여 true를 반환합니다.
var myCar = {company: "Lamborghini", model: "Lamborghini Veneno Roadster", year: 2014};
myCar.company = undefined;
"company" in myCar; // true를 반환합니다.
var trees = new Array("redwood", "bay", "cedar", "oak", "maple");
trees[3] = undefined;
3 in trees; // true를 반환합니다.
in 연산자는 프로토타입 체인에 의하여 접근할 수 있는 속성에 대하여 true를 반환합니다.
"toString" in {}; // true를 반환합니다.
| 명세 | Status | Comment |
|---|---|---|
| {{SpecName('ESDraft', '#sec-relational-operators', 'Relational Operators')}} | {{Spec2('ESDraft')}} | |
| {{SpecName('ES6', '#sec-relational-operators', 'Relational Operators')}} | {{Spec2('ES6')}} | |
| {{SpecName('ES5.1', '#sec-11.8.7', 'The in Operator')}} | {{Spec2('ES5.1')}} | |
| {{SpecName('ES3', '#sec-11.8.7', 'The in Operator')}} | {{Spec2('ES3')}} | 초기의 정의가 담겨 있습니다. JavaScript 1.4에 추가되었습니다. |
{{CompatibilityTable}}
| 기능 | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| 지원 | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |
| 기능 | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| 지원 | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |
delete