TIL02. JavaScript : About Six Operator

ID์งฑ์žฌยท2021๋…„ 8์›” 24์ผ
0

JavaScript

๋ชฉ๋ก ๋ณด๊ธฐ
14/19
post-thumbnail

๐Ÿ“Œ ์ด ํฌ์ŠคํŒ…์—์„œ๋Š” JavaScript์˜ 6๊ฐ€์ง€ ์—ฐ์‚ฐ์ž์™€ ์—ฐ์‚ฐ์ž ๋น„๊ต ์‹œ, ์˜ˆ์™ธ์‚ฌํ•ญ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.


๐ŸŒˆ About Six Operator

๐Ÿ”ฅ 6๊ฐœ์˜ ์—ฐ์‚ฐ์ž ์ดํ•ดํ•˜๊ธฐ

๐Ÿ”ฅ 0, null, undefined ๋น„๊ต



1. About Six Operator

JavaScript์—์„œ ์—ฐ์‚ฐ์ž๋Š” ์‚ฌ์น™์—ฐ์‚ฐ์ž, ์ฆ๊ฐ์—ฐ์‚ฐ์ž, ํ• ๋‹น์—ฐ์‚ฐ์ž, ๋น„๊ต์—ฐ์‚ฐ์ž, ๋…ผ๋ฆฌ์—ฐ์‚ฐ์ž, ๋™๋“ฑ์—ฐ์‚ฌ์ž๋กœ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


๐Ÿค” ์‚ฌ์น™์—ฐ์‚ฐ์ž๋ž€?

์‚ฌ์น™์—ฐ์‚ฐ์ž๋Š” ์ˆ˜ํ•™ ์‹œ๊ฐ„์— ๋ฐฐ์šด ๋ง์…ˆ(+), ๋บ„์…ˆ(-), ๊ณฑ์…ˆ(*), ๋‚˜๋ˆ—์…ˆ(/), ๋‚˜๋จธ์ง€(%) ๋“ฑ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
๋ง์…ˆ์—๋Š” ๋ฐ์ดํ„ฐ ํƒ€์ž…์— ๋”ฐ๋ฅธ ๊ทœ์น™์ด ์žˆ์Šต๋‹ˆ๋‹ค. 1) plus : ์ˆซ์ž๋ผ๋ฆฌ๋Š” ๋”ํ•ฉ๋‹ˆ๋‹ค. 2) concat : 2์ค‘ 1๊ฐœ๋ผ๋„ ํ•ญ์ด string์ด๋ฉด ๋ถ™์ž…๋‹ˆ๋‹ค.

console.log(1 + 1) // ๐Ÿ‘ˆ 2
console.log('Hello' + 999) // ๐Ÿ‘ˆ Hello999
console.log(999 + 'Hello') // ๐Ÿ‘ˆ 999Hello
console.log('6' + 3); // ๐Ÿ‘ˆ 63
console.log(6 + '3'); // ๐Ÿ‘ˆ 63
console.log(2 + 4 + '3'); // ๐Ÿ‘ˆ 63

๋บ„์…ˆ, ๊ณฑ์…ˆ, ๋‚˜๋ˆ—์…ˆ, ๋‚˜๋จธ์ง€ ์—ฐ์‚ฐ์ž ๋“ฑ์—์„œ๋Š” ๋‘๊ฐœ์˜ ํ•ญ์ด ๋ชจ๋‘ string์ด๋ผ๋„ ์šฐ์„  ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๊ณ„์‚ฐ์„ ์‹œ๋„ํ•ฉ๋‹ˆ๋‹ค.
์ˆซ์ž๋กœ ๋ณ€ํ™˜๋˜์ง€ ์•Š์€ ์ง„์งœ ๋ฌธ์ž์—ด์ด ํ•˜๋‚˜๋ผ๋„ ์žˆ๋‹ค๋ฉด Nan(Not a Number)๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ˆซ์ž๊ฐ€ ์•„๋‹ˆ๋ผ๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค.

console.log(10 - 2) // ๐Ÿ‘ˆ 8
console.log(5 * 2) // ๐Ÿ‘ˆ 10
console.log(8 / 2) // ๐Ÿ‘ˆ 4
console.log(7 % 2) // ๐Ÿ‘ˆ 1
console.log(6 - '2'); // ๐Ÿ‘ˆ 4
console.log('3' * 2); // ๐Ÿ‘ˆ 6
console.log('6' / '2'); // ๐Ÿ‘ˆ 3
console.log('9' % '2'); // ๐Ÿ‘ˆ 1
console.log('Hello' - 999) // ๐Ÿ‘ˆ Nan
console.log(999 * 'Hello') // ๐Ÿ‘ˆ Nan

JavaScript์—์„œ ๋ชซ์€ ์–ด๋–ป๊ฒŒ ๊ตฌํ• ๊นŒ์š”? Python์—์„œ๋Š” ๋‚˜๋ˆ„๊ธฐ(/), ๋ชซ(//), ๋‚˜๋จธ์ง€(%) ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, JavaScript์—์„œ๋Š” //๋Š” ์ฃผ์„์„ ์˜๋ฏธํ•˜์ฃ . ๋ชซ์„ ์†์‰ฝ๊ฒŒ ๊ตฌํ•ด์ฃผ๋Š” ์—ฐ์‚ฐ์ž๊ฐ€ ์—†๋‚˜๋ด…๋‹ˆ๋‹ค. ์ด์— parseInt ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ชซ์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค.

console.log(parseInt(5/2)) // 2
console.log(parseInt(33/2)) // 16
console.log(parseInt(32/8)) // 4

๐Ÿค” ์ฆ๊ฐ์—ฐ์‚ฐ์ž๋ž€?

์ˆซ์ž๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ์ฆ๊ฐ€(++)์‹œํ‚ค๊ฑฐ๋‚˜ ๊ฐ์†Œ(--)์‹œํ‚ค๋Š” ๊ฒƒ์ด ์ฆ๊ฐ์—ฐ์‚ฐ์ž์ด๋ฉฐ, ๋ฐ˜๋ณต๋ฌธ์—์„œ ์ฃผ๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
์ฆ‰, ++๋Š” counter = count + 1๋ฅผ ์ถ•์•ฝํ•˜์—ฌ ํ‘œํ˜„ํ•œ ์‹์ด๊ณ , --๋Š” counter = count - 1๋ฅผ ์ถ•์•ฝํ•œ ๊ฒƒ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

let counter = 100;
counter++;  // 101 ๐Ÿ‘ˆ counter = count + 1
counter++;  // 102 ๐Ÿ‘ˆ counter = count + 1
counter++;  // 103 ๐Ÿ‘ˆ counter = count + 1
console.log(counter); // 103
counter--; // 102 ๐Ÿ‘ˆ counter = count - 1   
counter--; // 101 ๐Ÿ‘ˆ counter = count - 1          
counter--; // 100 ๐Ÿ‘ˆ counter = count - 1        
console.log(counter); // 100

์ฆ๊ฐ์—ฐ์‚ฐ์ž๋Š” ๋ณ€์ˆ˜์™€ ์—ฐ์‚ฌ์ž์˜ ์œ„์น˜์— ๋”ฐ๋ผ์„œ ์ „์œ„ํ˜•, ํ›„์œ„ํ˜•์œผ๋กœ ๋‚˜๋‰ฉ๋‹ˆ๋‹ค.
์œ„์— counter ๋ณ€์ˆ˜์˜ ์˜ˆ์‹œ๋Š” ์ „์œ„ํ˜• ์ž…๋‹ˆ๋‹ค. ์ „์œ„ํ˜•์€ ์ฆ๊ฐ์—ฐ์‚ฌ์ž(++ ๋˜๋Š” --)๊ฐ€ ๋ณ€์ˆ˜ ์•ž์— ๋ถ™์–ด์žˆ์Šต๋‹ˆ๋‹ค.

  • ์ „์œ„ํ˜•(++๋ณ€์ˆ˜ ๋˜๋Š” --๋ณ€์ˆ˜)์€ ์ž์‹ ์˜ ๊ฐ’์„ ์ฆ๊ฐ€ ๋˜๋Š” ๊ฐ์†Œ ์‹œํ‚จ ๋’ค ๊ฐ’์„ ๋ณ€์ˆ˜์— ํ• ๋‹น
  • ํ›„์œ„ํ˜•(๋ณ€์ˆ˜++ ๋˜๋Š” ๋ณ€์ˆ˜--)์€ ๋ณ€์ˆ˜์— ๋จผ์ € ํ• ๋‹น์‹œํ‚จ ๋’ค, ์ž์‹ ์„ ์ฆ๊ฐ€ ๋˜๋Š” ๊ฐ์†Œ ์‹œํ‚ด

๋ง์ด ์–ด๋ ต์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ ์˜ˆ์‹œ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

// ์ฆ๊ฐ์—ฐ์‚ฐ์ž ์ „์œ„ํ˜•
// counter๋ฅผ ๋จผ์ € ์ฆ๊ฐ€์‹œํ‚จ ๋’ค, ์ฆ๊ฐ€๋œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜์—ฌ ํ• ๋‹นํ•จ
let counter = 10;
let a = ++counter; // ๐Ÿ‘ˆ let a = (counter = counter + 1)
console.log(`counter: ${counter}, a: ${a}`) // ๐Ÿ‘ˆ counter: 11, a: 11
// ์ฆ๊ฐ์—ฐ์‚ฌ์ž ํ›„์œ„ํ˜•
let counter = 10;
let a = counter++;  // ๐Ÿ‘ˆ let a = counter, counter = counter + 1
console.log(`counter: ${counter}, a: ${a}`) // ๐Ÿ‘ˆ counter: 11, a: 10

์ฆ๊ฐ์—ฐ์‚ฌ์ž์—์„œ ์ฃผ๋กœ ๋ณด๋Š” ํ˜•ํƒœ๋Š” ์ „์œ„ํ˜•์ž…๋‹ˆ๋‹ค. ์ „์œ„ํ˜•์€ ์ž์‹ ์„ ๋จผ์ € ๊ณ„์‚ฐ ํ•œ๋‹ค์Œ์— ๊ฐ’์„ ํ• ๋‹น์‹œํ‚ต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํ›„์œ„ํ˜•์€ ๊ทธ ๋ฐ˜๋Œ€๋กœ ์ƒ๊ฐํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

// ์ „์œ„ํ˜• vs ํ›„์œ„ํ˜• ์˜ˆ์ œ
let counter = 2; // ์ดˆ๊ธฐ counter 2๋กœ ์„ ์–ธ
const preIncrement = ++counter;
console.log(`preIncrement: ${preIncrement}, counter: ${counter}`); // preIncrement: 3, counter: 3
const postIncrement = counter++;
console.log(`postIncrement: ${postIncrement}, counter: ${counter}`); // postIncrement: 3, counter: 4
const preDecrement = --counter;
console<.log(`preDecrement: ${preDecrement}, counter: ${counter}`); // preDecrement: 3, counter: 3
const postDecrement = counter--;
console.log(`postDecrement: ${postDecrement}, counter: ${counter}`); // postDecrement: 3, counter: 2

๐Ÿค” ํ• ๋‹น์—ฐ์‚ฐ์ž๋ž€?

ํ• ๋‹น์—ฐ์‚ฌ์ž๋Š” ๊ทธ๋™์•ˆ ๊ณ„์† ์‚ฌ์šฉํ•œ "=" ์ž…๋‹ˆ๋‹ค. ํ• ๋‹นํ•  ๋•Œ ์“ฐ์ด๋Š” ์—ฐ์‚ฐ์ž์˜ ์ผ์ข…์ด์ฃ . ์ถ•์•ฝํ˜•์— ๋Œ€ํ•ด ์•„๋ž˜ ์ •๋ฆฌํ•ด๋ณด์•˜์–ด์š”.
"="์„ ๊ธฐ์ค€์œผ๋กœ ์™ผ์ชฝ์— ์žˆ๋Š” ํ‘œํ˜„์‹์—์„œ ๋จผ์ € ๊ณ„์‚ฐ์ด ๋จผ์ € ์ด๋ค„์ง€๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋ฅผ "="์„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฅธ์ชฝ์— ์žˆ๋Š” ๋ณ€์ˆ˜์— ํ• ๋‹น์‹œํ‚ต๋‹ˆ๋‹ค.

let x = 3;
let y = 6;
x += y; // ๐Ÿ‘ˆ x = x + y
console.log(x,y) // x=9, y=6
x -= y; //  ๐Ÿ‘ˆ  x = x - y
console.log(x,y) // x=3, y=6
x *= y; // ๐Ÿ‘ˆ  x = x * y
console.log(x,y) // x=18, y=6
x /= y; //  ๐Ÿ‘ˆ  x = x / y
console.log(x,y) // x=3, y=6

์—ฐ์‚ฐ์ž์˜ ์šฐ์„ ์ˆœ์œ„๋Š” ๋น„๊ต์  ๋‚ฎ์€ ํŽธ์ž…๋‹ˆ๋‹ค.

let n = 2;
n *= 3 + 5; // ๐Ÿ‘ˆ ํ• ๋‹น์—ฐ์‚ฐ์ž๊ฐ€ ์šฐ์„ ์ˆœ์œ„ ๋‚ฎ์•„ ์˜ค๋ฅธ์ชฝ ์—ฐ์‚ฐ(3+5)๋ถ€ํ„ฐ ์—ฐ์‚ฐ๋ฉ๋‹ˆ๋‹ค. ์ดํ›„ ํ• ๋‹น์—ฐ์‚ฐ์ž๊ฐ€ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค.
console.log(n); // 16

๐Ÿค” ๋น„๊ต์—ฐ์‚ฐ์ž๋ž€?

  • ํฌ๊ณ  ์ž‘์€ ๊ด€๊ณ„๋ฅผ ๋น„๊ตํ•˜๋Š” ์—ฐ์‚ฐ์ž๋ฅผ ๋น„๊ต์—ฐ์‚ฐ์ž๋ผํ•˜๊ณ , ์—ฐ์‚ฐ ๊ฒฐ๊ณผ๋ฅผ Boolean ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
console.log(10 < 6); // false
console.log(10 <= 6); // false
console.log(10 > 6); // true
console.log(10 >= 6); // ture

๐Ÿค” ๋…ผ๋ฆฌ์—ฐ์‚ฐ์ž๋ž€?

๋…ผ๋ฆฌ์—ฐ์‚ฐ์ž๋Š” Truthy ๋˜๋Š” Falstyํ•œ ๊ฐ’ ๋˜๋Š” Boolean ๊ฐ’์„ ๋น„๊ตํ•˜๋Š”๋ฐ ์‚ฌ์šฉํ•˜๊ณ , ์—ฐ์‚ฐ ๊ฒฐ๊ณผ๋ฅผ Boolean ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
๋…ผ๋ฆฌ์—ฐ์‚ฌ์ž์˜ ์ข…๋ฅ˜๋Š” OR์—ฐ์‚ฐ์ž(||), AND์—ฐ์‚ฐ์ž(&&), NOT์—ฐ์‚ฐ์ž(!)๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

OR์—ฐ์‚ฐ์ž(||)๋Š” ์™ผ์ชฝ๋ถ€ํ„ฐ true๊ฐ’์„ ์ฐพ์•„๋‚˜๊ฐ€๊ณ , true๊ฐ€ ์—†์œผ๋ฉด false๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

console.log(true || true); // ๐Ÿ‘ˆ true
console.log(true || false); // ๐Ÿ‘ˆ true
console.log(false || true); // ๐Ÿ‘ˆ true
console.log(false || false); // ๐Ÿ‘ˆ false
console.log(false || "" || 700 || 300); // ๐Ÿ‘ˆ 700

AND์—ฐ์‚ฐ์ž(&&)๋Š” ์™ผ์ชฝ๋ถ€ํ„ฐ false๊ฐ’์„ ์ฐพ๊ณ , false๊ฐ€ ์—†์œผ๋ฉด true๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

console.log(true && true); // ๐Ÿ‘ˆ true
console.log(true && false); // ๐Ÿ‘ˆ false
console.log(false && true); // ๐Ÿ‘ˆ false
console.log(false && false); // ๐Ÿ‘ˆ false
console.log(true && "jaewon" && 0 && 300 && -1); // ๐Ÿ‘ˆ 0

๐Ÿค” ๋™๋“ฑ์—ฐ์‚ฐ์ž๋ž€?

๋™๋“ฑํ•œ์ง€ ์•„๋‹Œ์ง€๋ฅผ ๋น„๊ตํ•˜๋Š” ๊ฒƒ์„ ๋™๋“ฑ์—ฐ์‚ฌ์ž๋ผ ํ•˜๋Š”๋ฐ, ๋™๋“ฑํ•˜๋ฉด true, ๋™๋“ฑํ•˜์ง€ ์•Š์œผ๋ฉด false๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
๋˜ํ•œ JavaScript์—์„œ๋Š” ๋น„๊ตํ•˜๋ ค๋Š” ํ”ผ์—ฐ์‚ฐ์ž๊ฐ€ ๋‹ค๋ฅธ ํƒ€์ž…์ผ ๋•Œ๋Š” ํ˜•๋ณ€ํ™˜ํ•˜์—ฌ ๋น„๊ตํ•ฉ๋‹ˆ๋‹ค.
์ด์— ๋™๋“ฑ์—ฐ์‚ฐ์ž('==', equality operator)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ 0๊ณผ false๋ฅผ ๋น„๊ตํ•˜๋ฉด true์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
์ด๋ฅผ ๋” ์ •ํ™•ํ•˜๊ณ  ์—„๊ฒฉํ•˜๊ฒŒ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ผ์น˜์—ฐ์‚ฐ์ž('===', strict equality operator)๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
์ผ์น˜์—ฐ์‚ฐ์ž๋Š” ๋™๋“ฑ์—ฐ์‚ฐ์ž์™€ ๊ฐ™์€ ๋น„๊ต ๊ธฐ๋Šฅ์— ๋”ํ•ด ๋ฐ์ดํ„ฐ ํƒ€์ž…๊นŒ์ง€ ๊ฐ™์€์ง€ ํ™•์ธํ•˜์—ฌ true, false๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
๋ฐ์ดํ„ฐ ํƒ€์ž…๊นŒ์ง€ ๋น„๊ตํ•œ๋‹ค๋Š” ๊ฒƒ์€ ํ˜•๋ณ€ํ™˜์„ํ•˜์ง€ ์•Š๊ณ  ๊ทธ ์ž์ฒด๋กœ ๊ฐ‘๊ณผ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ๋น„๊ตํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค.
๋™๋“ฑ์—ฐ์‚ฐ์ž์™€ ์ผ์น˜์—ฐ์‚ฐ์ž์˜ ๋ฐ˜๋Œ€๋กœ ๋น„๋™๋“ฑ์—ฐ์‚ฐ์ž('!==')์™€ ๋น„์ผ์น˜์—ฐ์‚ฐ์ž('!===')๋ผ๋Š” ๊ฒƒ๋„ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

console.log(10 == '10'); // ๐Ÿ‘ˆ ture
console.log(10 === '10'); // ๐Ÿ‘ˆ false
console.log(10 === 10); // ๐Ÿ‘ˆ ture
console.log(0 == false); // ๐Ÿ‘ˆ true
console.log('' == false); // ๐Ÿ‘ˆ true
console.log( 0 === false ); // ๐Ÿ‘ˆ false


2. 0, null, undefined ๋น„๊ต

'null'๊ณผ 'undefined'๋Š” ๋ชจ๋‘ '๊ฐ’์ด ์—†์Œ'์˜ ์˜๋ฏธ ๊ฐ€์ง€๊ณ , 'null'์€ Null ํƒ€์ž…์˜ ์œ ์ผํ•œ ๊ฐ’์ด๊ณ , 'undefined' ์—ญ์‹œ Undefined ํƒ€์ž…์˜ ์œ ์ผํ•œ ๊ฐ’์ž…๋‹ˆ๋‹ค.
๋‹จ, 'null'์€ ์˜๋„์ ์œผ๋กœ ๊ฐ’์ด ์—†์Œ์„ ๋‚˜ํƒ€๋‚ผ ๋•Œ ์‚ฌ์šฉํ•˜์ง€๋งŒ, 'undefined'์€ ๋ณ€์ˆ˜๊ฐ’์ด ํ• ๋‹น ์ž์ฒด๊ฐ€ ๋˜์ง€ ์•Š์•˜๋‹ค๋Š”๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

let testNull = null
console.log(testNull) // ๐Ÿ‘ˆ null
let testUndefined;
console.log(testUndefined) // ๐Ÿ‘ˆ undefined
console.log(typeof null) // object ๐Ÿ‘ˆ object๋ผ ๋‚˜์˜ค์ง€๋งŒ Null ํƒ€์ž…์ž…๋‹ˆ๋‹ค.
console.log(typeof undefined) // ๐Ÿ‘ˆ undefined

๋™๋“ฑ์—ฐ์‚ฐ์ž์—์„œ 'null'์ด ํ—ท๊ฐˆ๋ฆฌ๋Š” ์ด์œ ๋Š” ๋™๋“ฑ์—ฐ์‚ฐ์ž์—์„œ 'null'์„ ๋งŒ๋‚˜๋ฉด ๋น„๊ตํ•  ๋•Œ, ํ˜•๋ณ€ํ™˜์ด ์ด๋ค„์ง€์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— 'null'์„ ๋งŒ๋‚˜๋ฉด ๋Œ€๋ถ€๋ถ„ false๊ฐ€ ๋‚˜์˜ฌ ์ˆ˜ ๋ฐ–์— ์—†๊ฒ ์ฃ .
๋‹ค๋งŒ, ์˜ˆ์™ธ์ ์œผ๋กœ ๋™๋“ฑ์—ฐ์‚ฐ์ž ๋น„๊ต ์‹œ, "null"์ด "undefined"์„ ๋งŒ๋‚ฌ์„ ๋•Œ๋Š” ์„œ๋กœ๋ฅผ ํŠน๋ณ„ํ•œ ์ปคํ”Œ๋กœ ์ทจ๊ธ‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์— true๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ค๋‹ˆ๋‹ค.
๋‹จ, ์ผ์น˜์—ฐ์‚ฐ์ž๋กœ ๋น„๊ต๋  ๋•Œ๋Š” ์—ญ์‹œ ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— flase๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

// null vs undefined
console.log( null == undefined ); // ๐Ÿ‘ˆ true 
console.log( null === undefined ); // ๐Ÿ‘ˆ false

๋น„๊ต์—ฐ์‚ฐ์ž์—์„œ๋Š” 'null'๋„ ์ˆซ์ž๋กœ ํ˜• ๋ณ€ํ™˜์„ ์‹œ์ผœ์ค๋‹ˆ๋‹ค. 'null'์ด 0๋ณด๋‹ค ํฐ์ง€ ๋น„๊ตํ•˜๋ฉด false๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ฒ ์ฃ .
๋‹จ, ๋™๋“ฑ์—ฐ์‚ฐ์ž์— 'null'์ด ์กด์žฌํ•˜๋ฉด ์—ญ์‹œ ํ˜•๋ณ€ํ™˜์ด ์ด๋ค„์ง€์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— false์ž…๋‹ˆ๋‹ค.

// null vs 0
console.log( null > 0 );  // ๐Ÿ‘ˆ false -> null์ด ์ˆซ์žํ˜• 0์œผ๋กœ ๋ฐ˜ํ™˜๋˜๊ธฐ ๋•Œ๋ฌธ์— ํฌ์ง€ ์•Š์Œ
console.log( null == 0 ); // ๐Ÿ‘ˆ false -> ๋™๋“ฑ์—ฐ์‚ฐ์ž์—์„œ null์„ ๋งŒ๋‚˜๋•Œ๋Š” ํ˜•๋ณ€ํ™˜ ์•ˆ์ด๋ค„์ง(์˜ˆ์™ธ)
console.log( null >= 0 ); // ๐Ÿ‘ˆ true -> ๋น„๊ต์—ฐ์‚ฐ์ž์—์„œ๋Š” null์ด ์ˆซ์žํ˜• 0์œผ๋กœ ๋ฐ˜ํ™˜๋œ๊ธฐ ๋•Œ๋ฌธ์— true

'undefined'๋Š” ๋น„๊ต์—ฐ์‚ฐ์ž๋กœ ๋น„๊ตํ•  ๊ฒฝ์šฐ, Nan์œผ๋กœ ํ˜•๋ณ€ํ™˜์ด ๋˜์–ด ๋น„๊ต๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ˆซ์ž์™€ ๋น„๊ตํ•˜๋ฉด Nan์€ ์ˆซ์ž๊ฐ€ ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— false๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

// undefined vs 0
alert( undefined > 0 ); // ๐Ÿ‘ˆ false -> undefined ์ˆซ์žํ˜•์œผ๋กœ ํ˜•๋ณ€ํ™˜๋˜๋ฉด NaN์ด๊ธฐ ๋•Œ๋ฌธ
alert( undefined < 0 ); // ๐Ÿ‘ˆ false -> undefined ์ˆซ์žํ˜•์œผ๋กœ ํ˜•๋ณ€ํ™˜๋˜๋ฉด NaN์ด๊ธฐ ๋•Œ๋ฌธ
alert( undefined == 0 ); // ๐Ÿ‘ˆ false  -> ํ˜•๋ณ€ํ™˜ ๋˜์ง€ ์•Š์Œ. ์ฆ‰, undefined๋Š” null ๋˜๋Š” undefined๋ž‘๋งŒ ๊ฐ™์Œ
profile
Keep Going, Keep Coding!

0๊ฐœ์˜ ๋Œ“๊ธ€