01. 변수 : 데이터 저장

변수는 데이터를 저장하는 저장소이다. 이 저장소에는 숫자, 문자, 함수, 객체 등을 지정 할 수 있음.

{
    var x = 100;      //변수x에 숫자 100을 저장함 
    var y = 200;      //변수y에 숫자 200을 저장함 
    var z = "javascript";    //변수z에 문자 javascritp를 저장함 

    document.write(x);
    document.write(y);
    document.write(z);
}
결과보기

02. 변수 : 데이터 저장 + 데이터 변경

변수는 데이터를 저장, 변경할 수 있음.

{
    let x = 100;
    let y = 200;
    let z = "javascript";

    x = 300;  //x값이 300으로 변경 됨 
    y = 400;   //y값이 400으로 변경 됨 
    z = "jquery"   //z값이 jquery로 변경 됨 

    document.write(x);
    document.write(y);
    document.write(z);
}
결과보기

03. 변수 : 데이터 저장 + 데이터 변경 + 데이터 추가

변수는 데이터를 저장, 변경, 추가할 수 있음.

 {
    let x = 100;
    let y = 200;
    let z = "javascript";

    x += 300;  //x값에 300이 더해짐  
    y += 400;   //y값에 400이 더해짐  
    z += "jquery"   //z값에 jquery가 더해짐 

    document.write(x);
    document.write(y);
    document.write(z);
}
결과보기

04. 변수 : 지역 변수 + 전역 변수

전역변수는 함수 블록{} 밖에나 안에서 자유롭게 사용 가능하지만, 지역변수는 함수 블록{} 내에서만 사용 할 수 있습니다.

{
    let x = 100; //전역변수
    let y = 200;
    
    function func(){
    let x = 100;   //지역변수
    let z = "javascript"     
    x = 200;  //지역변수 x 100 -> 200으로 변경 
    y = 300; //전역변수 y 200 -> 300으로 변경  


    document.write("함수 안");    
    document.write(x);
    document.write(y);
    document.write(z);
    }
    func();

    document.write("함수 밖");    
    document.write(x);
    document.write(y);
    document.write(z);
}
결과보기
함수 안
200
300
javascript

함수 밖
100
300
undifined

05. 상수 : 데이터 저장 + 데이터 변경(x)

상수는 데이터 저장만 가능하며 변경은 할 수 없음.
상수(const)는 이미 선언한 상수에 대해 중복해서 선언할 수 없고 상수의 값을 재지정할 수도 없음.

{
       const x = 100;
       const y = 200;
       const z = "javascript";
       
       // x = 300;  //변경할 수 없음 
       // y = 400;
       // z = "jquery"

       document.write(x);
       document.write(y);
       document.write(z);
   }
결과보기

06. 배열 : 데이터 저장(여러개) : 표현 방법 1

배열은 데이터를 여러개 저장할 수 있는 저장소이다.

{
    const arr = new Array();

    arr[0] = 100;
    arr[1] = 200;
    arr[2] = "javascript";

    document.write(arr[0]);
    document.write(arr[1]);
    document.write(arr[2]);
}
결과보기

07. 배열 : 데이터 저장(여러개) : 표현 방법 2

{
    const arr = new Array(100,200,"javascript");

    document.write(arr[0]);
    document.write(arr[1]);
    document.write(arr[2]);
}
결과보기

08. 배열 : 데이터 저장(여러개) : 표현 방법 3

{
    const arr = [];

    arr[0] = 100;
    arr[1] = 200;
    arr[2] = "javascript";

    document.write(arr[0]);
    document.write(arr[1]);
    document.write(arr[2]);
}
결과보기

09. 배열 : 데이터 저장(여러개) : 표현 방법 4

{
    const arr = [100,200,"javascript"]

    document.write(arr[0]);
    document.write(arr[1]);
    document.write(arr[2]);
}
결과보기

10. 객체 : 데이터 저장(키와 값) : 표현 방법 1

객체는 데이터 값을 필요한 대로 만들어 사용할 수 있음.

{
    const obj = new Object();
    obj[0] = 100; 
    obj[1] = 200;
    obj[2] = "javascript";

    document.write(obj[0]);
    document.write(obj[1]);
    document.write(obj[2]);
}
결과보기

11. 객체 : 데이터 저장(키와 값) : 표현 방법 2

객체의 데이터는 '이름: 값'의 쌍으로 이루어 있으면 이것을 속성(Properties)라고 함.

{
    const obj = new Object(); 

    obj.a = 100; //a는 key(이름) 100은 value(값) 
    obj.b = 200;
    obj.c = "javascript";

    document.write(obj.a);
    document.write(obj.b);
    document.write(obj.c);
}
결과보기

12. 객체 : 데이터 저장(키와 값) : 표현 방법 3

{
    const obj = {};
    obj.a = 100; 
    obj.b = 200;
    obj.c = "javascript";

    document.write(obj.a);
    document.write(obj.b);
    document.write(obj.c);
}
결과보기

13. 객체 : 데이터 저장(키와 값) : 표현 방법 4

{
    const obj = {a:100, b:200, c:"javascript"};

    document.write(obj.a);
    document.write(obj.b);
    document.write(obj.c);
}
결과보기

14. 객체 : 데이터 저장(키와 값) : 표현 방법 5

배열 속에 객체를 쓸 수 있음.

{
    const obj = [
        {a:100, b:200},
        {c:"javascript"}
    ];

    document.write(obj[0].a);
    document.write(obj[0].b);
    document.write(obj[1].c);
}
결과보기

15. 객체 : 데이터 저장(키와 값) : 표현 방법 6

객체 속에 배열을 쓸 수 있음.

{
    const obj = {
        a: 100,
        b: [200, 300],
        c: {x:400, y:500},
        d: "javascript"
    }

    document.write(obj.a);
    document.write(obj.b[0]);
    document.write(obj.b[1]);
    document.write(obj.c.x);
    document.write(obj.c.y);
    document.write(obj.d);
}
결과보기

16. 객체 : 데이터 저장(키와 값) : 표현 방법 7

객체 속에 변수를 쓸 수 있음.

{
    const a = 100;
    const b = 200;
    const c = "javascript";

    const obj = {a, b, c}

    document.write(obj.a); //100출력
    document.write(obj.b);
    document.write(obj.c);
}
결과보기

17. 객체 : 데이터 저장(키와 값) : 표현 방법 8

객체 속에 함수를 쓸 수 있음.

{
    const obj = {
        a : 100,
        b : [200, 300],
        c : {x:400, y:500},
        d : "javascript",
        e : function(){
            document.write("자바스크립트가 실행되었습니다.");
        },
        f : function(){
            document.write( obj.d + "가 실행되었습니다.");
        },
        g : function(){
            document.write( this.d +"가 실행되었습니다.");
        }
    }
    
    document.write(obj.a);
    document.write(obj.b[0]);
    document.write(obj.b[1]);
    document.write(obj.b); //200,300 이 출력
    document.write(obj.c.x);
    document.write(obj.c.y);
    document.write(obj.d);
    obj.e(); //자바스크립트가 실행되었습니다.
    obj.f(); 
    obj.g();
}
결과보기
100
200
300
400
500
"javascript"
자바스크립트가 실행되었습니다.
javascript가 실행되었습니다.
javascript가 실행되었습니다.