Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
iliakan committed Nov 28, 2016
1 parent 83b93e5 commit 9ad9063
Show file tree
Hide file tree
Showing 742 changed files with 883 additions and 778 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,4 @@ Please, don't hesitate to comment your code.

Comments increase the overall code footprint, but that's not a problem at all. There are many tools which minify the code before publishing to production server. They remove comments, so they do not appear in the working scripts. So, the comments do not have any negative effects on production at all.

Further in the tutorial we'll devote a special chapter to code style, also explaining how to write better comments.

[todo is there such a chapter?]
Further in the tutorial there will be a chapter <info:coding-style> that also explains how to write better comments.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,7 @@ All other types are called "primitive", because their values can contain only a

The `symbol` type is used to create unique identifiers for objects. We have to mention it here for completeness, but we'd better study them after covering objects.

[todo when ? chapter?]

## The typeof operator [#type-typeof]
[todo we need typeof in types]

The `typeof` operator returns the type of the argument. It's useful when we want to process values of different types differently, or just want to make a quick check.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ There are also cases when we need to explicitly convert a value to put things ri
[cut]

```smart header="Not covering objects yet"
In this chapter we don't cover objects yet. Here we study primitives first, and then we'll add objects in the chapter [todo].
In this chapter we don't cover objects yet. Here we study primitives first, and then we'll see what happens with objects in the chapter <info:object-toprimitive>.
```

[todo where we cover them? link?]

## ToString

The string conversion happens when we need a string form of a value.
Expand Down Expand Up @@ -141,37 +139,32 @@ alert( Boolean(" ") ); // also true (any non-empty string is true)
There exist three most widely used type conversions: to string, to number and to boolean.
`ToString`
: Happens when we output something, can be performed with `String(value)`. The conversion to string is usually obvious for primitive values.
**`ToString`** -- occurs when we output something, can be performed with `String(value)`. The conversion to string is usually obvious for primitive values.
`ToNumber`
: Happens in math operations, can be performed with `Number(value)`.
**`ToNumber`** -- occurs in math operations, can be performed with `Number(value)`.
The conversion follows the rules:
The conversion follows the rules:
| Value | Becomes... |
|-------|-------------|
|`undefined`|`NaN`|
|`null`|`0`|
|<code>true&nbsp;/&nbsp;false</code> | `1 / 0` |
| `string` | The string is read "as is", whitespaces from both sides are ignored. An empty string is `0`. An error gives `NaN`. |
| Value | Becomes... |
|-------|-------------|
|`undefined`|`NaN`|
|`null`|`0`|
|<code>true&nbsp;/&nbsp;false</code> | `1 / 0` |
| `string` | The string is read "as is", whitespaces from both sides are ignored. An empty string becomes `0`. An error gives `NaN`. |
ToBoolean
: Happens in logical operations, or can be performed with `Boolean(value)`.
**`ToBoolean`** -- occurs in logical operations, or can be performed with `Boolean(value)`.
Follows the rules:
Follows the rules:
| Value | Becomes... |
|-------|-------------|
|`0`, `null`, `undefined`, `NaN`, `""` |`false`|
|any other value| `true` |
| Value | Becomes... |
|-------|-------------|
|`0`, `null`, `undefined`, `NaN`, `""` |`false`|
|any other value| `true` |
Most of these rules are easy to understand and memorize. The notable exceptions where people usually make mistakes are:
- `undefined` is `NaN` as a number.
- `"0"` is true as a boolean.
Objects are not covered here, we'll return to them in the special chapter, devoted exclusively to objects.
[todo link?]
Objects are not covered here, we'll return to them later in the chapter <info:object-toprimitive>, devoted exclusively to objects, after we learn more basic things about Javascript.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ There are 3 types (also called "hints") of object-to-primitive conversion:
if (user == 1) { ... };
```

So, binary `+` and `==` use this hint. Seems right, because they operate both with strings and numbers. Although, there's some inconsistency here. The greater/less operator `<>` can work with both strings and numbers too. Still, it uses "number" hint. That's for historical reasons.
So, binary addition `+` and equality check `==` use this hint. Seems right, because they operate both with strings and numbers. Although, there's some inconsistency here. The greater/less operator `<>` can work with both strings and numbers too. Still, it uses "number" hint. That's for historical reasons.

In practice, all built-in objects except for one case (`Date` object, we'll learn it later) implement `"default"` conversion the same way as `"number"`. And probably we should do the same.

Please note -- there are only three conversions. That simple. There is no "boolean" (all objects are `true` in boolean context) or anything else. And if we treat `"default"` and `"number"` the same, like most built-ins do, then there are only two conversions.
Please note -- there are only three conversions. That simple. There is no "boolean" hint (all objects are `true` in boolean context) or anything else. And if we treat `"default"` and `"number"` the same, like most built-ins do, then there are only two conversions.

To do the conversion, Javascript tries to find and call three object methods:

Expand Down Expand Up @@ -207,6 +207,7 @@ For instance:

## Summary


[todo describe article]


Expand Down
File renamed without changes.
Loading

0 comments on commit 9ad9063

Please sign in to comment.