상수와 변수
1. 상수 constant
-> 고정 불변의 값
document.write(3);
document.write(-5);
document.write(3.4);
document.write('A');
document.write("가");
document.write(true);
출력값 : 3
-5
3.4
A
가
true
2. 변수 variable
-> 변수의 성격 : var, let, const, nothing
1) var
var a=3; //a라는 변수를 선언하고 3을 저장
var b=5;
var c=7;
document.write(a); //변수
document.write(b);
document.write(c);
document.write(1+2+3);
document.write(a+b+c);
document.write("a"); //상수
출력값 : a
b
c
6
15
a
3) nothing
-> 변수를 선언하지 않아도 사용할 수 있다.
name="손흥민";
age=25;
height=178.5;
document.write(name);
document.write(age);
document.write(height);
출력값 : 손흥민
25
178.5
a=2;
b=4;
c=6;
name="김연아";
age=30;
height=165.9;
document.write(a);
document.write(b);
document.write(c);
document.write(name);
document.write(age);
document.write(height);
출력값 : 2
4
6
김연아
30
165.9
3) let
-> 반드시 변수를 선언하고 사용한다.
let i=2;
let j=4;
let k=i+j;
document.write(k);
출력값 : 6
에러
-> let으로 선언한 i변수를 이중으로 선언할 수 없다.
let i=2;
let j=4;
let i=8; //에러
4) const
-> 변수를 상수화
const x=10;
document.write(x);
출력값 : 10
에러
-> 변수를 바꿀 수 없다.
const x=10;
const x=9; //에러
'JavaScript' 카테고리의 다른 글
[JS] 연산자 (0) | 2024.06.01 |
---|---|
[JS] document (0) | 2024.06.01 |
[JS] 출력 (0) | 2024.06.01 |
[JS] Data (0) | 2024.06.01 |
[JS] 기본문법 (0) | 2024.06.01 |