02. 변수 만들기
- 숫자로 시작하면 안 됨.
- 소문자로 시작.
- 상수는 모두 대문자로 만들기. (var ADMIN_ID = "ddandongne";
- 여러 단어 조합시 카멜표기법!
- 예약어(키워드) 사용 불가.
03. 변수에 저장할 수 있는 데이터 종류
( * 은 기본타입)
- 숫자형(Number) *
- 문자형(String) *
- 논리형(Boolean) *
- 특수형(undefined) : 변수는 선언했지만 값이 아직 할당되지 않은 상태.
- 특수값(null) : 참조형변수의 주소값이 없음. 타입: Object
- 함수(Function)
- 객체(Object)
07. 변수에 어떤 값이 들어 있는지 확인하기.
- alert()
- document.write() : HTML body 영역에 <div>와 같은 태그 내용을 출력해 줌.
- console.log()
<script>
// 자바스크립트(프로그래밍)영역
var num = 10;
var str = '"hello world"\t\n\uac00';
var bool = true;
/* */
console.log(num);
console.log(str);
console.log(bool);
// alert(num);
// alert(str);
// alert(bool);
document.write(num);
document.write(str);
document.write(bool);
document.write("<h1>hello</h1>");
var a;
a = 10;
var b;
// alert(b);
// alert(c);
// alert(a);
a = undefined;
var fun = function add(a, b) { // 리턴타입, 파라미터 타입이 없음.
return a + b;
}
// var c = add(10, 20);
// alert(c);
// 객체는 있다, 클래스는 없다.
var object = {};
console.log("fun : " + fun);
console.log("object : " + object);
// 자바스크립트의 변수형(자료형)은 들어간 데이터에 의해서 결정됨.
// 변수나 리터럴의 자료형을 알아보는 연산자가 존재. typeof
function MyClass() {
}
var myClass = MyClass;
console.log(typeof num);
console.log(typeof str);
console.log(typeof bool);
console.log(typeof a);
console.log(typeof fun);
console.log(typeof object);
console.log(typeof myClass);
console.log(typeof null);
var funObj = new fun();
console.log(30 == true); // true가 1로 변환됨.
for(var i = 5 ; i-- ;) {
console.log(i);
}
var f = function(x) {
// alert(x * x);
alert(x);
};
// 인자없이 함수 호출
var ret = f();
// 리턴값이 없는 함수의 결과를 출력
</script>
09. 배열
- var 변수이름 = [데이터,,,];
<script>
// scope : 범위
data1 = 10;
data1 = 20;
data1 = 30; // 중복선언 개념이 없음.
console.log(data1);
// 배열: 하나의 변수로 여러 데이터를 저장
// "인덱스"를 통해 값을 지정, 조회, 수정
// 배열의 선언
var arr = [10, 20, 30, 40, "가", false, function() {}, null, undefined, {}];
// !!!
console.log(arr.length);
// 10칸 배열 만드는 법
var arr = {};
arr.length = 10;
var arr = new Array(10);
//
var arr = new Array(10, 20, 30, 40); // 인자의 값으로 배열을 생성 [10, 20, 30, 40]
// 객체
var obj = {x : 10, y : 20};
console.log(obj.x);
console.log(obj.y);
obj.z = 20;
console.log(obj);
var student = {no : 1, name : "홍길동", kor : 90, eng : 80, mat : 70
, total : function() {return this.kor + this.eng + this.mat}
, toString : function() {return this.no + "," + this.name + "," + this.total()}
}; // 후반부에 함.
var student2 = {no : 2, name : "2길동", kor : 20, eng : 50, mat : 70};
var student3 = {no : 3, name : "3길동", kor : 30, eng : 40, mat : 70};
var students = [];
students.push(student);
students.push(student2);
students.push(student3);
console.log(students);
</script>
'자바스크립트' 카테고리의 다른 글
JS Scope & JS Hoisting (w3schools) (0) | 2021.02.20 |
---|---|
Part 01. 부록02- jQuery 21. 02. 19. (0) | 2021.02.20 |
Part 01. 03장- 형변환 21. 02. 19. (0) | 2021.02.20 |
Part 01. 부록01- 함수 21. 02. 19. (0) | 2021.02.20 |
Part 01. 02장- 기본 연산자 21. 02. 19. (0) | 2021.02.20 |