Values, Objects & Vars

the essence of programming

Daniel Jackson

Values, Expressions & Variables

  • Functional programming: computing only with values
  • Functions are values too (more later)

Literals, Expressions & Values

  • An expression evaluates to a value
  • Expression can be a literal or a compound expression
  • JS: Operators are overloaded
  • JS: Numbers are 64 bit floating point
  • JS: Implicit conversions (yuck)

Function Values

  • A function expression evaluates to a function value
  • Functions can only be applied: no other operator
  • Arrow syntax (almost) equivalent to function keyword

Variables & Environments

  • Read assignment x=3 as "x gets 3"
  • Assignment binds variables in environment
  • Can rebind to a new value


  • Each function and block introduces a new environment
  • Variable in enclosing environment is accessible...
  • ... unless "shadowed" by variable of same name

Scope qualifiers: var

  • Keyword var limits scope to function
  • As if variable were a function argument
  • Remove var keyword and run again

Scope qualifiers: let

  • Keyword let limits scope to block
  • Add variable i as last expression and rerun

Scope qualifiers: const

  • Keyword const also limits scope to block
  • Prohibits reassignment
  • Try replacing const by let or var

Names in JavaScript

  • In JS, all names are variables or fields
  • Objects, including the environment, are mutable
  • So can replace built-in functions (for good or bad)

Contrast to Java

  • This compiles and executes in Java
  • Separate namespaces for methods, classes, vars
  • In JS, variables & fields are the only names!

Objects, Mutation & Aliasing

  • An object's value is its identity
  • Sharing leads to aliasing (& subtle bugs)


  • An object maps fields to values
  • Objects are mutable: can set field


  • An array is an object whose fields are indexes
  • So arrays are mutable too
  • The value of an object is its identity


  • Assignment x = e binds value of e to x
  • Recall that value of object is its identity
  • So here, two vars refer to same object
  • To avoid this, use immutable data structures

Polymorphism & Dynamic Types

  • Variables can be bound to any type at runtime
  • So functions are naturally polymorphic
  • And arrays are heterogeneous

Checking Your Understanding

  • How recursion in JS relies on variables
  • Minimal scopes for more robust code

How Recursion Works

  • This probably looks familiar
  • But how exactly is exp bound?

An Equivalent Version

  • You wouldn't actually write this
  • But it explains what's happening
  • Why no reference error when line 3 first runs?

What Happens and Why?

  1. Infinite loop
  2. Returns true
  3. Returns false

How to fix this code?

  1. Give sum an extra argument named sum
  2. Rename the inner sum variable
  3. Put var before first use of inner sum

Why this code?

  • What is this code doing and why?
  • This is a common idiom in JS
  • Now can be done with let instead