const name = [value1, value2, ... ];
const name = new Array(value1, value2, ... );

array.length - 1을 통해 접근할 수 있습니다.


...sports, ...games를 사용해서 배열 정의 가능함.
let fruits = ["apple", "banana"];
fruits.push("cherry"); // ["apple", "banana", "cherry"`
let fruits = ["apple", "banana", "cherry"];
fruits.pop(); // ["apple", "banana"], 반환값: "cherry"
let fruits = ["apple", "banana"];
fruits.unshift("mango"); // ["mango", "apple", "banana"]
let fruits = ["apple", "banana", "cherry"];
fruits.shift(); // ["banana", "cherry"], 반환값: "apple"
let fruits = ["apple", "banana", "cherry"];
fruits.splice(1, 1, "grape"); // ["apple", "grape", "cherry"]
let fruits = ["apple", "banana", "cherry", "mango"];
let sliced = fruits.slice(1, 3); // ["banana", "cherry"]
let fruits = ["apple", "banana"];
let moreFruits = ["cherry", "mango"];
let combined = fruits.concat(moreFruits); // ["apple", "banana", "cherry", "mango"]
let fruits = ["apple", "banana", "cherry"];
let result = fruits.join(", "); // "apple, banana, cherry"
let fruits = ["apple", "banana", "cherry"];
fruits.reverse(); // ["cherry", "banana", "apple"]
let numbers = [10, 3, 2, 7];
numbers.sort(); // [10, 2, 3, 7] (문자열로 정렬됨)
//숫자 정렬 시
numbers.sort((a, b) => a - b); // [2, 3, 7, 10]
let fruits = ["apple", "banana", "cherry"];
fruits.forEach(fruit => console.log(fruit));
// apple
// banana
// cherry
let numbers = [1, 2, 3];
let doubled = numbers.map(num => num * 2); // [2, 4, 6]
let numbers = [1, 2, 3, 4, 5];
let evenNumbers = numbers.filter(num => num % 2 === 0); // [2, 4]
undefined를 반환합니다.let numbers = [1, 2, 3, 4, 5];
let firstEven = numbers.find(num => num % 2 === 0); // 2
let numbers = [1, 2, 3, 4];
let sum = numbers.reduce((acc, curr) => acc + curr, 0); // 10
let fruits = ["apple", "banana", "cherry"];
let hasBanana = fruits.includes("banana"); // true
-1을 반환합니다.let fruits = ["apple", "banana", "cherry"];
let index = fruits.indexOf("banana"); // 1
-1을 반환합니다.let numbers = [1, 2, 3, 4, 5];
let evenIndex = numbers.findIndex(num => num % 2 === 0); // 1 (2의 인덱스)
// iterating through an array
for (let yr of years) {
console.log(yr);
}
for (let i = 0; i < years.length; i++) {
let yr = years[i];
console.log(yr);
}
Array Destructuring : 배열에서 여러 스칼라 값을 추출하는 과정을 단순화하는 방법을 제공
const league = ["Liverpool", "Man City", "Arsenal", "Chelsea"];
//before the ES6
let first = league[0];
let second = league[1];
let third = league[2];
//after the ES6
let [first, second, third] = league;
//요소 건너뛰기
let [first,,,fourth] = league; //첫 번째 요소와 네 번째 요소만 추출
// Spread 문법을 사용한 나머지 요소 추출
//스프레드 문법을 사용하면 배열의 나머지 요소를 새로운 배열로 쉽게 복사할 수 있습니다.
//first는 첫 번째 요소 "Liverpool"을 가지며, 나머지 "Man City", "Arsenal", "Chelsea"는 everyoneElse 배열에 복사됩니다.
let [first, ...everyoneElse] = league;
//before the ES6 --> 임시변수 필요
let tmp = first;
first = second;
second = tmp;
//after the ES6
[second, first] = [first, second];

//let billTotal = prompt("Please enter the total bill : ");
const billTotals = [50,150,20,500];
const tips = [];
for (const total of billTotals) {
let tipPercentage;
if (total > 75) {
tipPercentage = 0.1; // 10%
} else if (total >= 30 && total <= 75) {
tipPercentage = 0.2; // 20%
} else {
tipPercentage = 0.3; // 30%
}
// 5. 팁 계산 및 tips 배열에 추가
const tipAmount = total * tipPercentage;
tips.push(tipAmount);
}
// 6. 결과 출력
for (let i = 0; i < billTotals.length; i++) {
console.log(`For bill of $${billTotals[i]} the tip should be $${tips[i]}`);
}