๋ค์ ์ฝ๋์ ์คํ ๊ฒฐ๊ณผ๋ฅผ ์ฐ์์ค.
if ( [ ] ) console . log ( 1 ) ;
if ( [ 1 , 2 , 3 ] ) console . log ( 2 ) ;
if ( undefined ) console . log ( 3 ) ;
if ( null ) console . log ( 4 ) ;
if ( NaN ) console . log ( 5 ) ;
1
2
// ๋ฐฐ์ด์ ๋น์์ด๋ true๋ฅผ ๋ฐํํ๋ค.
undefined, null, NaN์ falsy ๊ฐ์ด๋ค.
๋ค์ ์ฝ๋์ ์คํ ๊ฒฐ๊ณผ๋ฅผ ์ฐ์์ค.
const func = ( ) => { } ;
console . log ( func + '' ) ;
console . log ( func ( ) + '' ) ;
console . log ( ! ! func ) ;
console . log ( + func ) ;
console . log ( func + 1 ) ;
+ function ( ) { console . log ( "Foo!" ) } ( ) ;
() => {} (๋ฌธ์์ด)// string ์๋ ํ๋ณํ
undefined(๋ฌธ์์ด) // undefined + ''
true // !์ฐ์ฐ์ ์ ์๋ ํ๋ณํ
nan // Number type์ผ๋ก ์๋ ํ๋ณํ
() => {}1(๋ฌธ์์ด) (์ฑ
์ ์์)
Foo // ํจ์ ์ ์ ์์ ๋จํญ์ฐ์ฐ์๊ฐ ์ค๋ฉด ์ฆ์ ์คํ ํจ์์ฒ๋ผ ๋์(์ฑ
์ ์์)
๋จ์ถํ๊ฐ๋ ๋ฌด์์ธ์ง ์์ฑํ์์ค.
const result = "Cat" && "Dog" // "Dog"
// ๋
ผ๋ฆฌ ์ฐ์ฐ์ ๊ฒฐ๊ณผ๋ฅผ ๊ฒฐ์ ํ๋ ํผ์ฐ์ฐ์์ ํ์
์ ๋ณํํ์ง์๊ณ ๊ทธ๋๋ก ๋ฐํํ๋ค.
// ์ฐธ๊ณ :๋จ๋ฝํ๊ฐ.
// OR์ฐ์ฐ์๋ truthy๋ฅผ ๋ง๋๋ฉด ๋๋จธ์ง ๊ฐ๋ค์ ๊ฑด๋๋ฆฌ์ง ์์ ์ฑ ํ๊ฐ๋ฅผ ๋ฉ์ถ๋ค.
// &&์ฐ์ฐ์๋ falsy๋ฅผ ๋ง๋๋ฉด ๋๋จธ์ง ๊ฐ๋ค์ ๊ฑด๋๋ฆฌ์ง ์์ ์ฑ ํ๊ฐ๋ฅผ ๋ฉ์ถ๋ค.
true || alert ( "1" ) ; // x
false || alert ( "2" ) ; // o
true && alert ( "3" ) ; // o
false && alert ( "4" ) ; // x
// alertํจ์
alert ( alert ( "1" ) && 3 ) ; // 1,undefined ์ผ๋ฟ์ฐฝ ์ถ๋ ฅ (alertํจ์๋ undefined๋ฅผ ๋ฐํํ๋ค.)
์ต์
๋ ์ฒด์ด๋, null๋ณํฉ ์ฐ์ฐ์์ ์ฌ์ฉ์ฌ๋ก๋ฅผ ์์ฑํ์์ค.
(1): ์ต์
๋ ์ฒด์ด๋, null๋ณํฉ ์ฐ์ฐ์์ ์ฌ์ฉ์ฌ๋ก๋ฅผ ์์ฑํ์์ค.
(2): ๋
ผ๋ฆฌ์ฐ์ฐ์๋ฅผ ํตํ ๋จ์ถํ๊ฐ์ ๋น๊ตํ์๋ ์ต์
๋ ์ฒด์ด๋, null๋ณํฉ ์ฐ์ฐ์์ ์ฅ์ ์๋ํด์ ์์ฑํ์์ค.
(1)
- ์ต์
๋ ์ฒด์ด๋ : ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํค๊ธฐ๋ฅผ ๊ธฐ๋ํ๋ ๋ณ์๊ฐ null ๋๋ undefined๊ฐ ์๋์ง ํ์ธํ๊ณ ํ๋กํผํฐ๋ฅผ ์ฐธ์กฐํ ๋.
- null๋ณํฉ ์ฐ์ฐ์ : ๋ณ์์ ๊ธฐ๋ณธ๊ฐ์ ์ค์ ํ ๋.
(2)
- Falsy๊ฐ์ด์๋, ์ค์ง null๊ณผ undefined์ธ์ง๋ฅผ ํ๊ฐํ๊ธฐ๋๋ฌธ์ ๊ฒฐ๊ณผ๋ฅผ ์์ํ๊ธฐ ์ฝ๋ค.
๋ค์ ์ฝ๋์ ์ถ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ์์ธกํ์ธ์.
let x = 10 ;
let str = x + "" ;
console . log ( typeof str , str ) ; // (1)
console . log ( typeof x , x ) ; // (2)
(1) string 10
(2) number 10
109p
์๋ฌต์ ํ์
๋ณํ์ ๊ธฐ์กด ๋ณ์ ๊ฐ์ ์ฌํ ๋นํ์ฌ ๋ณ๊ฒฝํ๋ ๊ฒ์ด ์๋๋ค.
์๋ฐ์คํฌ๋ฆฝํธ ์์ง์ ํํ์์ ์๋ฌ ์์ด ํ๊ฐํ๊ธฐ ์ํด ํผ์ฐ์ฐ์์ ๊ฐ์ ์๋ฌต์ ํ์
๋ณํํ์ฌ
์๋ก์ด ํ์
์ ๊ฐ์ผ๋ก ๋ง๋ค์ด ๋จ ํ ๋ฒ ์ฌ์ฉํ๊ณ ๋ฒ๋ฆฐ๋ค.
๋ค์ ์ฝ๋์ ์คํ ๊ฒฐ๊ณผ๋ฅผ ์์ธกํ์ธ์.
console . log ( ( "A" && "B" ) || false ) ; // (1)
console . log ( false || "A" || false ) ; // (2)
console . log ( "A" && true && true ) ; // (3)
118p
๋
ผ๋ฆฌ๊ณฑ, ๋
ผ๋ฆฌํฉ ์ฐ์ฐ์๋ ๋จ์ถ ํ๊ฐํ์ฌ ๋ฐํํ๋ค.
๋ฐ๋ผ์ ํํ์์ ํ๊ฐํ๋ ๋์ค์ ํ๊ฐ ๊ฒฐ๊ณผ๊ฐ ํ์ ๋๋ฉด, ๋๋จธ์ง ํ๊ณผ ๊ณผ์ ์ ์๋ตํ๋ค.
๋ค์ ์ฝ๋์ ์ถ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ์์ฑํ์์ค.
console . log ( ( undefined + 1 ) ) ; // (1)
console . log ( [ ] && true ) ; // (2)
console . log ( { } && true ) ; // (3)
console . log ( ( [ 10 , 20 ] + 1 ) ) ; // (4)
(1) : NaN
+ ์ฐ์ ์ฐ์ฐ์์ ์ํด์ NaN์ด ์ถ๋ ฅ๋๋ค.
(2) : true
๋น ๋ฐฐ์ด์ Truthy ๊ฐ์ผ๋ก ํ๊ฐ๋๋ค.
(3) : true
๋น ๊ฐ์ฒด์ Truthy ๊ฐ์ผ๋ก ํ๊ฐ๋๋ค.
(4) : 10,201
๋ฌธ์์ด ์ฐ๊ฒฐ ์ฐ์ฐ์์ ์ํด ๋ฌธ์์ด ํ์
์ด ์๋ ํผ์ฐ์ฐ์๋ฅผ ๋ฌธ์์ด ํ์
์ผ๋ก ์๋ฌต์ ํ์
๋ณํํ๋ค.
์ต์
๋ ์ฒด์ด๋ ์ฐ์ฐ์?.
๋ฅผ ์ด์ฉํ์ฌ null ๋๋ undefined๋ฅผ ํ๋จํ ๋์ ๋
ผ๋ฆฌ ์ฐ์ฐ์&&
๋ฅผ ์ด์ฉํ์ฌ ํ๋จํ ๋ ์ฐจ์ด์
์ ์์ฑํ์์ค.
์ต์
๋ ์ฒด์ด๋ ์ฐ์ฐ์๋ ํด๋น ๋ณ์์ null, undefined ์ธ ๊ฒฝ์ฐ๋ฅผ ๊ธฐ์ค์ผ๋ก ํ๋จํ๊ณ ,
๋
ผ๋ฆฌ ์ฐ์ฐ์(&&)๋ Falsy ๊ฐ์ธ ๊ฒฝ์ฐ๋ฅผ ๊ธฐ์ค์ผ๋ก ํ๋จํ๋ค.
Falsy : false, undefined, null, 0, -0, NaN, ''
๋ํ 0์ด๋ ''์ด ๋ํผ ๊ฐ์ฒด์ ์ํด ๊ฐ์ฒด๋ก ํ๊ฐ๋ ์ ์๋ค.