๐ŸŒฟ Primitive types

2019. 04. 06

๊ถŒ๊ธฐ์›…

Contents

  1. ๊ฐ’
  2. ์—ฐ์‚ฐ์ž
  3. Boolean
  4. ์ˆซ์ž
  5. ๋ฌธ์ž์—ด

๐ŸŒˆ Value [๊ฐ’]

๐ŸŒˆ Value [๊ฐ’]

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ฐ’์€ ์—ฌ์„ฏ๊ฐ€์ง€ ๊ธฐ๋ณธ ์ž๋ฃŒํ˜•๊ณผ Object๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค

String, Number, Boolean, null, undefined, Symbol

๐ŸŒˆ Value [๊ฐ’]

({String, Number, Boolean, null, undefined, Symbol})^\complement = ({Object})

๐ŸŒˆ Value [๊ฐ’]

๊ฐ์ฒด๋Š” ๊ณ ์œ ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์ž์‹ ๊ณผ์˜ ๋น„๊ต์—์„œ๋งŒ ์ผ์น˜ ํ•ฉ๋‹ˆ๋‹ค

๐ŸŒˆ Value [๊ฐ’]

๋ฐ˜๋ฉด, ์›์‹œ๊ฐ’์€ ๊ฐ’์ด ๊ฐ™์œผ๋ฉด ๊ฐ™์€ ๊ฒƒ์œผ๋กœ ํ‰๊ฐ€ํ•ฉ๋‹ˆ๋‹ค

๐ŸŒˆ Value [๊ฐ’]

์›์‹œ๊ฐ’์€ ๊ฐ’์„ ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย  ย ํ•˜๋ฉฐ, ํ•ญ์ƒ ๋ถˆ๋ณ€์ž…๋‹ˆ๋‹ค

"๊ฐ’์œผ๋กœ ๋น„๊ต"

๐ŸŒˆ Value [๊ฐ’]

"์ •๋ณด๊ฐ€ ์—†์Œ"์„ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ’

undefined, null

๐ŸŒˆ Value [๊ฐ’]

๊ฐ€ ์ž์—ฐ ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ์šฐ

undefined, null

๐ŸŒˆ Value [๊ฐ’]

์˜ ์—ญ์‚ฌ

undefined, null

์›์‹œ ๊ฐ’ / ๊ฐ์ฒด ๊ฐ’ ์œผ๋กœ ๋ถ„๋ฆฌํ•˜๋Š” Java ์˜ ์ ‘๊ทผ๋ฒ•์„ ์ฐจ์šฉ ํ–ˆ์Šต๋‹ˆ๋‹ค

๊ทธ๋ฆฌ๊ณ  "๊ฐ์ฒด ์•„๋‹˜" ์„ ํ‘œํ˜„ํ•˜๋Š” ๊ฐ’์ธ null ๋˜ํ•œ ๋„์ž… ํ–ˆ์Šต๋‹ˆ๋‹ค

ํ•˜์ง€๋งŒ, C ์–ธ์–ด ๊ณ„ํ†ต์˜ JavaScript ๋Š” null ์„ ์ˆซ์ž๋กœ ๊ฐ•์ œํ•˜๋ฉด 0์ด ๋ฉ๋‹ˆ๋‹ค

๐ŸŒˆ Value [๊ฐ’]

์˜ ์—ญ์‚ฌ

undefined, null

undefined ๋Š” null ์ดํ›„์— ์ถ”๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค

ย 

null ๋กœ "์ดˆ๊ธฐํ™” ๋˜์ง€ ์•Š์€ ๋ณ€์ˆ˜" ๋˜๋Š” "์กด์žฌํ•˜์ง€ ์•Š๋Š” ํ”„๋กœํผํ‹ฐ" ์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๋ฅผ ํ–ˆ์ง€๋งŒ

null ์€ ํ•˜๋‚˜์˜ ๊ฐ’์ด๊ณ , null ์ด๋ผ๋Š” ๊ฐ’์€ NULL Pointer ์˜ reference ์ž…๋‹ˆ๋‹ค

๋”ฐ๋ผ์„œ, ๋ธŒ๋žœ๋˜ ์•„์ดํฌ๋Š” ์•„๋ž˜์˜ ๊ฒฝ์šฐ๋ฅผ ํ”ผํ•˜๊ณ  ์‹ถ์—ˆ์Šต๋‹ˆ๋‹ค

ย 

  • null ์€ ๊ฐ์ฒด ์ด์ƒ์˜ ์˜๋ฏธ๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ, null ์„ ์ฐธ์กฐํ•˜๋Š” ๋“ฏํ•œ ๋‰˜์•™์Šค๋Š” ์•ˆ๋œ๋‹ค
  • Number(null) ๊ณผ ๊ฐ™์ด ๊ฐ’์ด 0์œผ๋กœ ๊ฐ•์ œ๋˜๋ฉด ์•ˆ๋œ๋‹ค

ย 

๊ทธ๋ฆฌํ•˜์—ฌ, "์ดˆ๊ธฐํ™” ๋˜์ง€ ์•Š์€ ๋ณ€์ˆ˜" ๋˜๋Š” "์กด์žฌํ•˜์ง€ ์•Š๋Š” ํ”„๋กœํผํ‹ฐ" ๋Š”

์ƒ์„ฑ(์ ‘๊ทผ) ์‹œ์ ์— undefined๊ฐ€ ํ• ๋‹น๋ฉ๋‹ˆ๋‹ค

๐ŸŒˆ Value [๊ฐ’]

์—ฌ๋‹ด

undefined, null

1. undefined๋Š” ECMAScript5 ์ดํ›„ ์ฝ๊ธฐ ์ „์šฉ์œผ๋กœ ์žฌ ํ• ๋‹น์ด ๋ถˆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค

(ECMAScript3 ์—์„œ๋Š” undefined ๊ฐ’์„ ๋ฎ์–ด์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค)

ย 

2. undefined๋Š” ์ „์—ญ ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ์ด๋ฉฐ, undefined ์›์‹œ ๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค

ย 

3. void 0 ๋Š” ํ•ญ์ƒ undefined ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค

ย 

4. ๋ณด๋‹ค ์ž์„ธํ•œ ๋‚ด์šฉ์€ ์•„๋ž˜ ๋งํฌ๋“ค์„ ์ฐธ๊ณ ํ•˜์„ธ์š”

ย  ย  โ†’ ECMAScript 1.3

ย  ย  โ†’ The history of "typeof null"ย 

ย  ย  โ†’ V8 Document

๐ŸŒˆ Value [๊ฐ’]

์›์‹œ ๊ฐ’์˜ ๋ž˜ํผ ๊ฐ์ฒด

๐ŸŒˆ Value [๊ฐ’]

์“ฐ์ง€๋งˆ์„ธ์š”

๐ŸŒˆ Value [๊ฐ’]

๋ž˜ํผ ๊ฐ์ฒด๋Š” ์›์‹œ ๊ฐ’๊ณผ ๋‹ค๋ฆ…๋‹ˆ๋‹ค

๐ŸŒˆ Value [๊ฐ’]

์›์‹œ ๊ฐ’์€ ๋ž˜ํผ ๊ฐ์ฒด์™€ prototype ์ฒด์ธ์„ ๊ณต์œ ํ•ฉ๋‹ˆ๋‹ค

๐ŸŒˆ Value [๊ฐ’]

๋ž˜ํผ ๊ฐ์ฒด๋ฅผ ์›์‹œ ๊ฐ’์œผ๋กœ, ์›์‹œ ๊ฐ’์„ ๋ž˜ํผ ๊ฐ์ฒด๋กœ ๋ฐ”๊พธ๊ธฐ

PrimitiveWrapper(primitiveValue)

ย 

PrimitiveWrapper.valueOf()

๐ŸŒˆ Value [๊ฐ’]

๊ทธ ์ „์—, falsy ํ•œ ๊ฐ’์„ ์•Œ์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค

undefined, null, false, 0, NaN, '' (๋นˆ ๋ฌธ์ž์—ด)

๐ŸŒˆ Value [๊ฐ’]

๊ฐ์ฒด๋ฅผ ์›์‹œ ๊ฐ’์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ

๐ŸŒˆ Value [๊ฐ’]

valueOf - ๋ž˜ํผ ๊ฐ์ฒด๋ฅผ ์›์‹œ ๊ฐ’์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ

๐ŸŒˆ Value [๊ฐ’]

falsy ๊ฐ’์„ Boolean, Number, String ์œผ๋กœ ๋ฐ”๊ฟ€ ๊ฒฝ์šฐ

๐ŸŒˆ Value [๊ฐ’]

ToPrimitiveย ์™€ ๊ทธ ๋™์ž‘ ์ดํ•ดํ•˜๊ธฐ

ToPrimitive ๋Š” ์ถ”์ƒ ์—ฐ์‚ฐ์ž(Abstract Operations) ๋กœ input ์„ non Object ์œ ํ˜•์œผ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค

signature

ToPrimitive(input, PreferredType)

๐ŸŒˆ Value [๊ฐ’]

ToPrimitiveย ์™€ ๊ทธ ๋™์ž‘ ์ดํ•ดํ•˜๊ธฐ

ToPrimitive ๋Š” ๋ชจ๋“  ํ˜• ๋ณ€ํ™˜ ๋™์ž‘์˜ ์ถ”์ƒ์ด๋ฉฐ

parseInt, .toJSON ๋“ฑ ์—ฌ๋Ÿฌ ํ˜• ๋ณ€ํ™˜ ๋™์ž‘์˜ ์กฐ์ƒ์ž…๋‹ˆ๋‹ค

๐Ÿถ ๊ด€๋ จ ๋ฌธ์„œ ๊ฐ™์ด ๋ณด๊ธฐ

ย 

1. Date.prototype.toJSON(key)

2. parseInt()

3. v8 Document > Internals

๐ŸŒˆ Value [๊ฐ’]

ํ˜• ๋ณ€ํ™˜์ด ๋  ๋•Œ๋Š” ์•„๋ž˜ ์ˆœ์„œ๋Œ€๋กœ...

1. input ์ด ์›์‹œ ๊ฐ’์ด๋ฉด ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค

2. input ์ด ๊ฐ์ฒด๋ผ๋ฉด, input.valueOf ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์›์‹œ๊ฐ’์ด๋ฉด ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค

3. valueOf ์˜ ๊ฐ’์ด ์›์‹œ๊ฐ’์ด ์•„๋‹ˆ๋ผ๋ฉด, toString ์„ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. ์›์‹œ๊ฐ’์ด๋ฉด ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค

4. 2-3๋ฒˆ ๊ณผ์ •์—์„œ return ์ด ์ผ์–ด๋‚˜์ง€ ์•Š์œผ๋ฉด, TypeError๋ฅผ throw ํ•ฉ๋‹ˆ๋‹ค

1. input ์ด ์›์‹œ ๊ฐ’์ด๋ฉด ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค

2. input ์ด ๊ฐ์ฒด๋ผ๋ฉด, input.valueOf ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์›์‹œ๊ฐ’์ด๋ฉด ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค

3. valueOf ์˜ ๊ฐ’์ด ์›์‹œ๊ฐ’์ด ์•„๋‹ˆ๋ผ๋ฉด, toString ์„ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. ์›์‹œ๊ฐ’์ด๋ฉด ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค

4. 2-3๋ฒˆ ๊ณผ์ •์—์„œ return ์ด ์ผ์–ด๋‚˜์ง€ ์•Š์œผ๋ฉด, TypeError๋ฅผ throw ํ•ฉ๋‹ˆ๋‹ค

๐ŸŽ OPERATOR [์—ฐ์‚ฐ์ž]

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋ชจ๋“  ์—ฐ์‚ฐ์ž๋Š” ํ”ผ์—ฐ์‚ฐ์ž๋ฅผ ์ ์ ˆํ•œ ํƒ€์ž…์œผ๋กœ ๊ฐ•์ œํ•ฉ๋‹ˆ๋‹ค

Operand

Primitive Value

๐ŸŽ OPERATOR [์—ฐ์‚ฐ์ž]

์™œ๋ƒ๋ฉด, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ์—ฐ์‚ฐ์ž๋Š” ๋Œ€๋ถ€๋ถ„ ์›์‹œ๊ฐ’์œผ๋กœ ์กฐ์ž‘๋˜๊ฑฐ๋“ ์š”

Primitive Value

๐ŸŽ OPERATOR [์—ฐ์‚ฐ์ž]

๐Ÿถ TL; DR

ย 

1. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ์—ฐ์‚ฐ์ž๋Š” ํ”ผ ์—ฐ์‚ฐ์ž์˜ ๊ฐ’์„ ์›์‹œ๊ฐ’์œผ๋กœ ๊ฐ€์ •ํ•œ๋‹ค

2. ์›์‹œ๊ฐ’์ด ์•„๋‹ ๊ฒฝ์šฐ, ์›์‹œ๊ฐ’์œผ๋กœ ๋ฐ”๊พผ๋‹ค

3. ์›์‹œ๊ฐ’์˜ ํƒ€์ž…์„ ๋งž์ถ˜๋‹ค!

๐ŸŽ OPERATOR [์—ฐ์‚ฐ์ž]

๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค

๐ŸŽ OPERATOR [์—ฐ์‚ฐ์ž]

์ด๋ ‡๊ฒŒ ๋๋‚ด๊ณ  ์‹ถ์ง€๋งŒ...

์˜ค๋Š˜์€ ์•ผํฌํ„ธ ๊นŽ๊ธฐ์˜ ์ง„์ˆ˜

๐ŸŽ OPERATOR [์—ฐ์‚ฐ์ž]

์—ฐ์‚ฐ์ž๋กœ ๊ฐ์ฒด๋„ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๋Š” ์–ธ์–ด๋„ ์žˆ์Šต๋‹ˆ๋‹ค

PHP

C++

๐ŸŽ OPERATOR [์—ฐ์‚ฐ์ž]

์•„๋ฌดํŠผ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ์•ˆ๋ฉ๋‹ˆ๋‹ค. ํ•ด ๋ณผ๊นŒ์š”?

๐ŸŽ OPERATOR [์—ฐ์‚ฐ์ž]

์—ฐ์‚ฐ์ž์˜ ์ข…๋ฅ˜๋Š” ์ด๋ ‡๊ฒŒ ๋งŽ์Šต๋‹ˆ๋‹ค ๐Ÿค”

๋Œ€์ž… ์—ฐ์‚ฐ์ž, ๋น„๊ต ์—ฐ์‚ฐ์ž, ์‚ฐ์ˆ  ์—ฐ์‚ฐ์ž, ๋น„ํŠธ๋‹จ์œ„ ์—ฐ์‚ฐ์ž, ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž, ๋ฌธ์ž์—ด ์—ฐ์‚ฐ์ž, ์กฐ๊ฑด (์‚ผํ•ญ) ์—ฐ์‚ฐ์ž, ์ฝค๋งˆ ์—ฐ์‚ฐ์ž, ๋‹จํ•ญ ์—ฐ์‚ฐ์ž, ๊ด€๊ณ„ ์—ฐ์‚ฐ์ž

๐ŸŽ OPERATOR [์—ฐ์‚ฐ์ž]

๐ŸŽ OPERATOR [์—ฐ์‚ฐ์ž]

์—ฐ์‚ฐ์ž์˜ ์ข…๋ฅ˜๋Š” ์ด๋ ‡๊ฒŒ ๋งŽ์Šต๋‹ˆ๋‹ค ๐Ÿค”

๋Œ€์ž… ์—ฐ์‚ฐ์ž, ๋น„๊ต ์—ฐ์‚ฐ์ž, ์‚ฐ์ˆ  ์—ฐ์‚ฐ์ž, ๋น„ํŠธ๋‹จ์œ„ ์—ฐ์‚ฐ์ž, ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž, ๋ฌธ์ž์—ด ์—ฐ์‚ฐ์ž, ์กฐ๊ฑด (์‚ผํ•ญ) ์—ฐ์‚ฐ์ž, ์ฝค๋งˆ ์—ฐ์‚ฐ์ž, ๋‹จํ•ญ ์—ฐ์‚ฐ์ž, ๊ด€๊ณ„ ์—ฐ์‚ฐ์ž

๐ŸŽ OPERATOR [์—ฐ์‚ฐ์ž]

๋Œ€์ž… ์—ฐ์‚ฐ์ž

[ = ]

๐ŸŽ OPERATOR [์—ฐ์‚ฐ์ž]

๋Œ€์ž… ์—ฐ์‚ฐ์žย  ย [ = ]

๋Œ€์ž… ์—ฐ์‚ฐ์ž๋ฅผ ์„ค๋ช…ํ•˜๊ธฐ ์ „์—

ย 

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋ฌธ(Statement)๊ณผ ์‹(Expression) ์— ๋Œ€ํ•ด์„œ ์•Œ๊ณ  ๊ณ„์‹œ๋‚˜์š”?

๐Ÿ“– MDN Expression and operatorsย 

๐Ÿ”—ย ๋ฌธ์„œ ๋ณด๊ธฐ ย 

ย 

An expressionย is any valid unit of code that resolves to a value.

์‹์ด๋ž€ย  ๊ฐ’์œผ๋กœ ํ•ด์„(resolves)๋  ์ˆ˜ ์žˆ๋Š” ์œ ํšจํ•œ ์–ด๋–ค ์ฝ”๋“œ์˜ ๋‹จ์œ„ ์ž…๋‹ˆ๋‹ค

ย 

Every syntactically valid expression resolves to some value but conceptually, there are two types of expressions: with side effects

(for example: those that assign value to a variable) and those that in some sense evaluate and therefore resolve to a value.

ย 

๋ชจ๋“  ๊ตฌ๋ฌธ์ ์œผ๋กœ ์œ ํšจํ•œ ์‹์€ ๊ฐ’์œผ๋กœ ํ•ด์„(resolves) ๋˜๊ณ , ๊ฐœ๋…์ ์œผ๋กœ๋Š” ๋‘ ์ข…๋ฅ˜์˜ ์‹์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค:

side effects(๋ณ€์ˆ˜์— ๊ฐ’์„ ํ• ๋‹นํ•˜๋Š” ํ‘œํ˜„์‹) ์™€ ํ‰๊ฐ€ํ•˜์—ฌ ๊ฐ’์œผ๋กœ ํ•ด์„๋˜๋Š” ๊ฐ’

ย 

The expression x = 7 is an example of the first type.

This expression uses the =ย operatorย to assign the value seven to the variable x. The expression itself evaluates to seven.

ย 

x = 7 ์ด๋ผ๋Š” ์‹์€ ์ฒซ ๋ฒˆ์งธ(side effect) ์˜ˆ์‹œ ์ž…๋‹ˆ๋‹ค

์ด ์‹์€ " = " ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ 7์ด๋ผ๋Š” ๊ฐ’์„ x ๋ผ๋Š” ๋ณ€์ˆ˜์— ํ• ๋‹นํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด ์‹์€ ์ด๊ฒƒ ์ž์ฒด๋กœ 7 ๋กœ์จ ํ‰๊ฐ€๋ฉ๋‹ˆ๋‹ค

ย 

The code 3 + 4 is an example of the second expression type.

This expression uses the + operator to add three and four together without assigning the result, seven, to a variable.

ย 

3 + 4 ๋ผ๋Š” ์ฝ”๋“œ๋Š” ๋‘ ๋ฒˆ์งธ(some sense evaluate and therefore resolve to a value) ์˜ˆ์‹œ ์ž…๋‹ˆ๋‹ค

์ด ์‹์€ " + " ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ 3๊ณผ 4๋ฅผ ์„œ๋กœ ๋”ํ–ˆ์ง€๋งŒ, 7 ์ด๋ผ๋Š” ๊ฒฐ๊ณผ๋ฅผ ํ• ๋‹น ํ•˜์ง€๋Š” ์•Š์•˜์Šต๋‹ˆ๋‹ค

๐ŸŽ OPERATOR [์—ฐ์‚ฐ์ž]

๋Œ€์ž… ์—ฐ์‚ฐ์žย  ย [ = ]

์‹๊ณผ ๋ฌธ์— ๋Œ€ํ•œ ์ดํ•ด๋ฅผ ํ•˜๋„๋ก ๋„์™€์ฃผ์‹  ์ฝ”๋“œ๋Œ•๋Œ•์ด ์„ ์ƒ๋‹˜์˜ย ์ข‹์€ ๊ธ€

๐ŸŽ OPERATOR [์—ฐ์‚ฐ์ž]

๋Œ€์ž… ์—ฐ์‚ฐ์žย  ย [ = ]

์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, ์‹์œผ๋กœ ํ‰๊ฐ€๋œ๋‹ค๊ณ  ํ•˜๋‹ˆ

๋Œ€์ž… ์—ฐ์‚ฐ์ž ๋˜ํ•œ ํ•˜๋‚˜์˜ ์‹์ด์ž ๊ฐ’ย ์ž…๋‹ˆ๋‹ค

๐ŸŽ OPERATOR [์—ฐ์‚ฐ์ž]

๋Œ€์ž… ์—ฐ์‚ฐ์žย  ย [ = ]

ํ• ๋‹น ์ž์ฒด๋งŒ์œผ๋กœ๋Š” ํ•˜๋‚˜์˜ ๋ฌธ์ด ์•„๋‹Œ ์‹์ด๋ผ๋Š” ์˜๋ฏธ!

๐ŸŽ OPERATOR [์—ฐ์‚ฐ์ž]

๋Œ€์ž… ์—ฐ์‚ฐ์žย  ย [ = ]

let, const, var ๋ผ๋Š” ํ‚ค์›Œ๋“œ๋Š”

if, else, for ๋“ฑ๊ณผ ๊ฐ™์ด ๋ฌธ์œผ๋กœ ๋งŒ๋“ค์–ด ์ฃผ๋Š” Keyword ์ž…๋‹ˆ๋‹ค

๐ŸŽ OPERATOR [์—ฐ์‚ฐ์ž]

๋Œ€์ž… ์—ฐ์‚ฐ์žย  ย [ = ]

๋Œ€์ž… ์—ฐ์‚ฐ์ž๋Š” ์‹ (Expression) ์ด๊ธฐ ๋•Œ๋ฌธ์—

์ด๋Ÿฐ๊ฒŒ ๊ฐ€๋Šฅํ–ˆ๋˜ ๊ฒƒ์ด์ ธ ใ…‡ใ……ใ…‡

ํ˜น์‹œ ๋‹ค ์•„์…จ๋˜๊ฑด ์•„๋‹ˆ์ฃ ...?

๐ŸŽ OPERATOR [์—ฐ์‚ฐ์ž]

๋ณตํ•ฉ ๋Œ€์ž… ์—ฐ์‚ฐ์ž

[ OP= ]

๐ŸŽ OPERATOR [์—ฐ์‚ฐ์ž]

๋ณตํ•ฉ ๋Œ€์ž… ์—ฐ์‚ฐ์žย  ย [ OP= ]

๋ณตํ•ฉ ๋Œ€์ž… ์—ฐ์‚ฐ์ž๋Š” ๋Œ€์ž… ์—ฐ์‚ฐ์ž์— ๊ฐ๋ฏธ๋ฃŒ๋ฅผ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค (Syntactic Sugar)

๐ŸŽ OPERATOR [์—ฐ์‚ฐ์ž]

๋ณตํ•ฉ ๋Œ€์ž… ์—ฐ์‚ฐ์žย  ย [ OP= ]

Syntactic Sugar ๋ผ๋Š” ์˜๋ฏธ๋Š”, ๋‹จ์ˆœํ•œ ์ถ•์•ฝ ๊ตฌ๋ฌธ ์ด๋ผ๋Š” ์˜๋ฏธ ์ž…๋‹ˆ๋‹ค!

(๋ฑ€์ด ๋งˆ์Šค์ฝ”ํŠธ์ธ ์–ด๋–ค ์–ธ์–ด์ฒ˜๋Ÿผ ์ƒํ™ฉ์— ๋”ฐ๋ผ ํ• ๋‹น์ด ๋‹ค๋ฅด์ง€ ์•Š์Šต๋‹ˆ๋‹ค)

๐ŸŽ OPERATOR [์—ฐ์‚ฐ์ž]

๋ณตํ•ฉ ๋Œ€์ž… ์—ฐ์‚ฐ์žย  ย [ OP= ]

[ OP= ] ์ด๋ผ๋Š” ์˜๋ฏธ๋Š” = ์•ž์— [์‚ฐ์ˆ , ๋น„ํŠธ] ์—ฐ์‚ฐ์ž๋ฅผ ์“ธ ์ˆ˜ ์žˆ๋‹ค๋Š” ๋œป

[์‚ฐ์ˆ ] ๋ณตํ•ฉ ๋Œ€์ž… ์—ฐ์‚ฐ์ž

ย 

๋ง์…ˆ ๋Œ€์ž…
โ†’ (x += y) == (x = x + y)
๋บ„์…ˆ ๋Œ€์ž…
โ†’ (x -= y) == (x = x - y)
๊ณฑ์…ˆ ๋Œ€์ž…
โ†’ (x *= y) == (x = x * y)
๋‚˜๋ˆ—์…ˆ ๋Œ€์ž…
โ†’ (x /= y) == (x = x / y)
๋‚˜๋จธ์ง€ ์—ฐ์‚ฐ ๋Œ€์ž…
โ†’ (x %= y) == (x = x % y)

[๋น„ํŠธ] ๋ณตํ•ฉ ๋Œ€์ž… ์—ฐ์‚ฐ์ž

ย 

์™ผ์ชฝ ์ด๋™ ์—ฐ์‚ฐ ๋Œ€์ž…
โ†’ x <<(x <<= y) == (x = x << y)
์˜ค๋ฅธ์ชฝ ์ด๋™ ์—ฐ์‚ฐ ๋Œ€์ž…
โ†’ (x >>= y) == (x = x >> y)
๋ถ€ํ˜ธ ์—†๋Š” ์˜ค๋ฅธ์ชฝ ์ด๋™ ์—ฐ์‚ฐ ๋Œ€์ž…
โ†’ (x >>>= y) == (x = x >>> y)
๋น„ํŠธ ๋‹จ์œ„ ๋…ผ๋ฆฌ๊ณฑ ์—ฐ์‚ฐ ๋Œ€์ž…
โ†’ (x &= y) == (x = x & y)
๋น„ํŠธ ๋‹จ์œ„ ๋ฐฐํƒ€์  ๋…ผ๋ฆฌํ•ฉ ์—ฐ์‚ฐ ๋Œ€์ž…
โ†’ (x ^= y) == (x = x ^ y)
๋น„ํŠธ ๋‹จ์œ„ ๋…ผ๋ฆฌํ•ฉ ์—ฐ์‚ฐ ๋Œ€์ž…
โ†’ (x |= y) == (x = x | y)

๐ŸŽ OPERATOR [์—ฐ์‚ฐ์ž]

๋ณตํ•ฉ ๋Œ€์ž… ์—ฐ์‚ฐ์žย  ย [ OP= ]

๐ŸŽ OPERATOR [์—ฐ์‚ฐ์ž]

๋™๋“ฑ ๋น„๊ต ์—ฐ์‚ฐ์ž

[ ==, !=, ===, !== ]

๐ŸŽ OPERATOR [์—ฐ์‚ฐ์ž]

๋™๋“ฑ ๋น„๊ต ์—ฐ์‚ฐ์žย  ย [ ==, !=, ===, !== ]

์„ค๋ช…์ด ๊ธธ์–ด์งˆ ๊ฒƒ ๊ฐ™์•„์„œ...

3์ค„ ์š”์•ฝ โœ…

ย 

1. == ์—ฐ์‚ฐ์ž์™€ === ์—ฐ์‚ฐ์ž์˜ ๋™์ž‘ ๋ฐฉ์‹์˜ ๊ฐ€์žฅ ํฐ ์ฐจ์ด์ ์€ ํƒ€์ž…์„ ๋ณ€ํ™˜ํ•˜๋Š”๊ฐ€ ์ž…๋‹ˆ๋‹ค

2. === ์—ฐ์‚ฐ์ž๋Š” ๊ฐ’๊ณผ ํƒ€์ž…๊นŒ์ง€ ๊ฐ™์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค

3. == ์—ฐ์‚ฐ์ž๋Š” ๊ฐ’์˜ ํƒ€์ž…์ด ๋‹ค๋ฅด๋ฉด ๋งž์ถ˜ ํ›„ ๋น„๊ตํ•ฉ๋‹ˆ๋‹ค

๐ŸŽ OPERATOR [์—ฐ์‚ฐ์ž]

๋™๋“ฑ ๋น„๊ต ์—ฐ์‚ฐ์žย  ย [ ==, !=, ===, !== ]

3์ค„๋„ ๊ธธ๋‹ค๋ฉด 1์ค„ ์š”์•ฝ

==, != ๋Š” ์“ฐ์ง€ ๋งˆ์„ธ์š” (์ง„์ง€ํ•จ)

๐ŸŽ OPERATOR [์—ฐ์‚ฐ์ž]

๋™๋“ฑ ๋น„๊ต ์—ฐ์‚ฐ์žย  ย [ ==, !=, ===, !== ]

๋งค๋ฒˆ ์ฑ…์—์„œ ์“ฐ์ง€ ๋ง๋ผ๊ณ  ํ•˜๋Š”๋ฐ ์™œ ๊ทธ๋Ÿฐ์ง€ ๋ด…์‹œ๋‹ค

๐ŸŽ OPERATOR [์—ฐ์‚ฐ์ž]

๋™๋“ฑ ๋น„๊ต ์—ฐ์‚ฐ์žย  ย [ ==, !=, ===, !== ]

== ์—ฐ์‚ฐ์ž์™€ === ์—ฐ์‚ฐ์ž๊ฐ€ ์–ด๋–ป๊ฒŒ ๋™์ž‘ํ•˜๋Š”์ง€

๋ญ๊ฐ€ ์–ผ๋งˆ๋‚˜ ๋‹ฌ๋ผ์„œ ์“ฐ์ง€ ๋ง๋ผ๋Š” ๊ฑด์ง€ ๋„ˆ๋ฌด ๊ถ๊ธˆํ•ด ์กŒ์Šต๋‹ˆ๋‹ค

ย 

๊ทธ๋ž˜์„œ ์˜ค๋žœ๋งŒ์— ์—„์ฒญ๋‚œ ์•ผํฌํ„ธ ๊นŽ๊ธฐ ใ…‡ใ……ใ…‡

์•ผํฌํ„ธ์„ ๊นŽ...์„์ˆ˜ ์žˆ์„๊นŒ์š”?

๐ŸŽ OPERATOR [์—ฐ์‚ฐ์ž]

๋™๋“ฑ ๋น„๊ต ์—ฐ์‚ฐ์žย  ย [ ==, !=, ===, !== ]

์ œ๊ฐ€ ๊ฐ€์ ธ์˜จ ์•ผํฌํ„ธ ์„ ๋ฌผ์€ ๋‘ ๊ฐœ ์ž…๋‹ˆ๋‹ค

๐ŸŽ v8/tags/4.3.18/src/runtime.js

โ†’ V8 ์—”์ง„ ์†Œ์Šค์ฝ”๋“œ

ย 

๐ŸŽ ECMAScript Equality Operators

โ†’ ECMAScript ๋ช…์„ธ

๐ŸŽ OPERATOR [์—ฐ์‚ฐ์ž]

Abstract Equality Comparison [ == ]

The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows:

1. If Type(x) is the same as Type(y), then

ย  ย  a. Return the result of performing Strict Equality Comparison x === y.
2. If x is null and y is undefined, return true.
3. If x is undefined and y is null, return true.
4. If Type(x) is Number and Type(y) is String, return the result of the comparison x == ! ToNumber(y).
5. If Type(x) is String and Type(y) is Number, return the result of the comparison ! ToNumber(x) == y.
6. If Type(x) is Boolean, return the result of the comparison ! ToNumber(x) == y.
7. If Type(y) is Boolean, return the result of the comparison x == ! ToNumber(y).
8. If Type(x) is either String, Number, or Symbol and Type(y) is Object, return the result of the comparison x == ToPrimitive(y).
9. If Type(x) is Object and Type(y) is either String, Number, or Symbol, return the result of the comparison ToPrimitive(x) == y.
10. Return false.

๐ŸŽ OPERATOR [์—ฐ์‚ฐ์ž]

Abstract Equality Comparison [ == ]

== ์—ฐ์‚ฐ ํ•˜๋Š”๋ฐ ์ €๋ ‡๊ฒŒ ๋งŽ์€ ์ผ์„ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค ๐Ÿค”

๐ŸŽ OPERATOR [์—ฐ์‚ฐ์ž]

Strict Equality Comparison [ === ]

1. If Type(x) is different from Type(y), return false.
2. If Type(x) is Number, then
ย  ย  a. If x is NaN, return false.
ย  ย  b. If y is NaN, return false.
ย  ย  c. If x is the same Number value as y, return true.
ย  ย  d. If x is +0 and y is -0, return true.
ย  ย  e. If x is -0 and y is +0, return true.
ย  ย  f. Return false.
3. Return SameValueNonNumber(x, y).

Number ๋น„๊ต๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ์—๋Š” ์ด ํ•จ์ˆ˜๋ฅผ ์“ฐ๋”๋ผ๊ตฌ์š”! ๋Œ€๋ฐ• ์‹ ๊ธฐ์“ฐ

1. x, y ๋Š” ๊ฐ™์€ ํƒ€์ž…์ด๋ผ๊ณ  ๊ฐ€์ •ํ•œ๋‹ค

2. null, undefined, Symbol, String, Boolean, Object ๊ฐ’ ๋น„๊ต๋ฅผ ํ•˜์—ฌ true || false ๋ฅผ ๋ฐ˜ํ™˜

๐ŸŽ OPERATOR [์—ฐ์‚ฐ์ž]

Strict Equality Comparison [ === ]

โ†’ return ๊นŒ์ง€ min(1) ~ max(3) ๋‹จ๊ณ„

ย 

Abstract Equality Comparisonย [ === ]

โ†’ return ๊นŒ์ง€ min(1) ~ max(10) ๋‹จ๊ณ„

์‹ฌ์ง€์–ด ๋‚ด๋ถ€์ ์œผ๋กœ Strict Equality Comparison ํ˜ธ์ถœ

๐ŸŽ OPERATOR [์—ฐ์‚ฐ์ž]

์ •๋ง ์ด๋ ‡๊ฒŒ ๋ณต์žกํ•œ์ง€ ์†Œ์Šค์ฝ”๋“œ ํ™•์ธ ๐Ÿ‘€

๐ŸŽ OPERATOR [์—ฐ์‚ฐ์ž]

Equality Comparison [ ==, === ]

๋งŒ์•ฝ == ์—ฐ์‚ฐ์ž๋ฅผ ์“ฐ๊ณ  ์‹ถ์œผ์‹œ๋ฉด, ๋ช‡ ๊ฐ€์ง€ ์˜ˆ์™ธ ์ผ€์ด์Šค๋ฅผ ์™ธ์šฐ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค!

ย 

1. undefined == null

2. string - number ๊ฐ„ ๋น„๊ต๋Š” ๋ฌธ์ž์—ด์„ ์ˆซ์ž๋กœ ๋ฐ”๊ฟ”์„œ ๋น„๊ต

3. bool - number (or string) ๋น„๊ต๋Š” bool ์„ ์ˆซ์ž๋กœ ๋ฐ”๊พผ ๋’ค ๋น„๊ต

ย  1. bool ๊ณผ string ๋น„๊ต๋Š” ๋‘˜ ๋‹ค ์ˆซ์ž๋กœ ๋ณ€ํ˜•ํ•˜์ง€๋งŒ

ย  2. ๋น„์–ด ์žˆ์ง€ ์•Š์€ ๋ฌธ์ž์—ด์ด ๋ชจ๋‘ true ๋Š” ์•„๋‹ˆ๋‹ค ('0' == true)

4. object - number (or string) ๋น„๊ต๋Š” ๊ฐ์ฒด๋ฅผ ์›์‹œ ๊ฐ’์œผ๋กœ ๋ฐ”๊พผ ๋’ค ๋น„๊ต

5. ๊ทธ ์™ธ๋Š” ๋ชจ๋‘ false

๐ŸŽ OPERATOR [์—ฐ์‚ฐ์ž]

์•„! ์ด๋Ÿฐ๊ฒƒ๋„ ์™ธ์›Œ ๋‘์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค ํžˆํžˆ ํ˜ธํ˜ธ

์ด๊ฑธ ์™ธ์šฐ๋Š๋‹ˆ ๊ทธ๋ƒฅ === ์„ ์”์‹œ๋‹ค...

๐ŸŽ OPERATOR [์—ฐ์‚ฐ์ž]

Equality Comparison [ ==, === ]

๋ญ”๊ฐ€ ์œ ์šฉํ•ด ๋ณด์ด๋Š” ํ•จ์ •๋“ค์— ์†์ง€ ๋งˆ์„ธ์š”!

๐ŸŽ OPERATOR [์—ฐ์‚ฐ์ž]

๋น„๊ต ์—ฐ์‚ฐ์ž

[ >, <, >=, <= ]

๐ŸŽ OPERATOR [์—ฐ์‚ฐ์ž]

๋น„๊ต ์—ฐ์‚ฐ์žย [ >, <, >=, <= ]

๋น„๊ต ์—ฐ์‚ฐ์ž๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ˆœ์„œ๋ฅผ ๊ฑฐ์นฉ๋‹ˆ๋‹ค

ย 

1. ํ”ผ ์—ฐ์‚ฐ์ž ๋ชจ๋‘๋ฅผ ์›์‹œ ๊ฐ’์œผ๋กœ ๋ฐ”๊ฟ‰๋‹ˆ๋‹ค (ToPrimitive)

2. ๋‘ ํ”ผ ์—ฐ์‚ฐ์ž๊ฐ€ ๋ชจ๋‘ ๋ฌธ์ž์—ด์ด๋ฉด ํ•ด๋‹น ๋ฌธ์ž์—ด์˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฌธ์ž์ธ 16๋น„ํŠธ ์ฝ”๋“œ ๋‹จ์œ„๋ฅผ ์‚ฌ์ „ ์ˆœ์„œ๋กœ ๋น„๊ตํ•ฉ๋‹ˆ๋‹ค

3. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ๋‘ ํ”ผ์—ฐ์‚ฐ์ž๋ฅผ ๋ชจ๋‘ ์ˆซ์ž๋กœ ๋ฐ”๊ฟ” ๋น„๊ตํ•ฉ๋‹ˆ๋‹ค

์†Œ์Šค ์ฝ”๋“œ ์ž ๊น ๋ณผ๊นŒ์š”?

๐ŸŽ OPERATOR [์—ฐ์‚ฐ์ž]

๋น„๊ต ์—ฐ์‚ฐ์žย [ >, <, >=, <= ]

๊ทธ๋ƒฅ ๋„˜์–ด๊ฐ€๋ฉด ์•„์‰ฌ์šฐ๋‹ˆ๊นŒ

์†Œ์Šค์ฝ”๋“œ ์ž ๊น ํ™•์ธ ๐Ÿ‘€

๐ŸŽ OPERATOR [์—ฐ์‚ฐ์ž]

์‚ฐ์ˆ  ์—ฐ์‚ฐ์ž

[ +, -, *, /, % ]

๐ŸŽ OPERATOR [์—ฐ์‚ฐ์ž]

์‚ฐ์ˆ  ์—ฐ์‚ฐ์žย [ +, -, *, /, % ]

์‚ฐ์ˆ  ์—ฐ์‚ฐ์ž์—๋„ ๋ช‡๊ฐ€์ง€ ์˜ˆ์™ธ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค (์˜ˆ์™ธ์˜ ์–ธ์–ด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๐Ÿ˜ญ)

ย 

1. + ๋ฅผ ์ œ์™ธํ•œ ์—ฐ์‚ฐ์ž์˜ ์ˆซ์ž OP [์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๊ฐ’] ์˜ ๊ฒฐ๊ณผ๋Š” NaN ์ด๋‹ค

ย  โ†’ ์—„์ฒญ ํ”ํ•œ ์˜ค๋ฅ˜

2. 0์œผ๋กœ ๋‚˜๋ˆ„์–ด๋„ ์˜ค๋ฅ˜๊ฐ€ ์—†๋‹ค (Infinity)

3. + ์—ฐ์‚ฐ์€ ์กฐ๊ฑด์ด ๋‹ค์Œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋”ฐ๋ฆ…๋‹ˆ๋‹ค

ย  1. ๋‘ ํ”ผ์—ฐ์‚ฐ์ž๋ฅผ ๋ชจ๋‘ ์›์‹œ๊ฐ’์œผ๋กœ ๋ฐ”๊ฟ‰๋‹ˆ๋‹ค (ToPrimitive)

ย  2. ์›์‹œ๊ฐ’ ์ค‘ ํ•˜๋‚˜๋ผ๋„ ๋ฌธ์ž์—ด์ด๋ฉด ๋‹ค๋ฅธ ํ•˜๋‚˜๋„ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ํ•ฉ์นœ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค

ย  3. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ๋‘ ํ”ผ์—ฐ์‚ฐ์ž ๋ชจ๋‘ ์ˆซ์ž๋กœ ๋ฐ˜ํ™˜ํ•˜๊ณ  ๋”ํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค

๐ŸŽ OPERATOR [์—ฐ์‚ฐ์ž]

์‚ฐ์ˆ  ์—ฐ์‚ฐ์žย [ +, -, *, /, % ]

TMI: 0์œผ๋กœ ๋‚˜๋ˆ„๋ฉด ์•ˆ๋˜๋Š” ์ด์œ 

ย 

1. ๋‚˜๋ˆ—์…ˆ์€ ํ”ผ์—ฐ์‚ฐ์ž์™€ ์ œ์ˆ˜์˜ ๋บ„์…ˆ์—ฐ์‚ฐ

2. ์ œ์ˆ˜๋Š” 0์ด๋ฏ€๋กœ ์˜์›ํžˆ ๋นผ์•ผ ํ•จ

3. ์˜ค๋ฅ˜

ย 

ย 

To be continued...

๋‹ค์Œ์— ํ• ๊ฑฐ: ์‚ฐ์ˆ  ์—ฐ์‚ฐ์ž (์ถ”๊ฐ€), ๋ถˆ๋ฆฌ์–ธ ์—ฐ์‚ฐ์ž, ๋น„ํŠธ ์—ฐ์‚ฐ์ž, ํŠน์ˆ˜ ์—ฐ์‚ฐ์ž, void, typeof, instanceof, ๊ฐ์ฒด ์—ฐ์‚ฐ์ž (new, delete, in)

Made with Slides.com