console . log ( false ?? 'hello' ) ;
console . log ( null ?? 'bye' ) ;
console . log ( '' ?? 'hi' )
console . log ( '' ? 'hello' : 'bye' ) ;
false
bye
// ๋น ๋ฌธ์์ด ์ถ๋ ฅ
bye
?? ์ฐ์ฐ์๋ ?? ์์ ๊ฐ์ด null ์ด๊ฑฐ๋ undefined์ผ ๋ ๋ค์ ๊ฐ์ ๋ฐํํด์ค๋ค.
์ผํญ ์ฐ์ฐ์์ ๋ค๋ฅด๊ฒ ์๋ฌต์ ํ๋ณํ์ ํ์ง ์๋๋ค.
๋ค์ ์ฝ๋์ ์ถ๋ ฅ๊ฒฐ๊ณผ๋ฅผ ์์ธกํด์ฃผ์ธ์.
const a = undefined ;
const b = null ;
const c = 123 ;
const a1 = a ?? "๊ฐ์ด ์์ต๋๋ค." ;
console . log ( a1 ) ; //(1)
const b1 = b ?? "๊ฐ์ด ์์ต๋๋ค." ;
console . log ( b1 ) ; //(2)
const c1 = c ?? "๊ฐ์ด ์์ต๋๋ค." ;
console . log ( c1 ) ; //(3)
(1): "๊ฐ์ด ์์ต๋๋ค."
(2): "๊ฐ์ด ์์ต๋๋ค."
(3): 123
-> ??๋ ์ขํญ์ ํผ์ฐ์ฐ์๊ฐ null ๋๋ undefined์ธ ๊ฒฝ์ฐ ์ฐํญ์ ํผ์ฐ์ฐ์๋ฅผ ๋ฐํํ๊ณ ๊ทธ๋ ์ง ์์ผ๋ฉด ์ขํญ์ ํผ์ฐ์ฐ์๋ฅผ ๋ฐํํ๋ค. (๋ณ์์ ๊ธฐ๋ณธ๊ฐ์ ์ค์ ํ ๋ ์ ์ฉ.)
์ฐ์ฐ์์ ๋ถ์ ํจ๊ณผ๊ฐ ๋ฌด์์ธ์ง ์ค๋ช
ํ๊ณ , ์ ์ค ๋ถ์ํจ๊ณผ๋ฅผ ์ผ์ผํค๋ ์ฐ์ฐ์๋ฅผ ์ฐพ์ผ์์ค
(1) ํ ๋น ์ฐ์ฐ์ (=)
(2) ์ผํญ ์ฐ์ฐ์
(3) ์ฆ๊ฐ ์ฐ์ฐ์
(4) ์ฐ์ ์ฐ์ฐ์
1, 3
์ฐ์ฐ์์ ๋ถ์ ํจ๊ณผ๋ ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ ๋ ํผ์ฐ์ฐ์์ ๊ฐ์ด ๋ณํ๋๋ ์ผ์ ๋งํ๋ค.
์ผํญ ์กฐ๊ฑด ์ฐ์ฐ์
์ if ... else๋ฌธ
์ ์ฐจ์ด์ ์ ์์ฑํ์์ค.
์ผํญ ์กฐ๊ฑด ์ฐ์ฐ์ ํํ์์ ๊ฐ์ผ๋ก ํํ๋๊ณ ,
if...else ๋ฌธ์ ํํ์์ด ์๋ ๋ฌธ์ด๋ฏ๋ก ๊ฐ์ผ๋ก ์ฌ์ฉํ ์ ์๋ค.
(1) ๋ค์ ์ฝ๋์ ์ถ๋ ฅ ๊ฐ์ ์์ฑํ์์ค.
let x = 1 ;
x = x + ++ x + x ;
console . log ( x ) ;
(2) ๋ค์ ๋ณด๊ธฐ์ ์ฐ์ฐ์ ์ฐ์ ์์๊ฐ ๋์ ๊ฒ๋ถํฐ ๋์ดํ์์ค.
(3) ๋ค์ ์ฝ๋ ์ํ ํ, x
์ ๊ฐ์ ์์ฑํ์์ค.
let x = 1 , y = 1 ;
x += ++ x && y != x ++ || y ;
(1) : 5
++x ๊ฐ์ด ์ํ๋ ํ ์ด๋ฒ์งธ ํญ์ ์๋ฆฌ์ก์ผ๋ฏ๋ก ์ธ๋ฒ์งธ ํญ์ ๋ค์ด์ค๋ x๊ฐ์ 2์ด๋ค.
(2) : x++ ++x != && || +=
(3) : 2
let x = 1 , y = 1 ;
x += ++ x && y != x ++ || y ;
x = x + ( ++ x && y != x ++ || y ) ;
x = x + ( ( ++ x ) && y != ( x ++ ) || y ) ;
x = x + ( ( ++ x ) && ( y != ( x ++ ) ) || y ) ;
x = x + ( ( ( ++ x ) && ( y != ( x ++ ) ) ) || y ) ;
๋ค์ ์ถ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ์์ธกํ์ธ์.
let num = 3 ;
console . log ( num -- ) ; // (1)
console . log ( ++ num ) ; // (2)
console . log ( 5 == "5" ) ; // (3)
console . log ( 5 === "5" ) ; // (4)
console . log ( NaN === NaN ) ; // (5)
(1) 3
(2) 3
(3) true
(4) false
(5) false