-
-
Notifications
You must be signed in to change notification settings - Fork 3
Type Conversions #73
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Type Conversions #73
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
92c2e06
1.2.7
otmon76 b8cb272
Update 1-js/02-first-steps/07-type-conversions/article.md
otmon76 89505f6
Update 1-js/02-first-steps/07-type-conversions/article.md
otmon76 0bddbd9
Update 1-js/02-first-steps/07-type-conversions/article.md
otmon76 30c0074
Update 1-js/02-first-steps/07-type-conversions/article.md
otmon76 80aaca5
Update 1-js/02-first-steps/07-type-conversions/article.md
otmon76 8554c7e
Update 1-js/02-first-steps/07-type-conversions/article.md
otmon76 5ae77fe
Update 1-js/02-first-steps/07-type-conversions/article.md
otmon76 b810622
Update 1-js/02-first-steps/07-type-conversions/article.md
otmon76 9126e5c
Update 1-js/02-first-steps/07-type-conversions/article.md
otmon76 50cc9a2
Update 1-js/02-first-steps/07-type-conversions/article.md
otmon76 23b73be
Update 1-js/02-first-steps/07-type-conversions/article.md
otmon76 c9e9243
Update 1-js/02-first-steps/07-type-conversions/article.md
otmon76 97b9d09
Update 1-js/02-first-steps/07-type-conversions/article.md
otmon76 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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,150 +1,145 @@ | ||
# Type Conversions | ||
# Typová konverze | ||
|
||
Most of the time, operators and functions automatically convert the values given to them to the right type. | ||
Operátory a funkce většinou automaticky převedou hodnoty, které jim byly zadány, na správný typ. | ||
|
||
For example, `alert` automatically converts any value to a string to show it. Mathematical operations convert values to numbers. | ||
Například `alert` automaticky převede libovolnou hodnotu na řetězec a ten zobrazí. Matematické operace převádějí hodnoty na čísla. | ||
|
||
There are also cases when we need to explicitly convert a value to the expected type. | ||
Existují však případy, v nichž musíme konverzi hodnoty na potřebný typ explicitně uvést. | ||
|
||
```smart header="Not talking about objects yet" | ||
In this chapter, we won't cover objects. For now, we'll just be talking about primitives. | ||
```smart header="Zatím nehovoříme o objektech" | ||
V této kapitole se ještě nebudeme zabývat objekty. Prozatím budeme hovořit jen o primitivních typech. | ||
|
||
Later, after we learn about objects, in the chapter <info:object-toprimitive> we'll see how objects fit in. | ||
Až se později dozvíme něco o objektech, v kapitole <info:object-toprimitive> uvidíme, jak do toho zapadají. | ||
``` | ||
|
||
## String Conversion | ||
## Konverze na řetězec | ||
|
||
String conversion happens when we need the string form of a value. | ||
Konverze na řetězec se odehraje tehdy, když potřebujeme hodnotu ve formě řetězce. Provádí ji například `alert(hodnota)`, aby mohla zobrazit hodnotu. | ||
|
||
For example, `alert(value)` does it to show the value. | ||
danipoma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
We can also call the `String(value)` function to convert a value to a string: | ||
Můžeme také volat funkci `String(hodnota)`, která převede zadanou hodnotu na řetězec: | ||
|
||
```js run | ||
let value = true; | ||
alert(typeof value); // boolean | ||
let hodnota = true; | ||
alert(typeof hodnota); // boolean | ||
|
||
*!* | ||
value = String(value); // now value is a string "true" | ||
alert(typeof value); // string | ||
hodnota = String(hodnota); // hodnota je nyní řetězec "true" | ||
alert(typeof hodnota); // string | ||
otmon76 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/!* | ||
``` | ||
|
||
String conversion is mostly obvious. A `false` becomes `"false"`, `null` becomes `"null"`, etc. | ||
Konverze na řetězec je většinou zřejmá: `false` se převede na `"false"`, `null` se převede na `"null"`, atd. | ||
|
||
## Numeric Conversion | ||
## Konverze na číslo | ||
|
||
Numeric conversion happens in mathematical functions and expressions automatically. | ||
Konverze na číslo se automaticky odehrává v matematických funkcích a výrazech. | ||
|
||
For example, when division `/` is applied to non-numbers: | ||
Například když použijeme dělení `/` na něco jiného než čísla: | ||
|
||
```js run | ||
alert( "6" / "2" ); // 3, strings are converted to numbers | ||
alert( "6" / "2" ); // 3, řetězce se konvertují na čísla | ||
``` | ||
|
||
We can use the `Number(value)` function to explicitly convert a `value` to a number: | ||
Pro explicitní konverzi hodnoty `hodnota` na číslo můžeme použít funkci `Number(hodnota)`: | ||
|
||
```js run | ||
let str = "123"; | ||
otmon76 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
alert(typeof str); // string | ||
otmon76 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
let num = Number(str); // becomes a number 123 | ||
let num = Number(str); // stane se z něj číslo 123 | ||
otmon76 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
alert(typeof num); // number | ||
otmon76 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``` | ||
|
||
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. | ||
Explicitní konverze je obvykle potřebná, když načítáme hodnotu z řetězcového zdroje, například z textového formuláře, ale očekáváme zadání čísla. | ||
|
||
If the string is not a valid number, the result of such a conversion is `NaN`. For instance: | ||
Není-li v řetězci platné číslo, výsledkem konverze bude `NaN`. Například: | ||
|
||
```js run | ||
let age = Number("an arbitrary string instead of a number"); | ||
let věk = Number("nějaký řetězec místo čísla"); | ||
|
||
alert(age); // NaN, conversion failed | ||
alert(věk); // NaN, konverze neuspěla | ||
``` | ||
|
||
Numeric conversion rules: | ||
Pravidla pro konverzi na číslo: | ||
|
||
| Value | Becomes... | | ||
|-------|-------------| | ||
| Hodnota | Převede se na... | | ||
|---------|-------------------| | ||
|`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`. | | ||
|<code>true</code> a <code>false</code> | `1` a `0` | | ||
| `řetězec` | Odstraní se bílé znaky (mezery, tabulátory, konce řádku) ze začátku a konce. Je-li výsledný řetězec prázdný, výsledkem je `0`. Jinak se číslo „přečte“ z řetězce. Při chybě je vydáno `NaN`. | | ||
|
||
Examples: | ||
Příklady: | ||
|
||
```js run | ||
alert( Number(" 123 ") ); // 123 | ||
alert( Number("123z") ); // NaN (error reading a number at "z") | ||
alert( Number("123z") ); // NaN (chyba při čtení čísla v písmenu "z") | ||
otmon76 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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. | ||
Všimněte si, že `null` a `undefined` se tady chovají rozdílně: `null` se převede na nulu, zatímco `undefined` se převede na `NaN`. | ||
|
||
## Boolean Conversion | ||
Tuto konverzi provádí i většina matematických operátorů. Uvidíme to v následující kapitole. | ||
|
||
Boolean conversion is the simplest one. | ||
## Konverze na boolean | ||
|
||
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)`. | ||
danipoma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Konverze na boolean je nejjednodušší. Odehrává se v logických operátorech (později se setkáme s testy platnosti podmínky a podobnými věcmi), ale můžeme ji provést i explicitně voláním `Boolean(value)`. | ||
otmon76 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
The conversion rule: | ||
Pravidla konverze: | ||
|
||
- Values that are intuitively "empty", like `0`, an empty string, `null`, `undefined`, and `NaN`, become `false`. | ||
- Other values become `true`. | ||
- Hodnoty, které jsou intuitivně „prázdné“, tj. `0`, prázdný řetězec, `null`, `undefined` a `NaN`, se převedou na `false`. | ||
- Ostatní hodnoty se převedou na `true`. | ||
|
||
For instance: | ||
Příklad: | ||
|
||
```js run | ||
alert( Boolean(1) ); // true | ||
alert( Boolean(0) ); // false | ||
|
||
alert( Boolean("hello") ); // true | ||
alert( Boolean("ahoj") ); // 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`. | ||
````warn header="Všimněte si: řetězec s nulou `\"0\"` se převede na `true`" | ||
Některé jazyky (konkrétně PHP) zacházejí s řetězcem `"0"` jako s `false`, avšak v JavaScriptu je neprázdný řetězec vždy `true`. | ||
|
||
```js run | ||
alert( Boolean("0") ); // true | ||
alert( Boolean(" ") ); // spaces, also true (any non-empty string is true) | ||
alert( Boolean(" ") ); // mezery, také true (každý neprázdný řetězec je true) | ||
``` | ||
```` | ||
|
||
## Summary | ||
## Shrnutí | ||
|
||
The three most widely used type conversions are to string, to number, and to boolean. | ||
Tři nejčastěji používané typové konverze jsou na řetězec, na číslo a na boolean. | ||
|
||
**`String Conversion`** -- Occurs when we output something. Can be performed with `String(value)`. The conversion to string is usually obvious for primitive values. | ||
**`Konverze na řetězec`** -- Nastává, když něco vypisujeme. Můžeme ji provést pomocí `String(value)`. Konverze na řetězec je u primitivních hodnot obvykle zřejmá. | ||
otmon76 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
**`Numeric Conversion`** -- Occurs in math operations. Can be performed with `Number(value)`. | ||
**`Konverze na číslo`** -- Nastává při matematických operacích. Můžeme ji provést pomocí `Number(value)`. | ||
otmon76 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
The conversion follows the rules: | ||
Konverze se řídí těmito pravidly: | ||
|
||
| Value | Becomes... | | ||
|-------|-------------| | ||
| Hodnota | Převede se na... | | ||
|---------|-------------------| | ||
|`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`. | | ||
| `řetězec` | Načte se „doslovně“, bílé znaky na obou stranách se ignorují. Z prázdného řetězce se stane `0`. Při chybě je vydáno `NaN`. | | ||
|
||
**`Boolean Conversion`** -- Occurs in logical operations. Can be performed with `Boolean(value)`. | ||
**`Konverze na boolean`** -- Nastává při logických operacích. Můžeme ji provést pomocí `Boolean(value)`. | ||
otmon76 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Follows the rules: | ||
Řídí se těmito pravidly: | ||
|
||
| Value | Becomes... | | ||
|-------|-------------| | ||
| Hodnota | Převede se na... | | ||
|---------|-------------------| | ||
|`0`, `null`, `undefined`, `NaN`, `""` |`false`| | ||
|any other value| `true` | | ||
|
||
|jakákoli jiná hodnota| `true` | | ||
|
||
Most of these rules are easy to understand and memorize. The notable exceptions where people usually make mistakes are: | ||
Většinu těchto pravidel je snadné pochopit a zapamatovat si. Významné výjimky, v nichž lidé obvykle chybují, jsou: | ||
|
||
- `undefined` is `NaN` as a number, not `0`. | ||
- `"0"` and space-only strings like `" "` are true as a boolean. | ||
- `undefined` převedené na číslo je `NaN`, ne `0`. | ||
- `"0"` a řetězce obsahující jen mezery, např. `" "`, převedené na boolean jsou true. | ||
otmon76 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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. | ||
O objektech se zde nezmiňujeme. Později, až se v JavaScriptu naučíme další základní věci, se k nim vrátíme v kapitole <info:object-toprimitive>, která je věnována výlučně objektům. | ||
otmon76 marked this conversation as resolved.
Show resolved
Hide resolved
|
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.
Uh oh!
There was an error while loading. Please reload this page.