let arr = new Array();
let arr = [];
let arr = ['lee', 'hong', 'kim'];
๊ฐ ๋ฐฐ์ด ์์๋ 0๋ถํฐ ์์ํ๋ ์ธ๋ฑ์ค๊ฐ ๋ฐฐ์ ๋์ด ์์ผ๋ฉฐ, ๊ฐ ์ธ๋ฑ์ค
๋ ๋ฐฐ์ด ๋ด ์์๋ฅผ ์๋ฏธํ๋ค.
๋ฐฐ์ด ๋ด ํน์ ์ธ๋ฑ์ค์ ๊ฐ์ ์ป๊ณ ์ถ์ ๊ฒฝ์ฐ, ๋ฐฐ์ด์ ๋ํ๋ด๋ []์์ ์ธ๋ฑ์ค ๋ฒํธ๋ฅผ ๊ธฐ์
ํ๋ฉด ๋๋ค.
console.log(arr[0]); // lee
console.log(arr[1]); // hong
console.log(arr[2]); // kim
์ธ๋ฑ์ค๋ฅผ ์ฐธ๊ณ ํ์ฌ ์์ ์์ ๋ฐ ์ถ๊ฐ๊ฐ ๊ฐ๋ฅํ๋ค.
arr[1] = 'choi'; // 1๋ฒ ์ธ๋ฑ์ค choi๋ก ๋ณ๊ฒฝ
arr[3] = 'koo'; // 3๋ฒ ์ธ๋ฑ์ค์ koo ์ถ๊ฐ
console.log(arr); // [ 'lee', 'bak', 'kim', 'goo' ]
length
๋ฅผ ์ฌ์ฉํด ๋ฐฐ์ด ๋ด ์์์ ๊ฐฏ์๋ฅผ ์์๋ผ ์ ์๋ค.
let arr = ['lee', 'hong', 'kim'];
console.log(arr.length); // 3
let arr = ['lee', 'hong', 'kim'];
arr.push('koo');
console.log(arr); // ['lee', 'hong', 'kim', 'koo']
let arr = ['lee', 'hong', 'kim'];
arr.pop();
console.log(arr); // ['lee', 'hong']
let arr = ['lee', 'hong', 'kim'];
arr.shift();
console.log(arr); // ['hong', 'kim']
let arr = ['lee', 'hong', 'kim'];
arr.unshift('koo');
console.log(arr); // ['koo', 'lee', 'hong', 'kim']
let arr1 = ['foo','bar'];
let arr2 = ['baz','duck'];
let result = arr1.concat(arr2);
console.log(result);
let alp = ['ccccc','aaaa','dddd', 'bbbb'];
let num = [1,5,3,9,7];
console.log(alp.sort()); [ 'aaaa', 'bbbb', 'ccccc', 'dddd' ]
console.log(num.sort()); [ 1, 3, 5, 7, 9 ]
์ฐธ๊ณ ์ฌ์ดํธ
ko.javascript