Types in JavaScript

`·2023년 4월 27일
0

1. Primitive

  • number, boolean, string, null, undefined, symbol
  • pass by value
    • a copy of the value is created and passed/assigned instead of reference to the original value.
    • any changes made to the value inside the function or to the new variable do not affect the original value

2. Non-Promitive

  • object(function, array)
  • pass by reference
    - passing function arguments by reference to the memory address of an object or data structure instead of making a copy of the object.
    - shallow copy: Object.assign({},obj) or Array.concat()
    • deep copy: JSON.parse(JSON.stringify())

3. Type Coercion

JS can be weird when it comes to type coercion. Try to guess what the output for each of the lines below are:

false == ""
false == []
false == {}
"" == 0
"" == []
"" == {}
0 == []
0 == {}
0 == null
true.toString() => 'true'
type of [] => 'object'
to check if it is array => Array.isArray()

4. static typing(vs dynamic)

1) props

  • self documentation
  • helpful in auto-completion
  • likely to be less bugs

2) cons

  • complexity
  • learning curve
  • slow speed in development

0개의 댓글