본문 바로가기

Javascript/기초

배열의 마지막으로 데이터 추가하기

배열의 마지막에 데이터를 추가하고 싶다면

push() 를 통해 배열에 데이터가 추가됩니다.

  1. push를 사용하여 배열에 cd, ef 라는 값을 추가해 보자.
<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <h1>Array</h1>
        <h2>Syntax</h2>
        <script>
            let boom = ["A", "B"];

            boom.push('cd');
            boom.push('ef');
        </script>

        <h2>get</h2>
        <script>
            document.write(boom[0]); //A
            document.write(boom[1]); //B
            document.write(boom[2]); //cd
            document.write(boom[3]); //ef

        </script>

        <h2>add</h2>
        <script>
           
        </script>

        <h2>count</h2>
        <script>
            document.write(boom.length); //4
        </script>
    </body>
</html>