JS – UP and Going (chapter 2)

Into JavaScript

As we asserted in Chapter 1, JavaScript has typed values, not typed variables. The following built-in types are available:

  • string
  • number
  • boolean
  •  null and undefined
  • object
  • symbol (new to ES6)

JavaScript provides a `typeof` operator that can examine a value and tell you what type it is:


typeof null is an interesting case, because it errantly returns `”object“`, when you’d expect it to return `”null”`.

**Warning:** This is a long-standing bug in JS, but one that is likely never going to be fixed. Too much code on the Web relies on the bug and thus fixing it would cause a lot more bugs!


The `object` type refers to a compound value where you can set properties (named locations) that each hold their own values of any type. This is perhaps one of the most useful value types in all of JavaScript.


It may be helpful to think of this `obj` value visually:fig4

Properties can either be accessed with *dot notation* (i.e., `obj.a`) or *bracket notation* (i.e., `obj[“a”]`). Dot notation is shorter and generally easier to read, and is thus preferred when possible.


An array is an `object` that holds values (of any type) not particularly in named properties/keys, but rather in numerically indexed positions. For example:


Note: Languages that start counting at zero, like JS does, use `0` as the index of the first element in the array. It may be helpful to think of `arr` visually:


Because arrays are special objects (as `typeof` implies), they can also have properties, including the automatically updated `length` property.


The other object sub-type you’ll use all over your JS programs is a function:


Again, functions are a subtype of objects ‘typeof` returns `”function“`, which implies that a `function` is a main type — and can thus have properties, but you typically will only use function object properties (like ``) in limited cases.

Built-In Type Methods


Truthy & Falsy

The specific list of “falsy” values in JavaScript is as follows:

  • “”  (empty string)
  • 0,  -0, NaN (invalid `number`)
  • null, undefined
  • false

Any value that’s not on this “falsy” list is “truthy.” Here are some examples of those:

  • `”hello”`
  • `42`
  • `true`
  •  `[ ]`, `[ 1, “2”, 3 ]` (arrays)
  • `{ }`, `{ a: 42 }` (objects)
  •  `function foo() { .. }` (functions)


The difference between `==` and `===` is usually characterized that `==` checks for value equality and `===` checks for both value and type equality. However, this is inaccurate. The proper way to characterize them is that `==` checks for value equality with coercion allowed, and `===` checks for value equality without allowing coercion; `===` is often called “strict equality” for this reason.

Consider the implicit coercion that’s allowed by the `==` loose-equality comparison and not allowed with the `===` strict-equality:



Strict Mode

ES5 added a “strict mode” to the language, which tightens the rules for certain behaviors. Generally, these restrictions are seen as keeping the code to a safer and more appropriate set of guidelines. Also, adhering to strict mode makes your code generally more optimizable by the engine. Strict mode is a big win for code, and you should use it for all your programs.

You can opt in to strict mode for an individual function, or an entire file, depending on where you put the strict mode pragma:


One key difference (improvement!) with strict mode is disallowing the implicit auto-global variable declaration from omitting the `var`:


Functions as Values

the function itself is a value, just like 42 or [1,2,3] would be.

Not only can you pass a value (argument) to a function, but a function itself can be a value that’s assigned to variables, or passed to or returned from other functions.


The first function expression assigned to the foo variable is called anonymous because it has no name.

The second function expression is named (bar), even as a reference to it is also assigned to the x variable. *Named function expressions* are generally more preferable, though *anonymous function expressions* are still extremely common.


You can think of closure as a way to “remember” and continue to access a function’s scope (its variables) even once the function has finished running.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s