-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperator.js
More file actions
184 lines (155 loc) · 4.37 KB
/
operator.js
File metadata and controls
184 lines (155 loc) · 4.37 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// 1. String concatenation(문자열 + 문자열)
console.log('my' + ' cat')
console.log('1' + 2)
console.log(`string literals: 1+ 2 = ${1 + 2}`)
// 2. Numeric operators(숫자 연산)
console.log(1 + 1) // add
console.log(1 - 1) // substract
console.log(1 / 1) // divide
console.log(1 * 1) // multiply
console.log(5 % 2) // remainder
console.log(2 ** 3) // --> 2^3 exponentiation
// 3. Increment and decrement operators(증가 감소)
let counter = 2
const preIncrement = ++counter
// counter = counter + 1
// preIncrement = counter
console.log(`preIncrement: ${preIncrement}, counter: ${counter}`)
const postIncrement = counter++
// postIncrement = counter;
// counter = counter + 1
console.log(`preIncrement: ${postIncrement}, counter: ${counter}`)
const preDecrement = --counter
console.log(`preIncrement: ${preDecrement}, counter: ${counter}`)
const postDecrement = counter--
console.log(`preIncrement: ${postDecrement}, counter: ${counter}`)
// 4. Assigment operators(할당)
let x = 3
let y = 6
x += y // x = x + y
x -= y
x *= y
x /=y
// 5. Comparison operators (비교)
console.log(10 < 6) // less than
console.log(10 <= 6) // less than or equal
console.log(10 > 6) // greater than
console.log(10 >=6) // greater than or equal
// 6. Logical operators : || (or), && (and), ! (not)
const value1 = true
const value2 = 4 < 2
// || (or), finds the first truthy value (처음으로 true가 나오면 멈춤)
console.log(`or : ${value1 || value2 || check()}`)
// && (and)), finds the first falsy value (처음으로 false가 나오면 멈춤)
console.log(`and : ${value1 && value2 && check()}`)
// often used to compress long if - statement
// nullableObject && nullableObject.something (간편한 null 체크할때도 사용)
// if(nullableObject !=null){
// nullableObject.something
// }
function check(){
for(let i=0; i<10; i++){
//wasting time
console.log('ㅠㅠ')
}
return true
}
// ! (not)
console.log(!value1)
// 7. Equality
const stringFive = '5'
const numberFive = 5
// == loose equality, with type conversion
console.log(stringFive == numberFive)
console.log(stringFive != numberFive)
// === strict equality, no type conversion
console.log(stringFive === numberFive)
console.log(stringFive !== numberFive)
// object equality by reference
const ellie1 = { name: 'ellie'}
const ellie2 = { name: 'ellie'}
const ellie3 = ellie1
console.log(ellie1 == ellie2)
console.log(ellie1 === ellie2)
console.log(ellie1 === ellie3)
// equality = puzzler
console.log(0 == false)
console.log(0 === false)
console.log('' == false)
console.log('' === false)
console.log(null == undefined)
console.log(null === undefined)
// 8. Conditional operators : if
// if, else if , else
const name = 'ellie'
if(name==='ellie'){
console.log('Welcome, Ellie!')
}else if(name==='coder'){
console.log('You are amazing coder')
}else{
console.log('unkwnon')
}
// 9. Ternary operator: ?
// condition ? value1 : value2 ;
console.log(name === 'ellie' ? 'yes' : 'no')
// 10. Switch statement
// use for multiple if checks
// use for enum-like value check
// use for multiple type checks in TS
const browser = 'IE'
switch(browser){
case 'IE' :
console.log('go away!')
break;
case 'Chrome':
case 'Firefox':
console.log('love you!')
break
default:
console.log('same all!')
break
}
// 11. Loops
// while loop, while the condition is truthy,
// body code is executed.
let i = 3
while (i>0){ // statement가 false로 나오기 전까지 무한대로 반복해서 돈다.
console.log(`while: ${i}`)
i--;
}
// do while loop, body code is executed first,
// than check the condition.
do{
console.log(`do while: ${i}`)
i--;
} while (i>0)
// for loop, for(begin; condition; step)
for(i = 3; i>0; i--){
console.log(`for: ${i}`)
}
for (let i=3; i>0; i=i-2){
// inline variable declaration
console.log(`inline variable for: ${i}`)
}
// nested loops
for(let i =0; i<10; i++){
for(let j = 0; j<10; j++){
console.log(`i: ${i}, j: ${j}`)
}
}
// break, continue
// Q1. iterate from 0 to 10 and print only even numbers(use continue)
for(let i=0; i<11; i++){
if(i%2==0){
console.log(`Q1. ${i}`)
}
}
for(let i=0; i<11; i++){
if(i%2 == 1) continue
console.log(`Q1. ${i}`)
}
// Q2. iterate from 0 to 10 and print numbers nutil reaching 8 (use break)
for (let i =0; i<11; i++){
if(i>8) break
console.log(`Q2. ${i}`)
}