Software Studio: Javascript: Values & Types
Software Studio: Javascript: Values & Types
studio
javascript: values & types
Daniel Jackson
classifying values
all JS values
JavaScript
Value
Object
almost
immutable
Immutable
Object
Primitive
boolean
number
String
Function
user-defined
objects
Mutable
Object
User-Defined
Object
Array
typeof
> typeof(1)
"number"
> typeof(false)
"boolean"
> typeof('a')
"string"
> typeof([])
"object"
> typeof({})
"object"
> typeof(null)
"object"
"function"
> typeof(undefined)
"undefined"
> typeof(typeof(1))
"string"
["h", "i"]
["h", "i"]
> y = x
["h", "i"]
"o"
"o"
> x
["h", "o"]
> x
["h", "o"]
> x = 'hi'
"hi"
aliasing
> x[1]
"i"
"o"
> x[1]
"i"
some objects
cant be modified
false
> '' === '0'
false
"false"
"false"
"false"
"false"
"false"
true
false
> 1 + 2
> 1.0 + 2
> 1/3
0.3333333333333333
1000000000000
1000000000000000000
1e+24
> 1/0
Infinity
> (1/0) * 0
NaN
good news
no int vs float
exponents
bad news
strange NaN value
no infinite precision
puzzle
is this good practice?
no, need
if (isNaN(f())) {...}
> typeof(NaN)
"number"
false
true
> newvar
undefined
> newvar
undefined
> obj = {}
Object
> obj.f
undefined
> obj.f = null
null
MIT OpenCourseWare
http://ocw.mit.edu
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.