프론트엔드/javascript

[javascript] 자바스크립트 배열 (Array) 정리

Se Yeon 2022. 6. 7. 12:07

배열 선언하는 방법

cosnt number = ["one","two", "three"];

배열 출력하기

console.log(number);  // ["one", "two", "three"]

배열에 요소 추가하기

number.push("four");

 

push로 요소를 추가하고 나서 다시 출력하면  four까지 추가된걸 확인할 수 있다. 

console.log(number);  // ["one", "two", "three","four"]

원하는 요소만 출력하고싶을때는 대괄호안에 순서를 넣은 후 출력하면 된다 

console.log(number[0]); // "one"출력
console.log(number[3]); // "four" 출력

 

 

이러한 방식으로 요소를 수정할 수도 있다

number[0] ="원";
console.log(number); // ["원", "two", "three" ,"four"] 출력

 

이처럼 배열을 사용할 수 있고 배열은 설명이 필요하지 않은 목록의 값들을 정리할 때 주로 사용한다 !