Skip to content

Commit

Permalink
tweaks to ch1, starting to cover 'void' operator
Browse files Browse the repository at this point in the history
  • Loading branch information
getify committed Jul 16, 2014
1 parent 8a64b41 commit 442ace1
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions types & grammar/ch1.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# You Don't Know JS: Types & Grammar
# Chapter 1: Wait... Types?

Most developers would tell you that a dynamic language (like JS) does not have *types*. Let's see what the authors of the ES5.1 specification have to say on the topic:
Most developers would say that a dynamic language (like JS) does not have *types*. Let's see what the ES5.1 specification has to say on the topic:

> Algorithms within this specification manipulate values each of which has an associated type. The possible value types are exactly those defined in this clause. Types are further sub classified into ECMAScript language types and specification types.
>
Expand All @@ -11,7 +11,7 @@ Now, if you're a fan of strongly-typed (statically-typed) languages, you probabl

Some people say JS shouldn't be said to have "types", but they should instead be called "tags" or perhaps "sub types".

Bah. We're going to use this definition (the same one that seems to drive the wording of the spec!): a *type* is an intrinsic, built-in set of characteristics that uniquely identifies a particular value and distinguishes it from other values, both to the engine **and to the developer**.
Bah. We're going to use this definition (the same one that seems to drive the wording of the spec!): a *type* is an intrinsic, built-in set of characteristics that uniquely identifies the behavior of a particular value and distinguishes it from other values, both to the engine **and to the developer**.

In other words, if both the engine and the developer treat value `42` (the number) differently than they treat value `"42"` (the string), then those two values have different *types*, namely `number` and `string`, respectively. When you use `42`, you are *intending* to do something numeric, like math. But when you use `"42"`, you are *intending* to do something string'ish, like outputting to the page, etc. **These two values have different types.**

Expand All @@ -21,7 +21,7 @@ That's by no means a perfect definition. But it's good enough for now. And it's

In JavaScript, variables don't have types -- **values have types**. Variables can hold any value, at any time.

Another way to think about JS types is that JS has types but it doesn't have "type enforcement", in that the engine doesn't insist that *variable* always holds values of the *same initial type*. A variable can, in one assignment statement, hold a `string`, and in the next hold a `number`, and so on.
Another way to think about JS types is that JS has types but it doesn't have "type enforcement", in that the engine doesn't insist that a *variable* always holds values of the *same initial type*. A variable can, in one assignment statement, hold a `string`, and in the next hold a `number`, and so on.

Also, the *value* `42` has an intrinsic type of `number`, and its *type* cannot be changed. Another value, like `"42"` with the `string` type, can be created *from* the `number` value `42`, through a process called **coercion**, which we will cover later.

Expand Down Expand Up @@ -52,7 +52,7 @@ typeof Symbol() === "symbol"; // true

These 6 listed types have values of the corresponding type and return a string of the same name, as shown. `Symbol` is a new data type as of ES6, and will be covered later.

As you may have noticed, I excluded `null` from this listing. It's *special*. Special in the sense that it's buggy.
As you may have noticed, I excluded `null` from this listing. It's *special* -- special in the sense that it's buggy.

```js
typeof null === "object"; // true
Expand Down Expand Up @@ -122,7 +122,7 @@ function foo() {
foo();
```

In both non-`strict mode` and `strict mode`, however, you can create a local variable of the name `undefined`, though again, this is a terrible idea!
In both non-`strict mode` and `strict mode`, however, you can create a local variable of the name `undefined`. But again, this is a terrible idea!

```js
function foo() {
Expand All @@ -136,8 +136,9 @@ foo();

Friends don't let friends override `undefined`. Ever.

While `undefined` is an actual identifier that represents (unless modified -- see above!) the built-in `undefined` value. Another way to get this value is the `void` operator.

// TODO cover:
// void 0
// typeof foo === "undefined"
// undefined vs. undeclared

Expand Down

0 comments on commit 442ace1

Please sign in to comment.