shift(), unshift()는 배열의 처음에 적용된다. shift()는 배열을 좌측으로 1개 요소만큼 이동하면 맨 좌측의 요소가 배열 밖으로 나오게 되어 결과적으로 배열의 크기를 줄이게 되고, 각 배열 인덱스는 좌측방향으로 1씩 감소한다.
unshift()는 배열의 맨처음(좌측 끝)에 요소를 추가하여 추가한 수만큼 배열의 크기를 조절하고 각 인덱스는 우측방향으로 1씩 증가한다.
array.shift();
array.unshift(item1, item2, ..., itemn);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let array1 = ["apple","pear","orange"];
document.write(array1.length + " , " + array1 + "<br>");
let loseArray = array1.shift(); // 앞부분 1개 요소 사라짐
document.write("삭제된 값 : " + loseArray + "<br>");
document.write("삭제 이후 원본 : " + array1.length + ", " + array1 + "<br>");
let plusArray = array1.unshift("mango","strawberry");
document.write("plusArray : " + plusArray + "<br>");
document.write("추가 이후 원본 : " + array1.length + ", " + array1 );
</script>
</body>
</html>
'웹 프로그래밍 기초 > 객체' 카테고리의 다른 글
sort(), reverse() (0) | 2025.04.07 |
---|---|
splice(), slice() (0) | 2025.04.07 |
indexOf() (0) | 2025.04.07 |
push(), pop() (0) | 2025.04.07 |
concat()메서드 (0) | 2025.04.07 |