0% found this document useful (0 votes)
28K views

Software Studio: Javascript: Values & Types

This document discusses JavaScript values and types. It classifies JavaScript values as either primitive or object values. Primitive values include boolean, number, string, null and undefined, while object values include functions and user-defined objects like arrays. It demonstrates using the typeof operator to determine a value's type and discusses aliasing, immutability, equality, truthiness, numbers, NaN, ReferenceError, null and undefined in JavaScript.

Uploaded by

singyee123
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28K views

Software Studio: Javascript: Values & Types

This document discusses JavaScript values and types. It classifies JavaScript values as either primitive or object values. Primitive values include boolean, number, string, null and undefined, while object values include functions and user-defined objects like arrays. It demonstrates using the typeof operator to determine a value's type and discusses aliasing, immutability, equality, truthiness, numbers, NaN, ReferenceError, null and undefined in JavaScript.

Uploaded by

singyee123
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

software

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"

note: no array type

> typeof([])

"object"

> typeof({})

"object"

> typeof(null)

"object"

> typeof(function () {})

"function"

> typeof(undefined)

"undefined"

note: no type type

> typeof(typeof(1))

"string"

aliasing & immutables

> x = ['h', 'i']

["h", "i"]

> x = ['h', 'i']

["h", "i"]

> y = x

["h", "i"]

> x[1] = 'o'

"o"

> y[1] = 'o'

"o"

> x

["h", "o"]

> x

["h", "o"]

> x = 'hi'

"hi"

aliasing

immutables are our friends!


no mention of x
no change to x

> x[1]

"i"

> x[1] = 'o'

"o"

> x[1]

"i"

some objects

cant be modified

equality and truthiness


like many scripting languages
features that save 3 seconds of typing
instead cost 3 hours of debugging
examples
behavior of built-in equality operator ==
strange rules of truthiness
> 0 == ''
true
> 0 == '0'
true
> '' == '0'
false

> 0 === ''


false

> 0 === '0'

false
> '' === '0'

false

> (false) ? "true" : "false";

"false"

> (null) ? "true" : "false";

"false"

> (undefined) ? "true" : "false";

"false"

> (0) ? "true" : "false";

"false"

> ('') ? "true" : "false";

"false"

> (undefined == null)

true

> (undefined === null)

false

numbers & non-numbers

> 1 + 2

> 1.0 + 2

> 1/3

0.3333333333333333

> 1000000 * 1000000

1000000000000

> 1000000 * 1000000 * 1000000

1000000000000000000

> 1000000 * 1000000 * 1000000 * 1000000

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?

if (f() != NaN) {...}

no, need

if (isNaN(f())) {...}

> typeof(NaN)

"number"

> NaN === NaN

false

> NaN !== NaN

true

undefined, reference error & null

notions of bad access


ReferenceError exception
undefined value (built-in)
null (predefined value)

> newvar

ReferenceError: newvar is not defined

> newvar = undefined

undefined

> newvar

undefined

> obj = {}

Object

> obj.f

undefined
> obj.f = null

null

how to use them


ReferenceError: regard as failure if raised
null: best left unused, IMO
undefined: unavoidable (eg, optional args)
a little paradox
undefined is a defined value for a variable

MIT OpenCourseWare
http://ocw.mit.edu

6.170 Software Studio


Spring 2013

For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.

You might also like