- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariable.js
More file actions
Latest commit
114 lines (95 loc) · 3.5 KB
/
Copy pathvariable.js
File metadata and controls
114 lines (95 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// 1. Use strict
// added in ES5
// use this for Valina JavaScript.
'use strict';
// 2. Variable, rw(read/write)
// let (added in ES6)
letglobalName='global name'
{
letname='sill';
console.log(name);
name='hello';
console.log(name);
console.log(globalName);
}
console.log(globalName);
// var (don't ever use this!)
// var hoisting (move declaration from bottom to top; 어디에 선언했는가와 상관없이 항상 선언을 제일 위로 끌어 올려주는 것)
// var는 블럭스콥을 무시함
{
console.log(age);
age=4;
varage;
}
console.log(age);
// 3. Contant, r(read only)
// const(한 번 할당하면 값이 변하지 않음 -> favor immutale data type always)
// use const whenever possible.
// only use let if variable needs to change.
constdatyInWeek=7;
constmaxNumber=5;
// Note!
// Immutable data type: primitive types, frozen objects (i.e. object.freeze())
// Mutable data types: all objects by default are mutable in JS
// favor immutable data type always for a few reasons:
// - security
// - thread safety
// - reduce human mistakes
// 4. Variable types
// primitive, single item(더 이상 작은 단위로 나뉘어질 수 없는, 한 가지 타입): number, string, boolean, null, undefined, symbol
// object(single 단위를 여러 개 묶음), box container
// funcion, first-class function
constcount=17;// integer
constsize=17.1;// decimal number
console.log(`value: ${count}, type: ${typeofcount}`);
console.log(`value: ${size}, type: ${typeofsize}`);
// number - special numeric values: infinity, -infinity, NaN
constinfinity=1/0;
constnegativeInfinity=-1/0;
constnAn='not a number'/2;
console.log(infinity);
console.log(negativeInfinity);
console.log(nAn);
// bigInt(fairly new, don't use it yet)
constbigInt=123456789013456789012345678901234567890;// over (-2**53) ~ 2**53
console.log(`value: ${bigInt}, type: ${typeofbigInt}`);
Number.MAX_SAFE_INTEGER;
// string
constchar='a';
constbrendan='brendan';
constgreeting='hello'+brendan;
console.log(`value: ${greeting}, type: ${typeofgreeting}`);
consthelloBob=`hi ${brendan}!`// template literals (string)
console.log(`value: ${helloBob}, type: ${typeofhelloBob}`);
// boolean
// false: 0, null, undefined, NaN, ''
// true: any other value
constcanRead=true;
consttest=3<1;// false
console.log(`value: ${canRead}, type: ${typeofcanRead}`);
console.log(`value: ${test}, type: ${typeoftest}`);
// null
letnothing=null;
console.log(`value: ${nothing}, type: ${typeofnothing}`);
// undefined
letx;
console.log(`value: ${x}, type: ${typeofx}`);
// symbol, create unique identifiers for objects
constsymbol1=Symbol('id');
constsymbol2=Symbol('id');
console.log(symbol1===symbol2);// false
console.log(`value: ${symbol1.description}, type: ${typeofsymbol1}`);// symboldms .description을 붙여야지 값을 콘솔로 찍을 수 있음
// object, real-life object, data structure
constsill={name: 'sill',age: 20};
sill.age=21;// 변경 가능!
// 5. Dynamic typing: dynamically typed language
lettext='hello';
console.log(text.charAt(0));// h
console.log(`value: ${text}, type: ${typeoftext}`);// type -> ㄴstring
text=4;
console.log(`value: ${text}, type: ${typeoftext}`);// type -> number
text='7'+5;
console.log(`value: ${text}, type: ${typeoftext}`);// value 75 but, type -> string
text='8'/'2';
console.log(`value: ${text}, type: ${typeoftext}`);// value 4, type -> number
// console.log(text.charAt(0)) -> text type은 string이기 때문에 에러 발생