본문 바로가기

FrontEnd/Javascript

Js 배열


//배열은 index를 나타내는 문자열을 property로 갖는다.
//js배열에서 key=index다(=js에서 프라퍼티의 key는 index로 쓰일 수 있다)

const arr=[7,8,9];
arr[3]="s"
arr.x=4;
console.dir(Object.getOwnPropertyDescriptors(arr));
console.log(arr);
console.log(arr['2']);//배열은 index를 나타내는 문자열을 프라퍼티로 갖는다.
console.log(arr[2]);

console.log(arr['s']);//undefined;

// console.log(arr[x]);//Error x is not defined;

console.log(arr['x']);//Js에서는 이처럼 배열의 문자열인 프라퍼티의 key는 index로 쓰일 수 있다.


 

const arr = [1, 2, 3];
//배열요소 선언
arr[3] = 4;
arr['4'] = 5; //arr[4] = 5; 와 같음

//배열 프로퍼티 선언
arr['5th'] = 6;
arr.6th = 7;

 //에러
arr.7 = 8;