컴퓨터는 잘못이 없다..

[JavaScript]Array Type 파헤쳐보기, typeof 연산자, length와 push() 본문

공부/JavaScript

[JavaScript]Array Type 파헤쳐보기, typeof 연산자, length와 push()

도토리까꿍v 2020. 11. 5. 19:02
Contents 접기

[Array Type 특징]

    1. typeof 연산자

    └사용예

    - typeof 변수명

    - typeof 데이터타입명

    ex) typeof num1

    ex) typeof array

 

    2. array type

    -순서가 중요한 데이터를 담는다

    -대괄호 [] 리터럴을 이용해서 만들 수 있다.

    -0부터 시작해서 1씩 증가하는 인덱스가 자동 부여된다.

    -array type의 데이터를 변수에 저장하고 실제로 typeof 변수 해보면 object 가 출력된다.

  

    -array도 object의 부분집합이다. (array도 object이다.)

    -array도 object이기 때문에 변수에 참조값을 담아둔다.

let nums = [10,20,30,40,50];
let names = ["김구라","해골","원숭이"];

 

 

 

 

    3. array type 도 object type! 배열명.~ 으로 참조와 함수호출 가능!

    -배열명.length 

        : 배열의 size를 알 수 있음

    

    -배열명.push(data)

        : 배열에 data 추가 가능, data 추가 후의 배열 size를 return해줌

        (실제로 이 return값은 잘 사용 안함, length가 있기 때문!)

 

[Array Type 예제 코드]

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Step06_array.html</title>
</head>
<body>

    <h1>array type 테스트 중...</h1>
    <script>
        let nums = [10,20,30,40,50];
        let names = ["김구라","해골","원숭이"];

        //빈 배열을 만들고
        let friends=[];
        
        //추후에 값을 하나씩 추가 할수도 있다.
        friends.push("dog");
        friends.push("cat");
        friends.push("elephant");

        //배열의 방의 크기 참조
        //변수(배열의 참조값)에 .(dot) 찍어서 언제든지 size 참조 가능!
        let size1 = nums.length; //5
        let size2 = names.length; //3
        let size3 = friends.length; //3

        

    </script>


    
</body>
</html>

 

Comments