Skip to content

Latest commit

 

History

History
34 lines (28 loc) · 1.06 KB

File metadata and controls

34 lines (28 loc) · 1.06 KB

인사이드 자바스크립트 P. 57 ~ 60

8. 배열과 객체

자바스크립트에서 배열 역시 객체이다. 하지만 일반 객체와는 차이가 있다.

//colorsArray 배열
var colorsArray = ['orange', 'yellow' ,'green'];
console.log(colorsArray[0]); // 출력값 orange
console.log(colorsArray[1]); // 출력값 yellow
console.log(colorsArray[2]); // 출력값 green

//colorsObj 객체
var colorsObj = {
    '0' = 'orange',
    '1' = 'yellow',
    '2' = 'green'
};
console.log(colorsObj[0]); // 출력값 orange
console.log(colorsObj[1]); // 출력값 yellow
console.log(colorsObj[2]); // 출력값 green

//typeof 연산자 비교
console.log(typeof colorsArry); // 출력값 object (not Array)
console.log(typeof colorsArry); // 출력값 object

//length 프로퍼티
console.log(colorsArray.length); // 출력값 3
console.log(colorsObj.length); // 출력값 undefined

//배열 표준 메서드
colorsArray.push('red'); // ['orange', 'yellow', 'green', 'red']
colorsObj.push('red'); // uncaught TypeError : object #<object> has no method 'push'