Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork3
Revert "1.2.7"#87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
133 changes: 67 additions & 66 deletions1-js/02-first-steps/07-type-conversions/article.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,149 +1,150 @@ | ||
#Type Conversions | ||
Most of the time, operators and functions automatically convert the values given to them to the right type. | ||
For example,`alert`automatically converts any value to a string to show it. Mathematical operations convert values to numbers. | ||
There are also cases when we need to explicitly convert a value to the expected type. | ||
```smart header="Not talking about objects yet" | ||
In this chapter, we won't cover objects. For now, we'll just be talking about primitives. | ||
Later, after we learn about objects, in the chapter<info:object-toprimitive>we'll see how objects fit in. | ||
``` | ||
##String Conversion | ||
String conversion happens when we need the string form of a value. | ||
For example,`alert(value)` does it to show the value. | ||
We can also call the`String(value)` function to convert a value to a string: | ||
```js run | ||
letvalue = true; | ||
alert(typeofvalue); // boolean | ||
*!* | ||
value = String(value); //now value is a string "true" | ||
alert(typeofvalue); // string | ||
*/!* | ||
``` | ||
String conversion is mostly obvious. A `false`becomes`"false"`, `null`becomes`"null"`,etc. | ||
##Numeric Conversion | ||
Numeric conversion happens in mathematical functions and expressions automatically. | ||
For example, when division `/`is applied to non-numbers: | ||
```js run | ||
alert( "6" / "2" ); // 3,strings are converted to numbers | ||
``` | ||
We can use the `Number(value)` function to explicitly convert a `value` to a number: | ||
```js run | ||
letstr = "123"; | ||
alert(typeofstr); // string | ||
letnum = Number(str); //becomes a number 123 | ||
alert(typeofnum); // number | ||
``` | ||
Explicit conversion is usually required when we read a value from a string-based source like a text form but expect a number to be entered. | ||
If the string is not a valid number, the result of such a conversion is`NaN`.For instance: | ||
```js run | ||
letage = Number("an arbitrary string instead of a number"); | ||
alert(age); // NaN,conversion failed | ||
``` | ||
Numeric conversion rules: | ||
|Value |Becomes... | | ||
|-------|-------------| | ||
|`undefined`|`NaN`| | ||
|`null`|`0`| | ||
|<code>true and false</code> | `1`and `0` | | ||
| `string` |Whitespaces from the start and end are removed. If the remaining string is empty, the result is`0`.Otherwise, the number is "read" from the string. An error gives `NaN`. | | ||
Examples: | ||
```js run | ||
alert( Number(" 123 ") ); // 123 | ||
alert( Number("123z") ); // NaN (error reading a number at "z") | ||
alert( Number(true) ); // 1 | ||
alert( Number(false) ); // 0 | ||
``` | ||
Please note that `null`and `undefined`behave differently here: `null`becomes zero while`undefined`becomes `NaN`. | ||
Most mathematical operators also perform such conversion, we'll see that in the next chapter. | ||
##Boolean Conversion | ||
Boolean conversion is the simplest one. | ||
It happens in logical operations (later we'll meet condition tests and other similar things) but can also be performed explicitly with a call to `Boolean(value)`. | ||
The conversion rule: | ||
-Values that are intuitively "empty", like `0`,an empty string, `null`, `undefined`, and `NaN`,become `false`. | ||
-Other values become `true`. | ||
For instance: | ||
```js run | ||
alert( Boolean(1) ); // true | ||
alert( Boolean(0) ); // false | ||
alert( Boolean("hello") ); // true | ||
alert( Boolean("") ); // false | ||
``` | ||
````warn header="Please note: the string with zero`\"0\"`is `true`" | ||
Some languages (namely PHP)treat`"0"`as`false`. But in JavaScript, a non-empty string is always `true`. | ||
```js run | ||
alert( Boolean("0") ); // true | ||
alert( Boolean(" ") ); //spaces, also true (any non-empty string is true) | ||
``` | ||
```` | ||
##Summary | ||
The three most widely used type conversions are to string, to number, and to boolean. | ||
**`String Conversion`** --Occurs when we output something. Can be performed with `String(value)`.The conversion to string is usually obvious for primitive values. | ||
**`Numeric Conversion`** --Occurs in math operations. Can be performed with `Number(value)`. | ||
The conversion follows the rules: | ||
|Value |Becomes... | | ||
|-------|-------------| | ||
|`undefined`|`NaN`| | ||
|`null`|`0`| | ||
|<code>true / 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`. | | ||
**`Boolean Conversion`** --Occurs in logical operations. Can be performed with `Boolean(value)`. | ||
Follows the rules: | ||
|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, not `0`. | ||
- `"0"` and space-only strings like `" "` are true as a boolean. | ||
Objects aren't covered here. We'll return to them later in the chapter <info:object-toprimitive> that is devoted exclusively to objects after we learn more basic things about JavaScript. |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.