-
Notifications
You must be signed in to change notification settings - Fork 31
Type Conversions #141
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 #141
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
39d00b2
Update article.md
Gabitzzz 5008a0e
Update article.md
Gabitzzz 9ac94af
Update 1-js/02-first-steps/07-type-conversions/article.md
Gabitzzz 8084a93
Update 1-js/02-first-steps/07-type-conversions/article.md
Gabitzzz 4887660
Update 1-js/02-first-steps/07-type-conversions/article.md
Gabitzzz 6d21296
Update 1-js/02-first-steps/07-type-conversions/article.md
Gabitzzz ecbea80
Update 1-js/02-first-steps/07-type-conversions/article.md
Gabitzzz 542fe33
Update 1-js/02-first-steps/07-type-conversions/article.md
Gabitzzz cb3d429
Update 1-js/02-first-steps/07-type-conversions/article.md
Gabitzzz 6859bfe
Update 1-js/02-first-steps/07-type-conversions/article.md
Gabitzzz 797d94e
Update solution.md
Gabitzzz 2bc46af
Update task.md
Gabitzzz 7b9aa74
Update solution.md
Gabitzzz c56949e
Update 1-js/02-first-steps/07-type-conversions/article.md
Gabitzzz 91ba012
Update 1-js/02-first-steps/07-type-conversions/article.md
Gabitzzz a997f19
Update 1-js/02-first-steps/07-type-conversions/article.md
Gabitzzz b6bb4a3
Update 1-js/02-first-steps/07-type-conversions/article.md
Gabitzzz 6354489
Update 1-js/02-first-steps/07-type-conversions/article.md
Gabitzzz b9daeaf
Update 1-js/02-first-steps/07-type-conversions/article.md
Gabitzzz 26d0690
Update 1-js/02-first-steps/07-type-conversions/article.md
Gabitzzz 042d40c
Update 1-js/02-first-steps/07-type-conversions/article.md
Gabitzzz 42c0af0
Update 1-js/02-first-steps/07-type-conversions/article.md
Gabitzzz 135b3de
Update 1-js/02-first-steps/07-type-conversions/article.md
Gabitzzz b0a77b9
Update 1-js/02-first-steps/07-type-conversions/article.md
Gabitzzz 53543ca
Update 1-js/02-first-steps/07-type-conversions/article.md
Gabitzzz 76a33db
Update task.md
Gabitzzz 4d82f99
Update solution.md
Gabitzzz 26ed690
Update 1-js/02-first-steps/07-type-conversions/article.md
Gabitzzz 98b8510
Type Conversions
Gabitzzz 46093f7
Update task.md
Gabitzzz 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,152 @@ | ||
# Type Conversions | ||
# Conversii de tip | ||
|
||
Most of the time, operators and functions automatically convert the values given to them to the right type. | ||
Majoritatea timpului, operatorii si funcțiile convertesc automat valorile date către tipul corect. | ||
|
||
For example, `alert` automatically converts any value to a string to show it. Mathematical operations convert values to numbers. | ||
De exemplu, `alert` convertește automat orice valoare către un șir pentru a o arăta. Operațiile matematice convertesc valorile în numere. | ||
|
||
There are also cases when we need to explicitly convert a value to the expected type. | ||
De asemenea există și cazuri unde este nevoie să convertim explicit o valoare către tipul așteptat. | ||
|
||
```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="Nu vorbim despre obiecte încă" | ||
În acest captiol, nu vom acoperi obiectele. Pentru moment vom vorbi doar despre primitive. | ||
|
||
Later, after we learn about objects, in the chapter <info:object-toprimitive> we'll see how objects fit in. | ||
Mai târziu, dupa ce vom învăța despre obiecte, în acest capitol <info:object-toprimitive> vom vedea cum se incadrează obiectele. | ||
``` | ||
|
||
## String Conversion | ||
## Conversia unui Șir | ||
|
||
String conversion happens when we need the string form of a value. | ||
Conversia unui șir se întâmplă când avem nevoie de forma de șir a unei valori. | ||
|
||
For example, `alert(value)` does it to show the value. | ||
De exemplu, `alert(value)` o face pentru a arăta valoarea. | ||
|
||
We can also call the `String(value)` function to convert a value to a string: | ||
Totodată putem apela funcția `String(value)` pentru a converti o valoare într-un șir | ||
|
||
```js run | ||
let value = true; | ||
alert(typeof value); // boolean | ||
|
||
*!* | ||
value = String(value); // now value is a string "true" | ||
alert(typeof value); // string | ||
value = String(value); // acum valoarea este un șir(string?): "adevărat" | ||
alert(typeof value); // șir | ||
*/!* | ||
``` | ||
|
||
String conversion is mostly obvious. A `false` becomes `"false"`, `null` becomes `"null"`, etc. | ||
Conversia unui șir este în mare parte evidentă. Un `false` devine `"false"`, `null` devine `"null"`, etc. | ||
|
||
## Numeric Conversion | ||
## Conversia Numerică | ||
|
||
Numeric conversion happens in mathematical functions and expressions automatically. | ||
Conversia numerică se petrece în funcții și expresii matematice automat. | ||
|
||
For example, when division `/` is applied to non-numbers: | ||
De exemplu, când împărțirea `/` este aplicată non-numerelor: | ||
|
||
```js run | ||
alert( "6" / "2" ); // 3, strings are converted to numbers | ||
alert( "6" / "2" ); // 3, șirurile sunt convertite în numere | ||
``` | ||
|
||
We can use the `Number(value)` function to explicitly convert a `value` to a number: | ||
Putem folosi funcția `Number(value)` pentru a converti explicit o `value` către un număr. | ||
|
||
```js run | ||
let str = "123"; | ||
alert(typeof str); // string | ||
alert(typeof str); // șir | ||
|
||
let num = Number(str); // becomes a number 123 | ||
let num = Number(str); // devine un număr 123 | ||
|
||
alert(typeof num); // number | ||
alert(typeof num); // număr | ||
``` | ||
|
||
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. | ||
Conversia explicită de obicei este cerută când citim o valoarea dintr-o sursă bazată pe un șir ca și un formular text dar așteaptă un număr să fie inclus. | ||
|
||
If the string is not a valid number, the result of such a conversion is `NaN`. For instance: | ||
Dacă șirul nu este un număr valid, rezultatul acestei conversii este `NaN`. De exemplu: | ||
|
||
```js run | ||
let age = Number("an arbitrary string instead of a number"); | ||
let age = Number("un șir arbitrar in locul unui număr"); | ||
|
||
alert(age); // NaN, conversion failed | ||
alert(age); // NaN, conversie eșuată | ||
``` | ||
|
||
Numeric conversion rules: | ||
Regulile conversiei numerice: | ||
|
||
| Value | Becomes... | | ||
| Valoare | Devine... | | ||
|-------|-------------| | ||
|`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`. | | ||
| `string` | Spațiile goale de la ïnceput și sfârșit sunt eliminate. Dacă șirul rămas este gol, rezultatul este `0`. Altfel numărul este "citit" din șir. O eroare transmite `NaN`. | | ||
|
||
Examples: | ||
Exemple: | ||
|
||
```js run | ||
alert( Number(" 123 ") ); // 123 | ||
alert( Number("123z") ); // NaN (error reading a number at "z") | ||
alert( Number("123z") ); // NaN (eroare la citirea unui număr la "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`. | ||
Va rugăm să notați faptul că `null` și `undefined` se comportă diferit aici: `null` devine 0, în timp ce `undefined` devine `NaN`. | ||
|
||
Most mathematical operators also perform such conversion, we'll see that in the next chapter. | ||
Cei mai mulți operatori matematici execută o astfel de conversie, vom vedea aceasta în capitolul următor. | ||
|
||
## Boolean Conversion | ||
## Conversia Boolean | ||
|
||
Boolean conversion is the simplest one. | ||
Conversia boolean este cea mai simplă. | ||
|
||
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)`. | ||
Are loc în operațiile logice (mai târziu vom întâlni teste condiționale și alte lucruri similare) dar poate de asemenea fi executată specific cu o apelare la `Boolean(value)`. | ||
|
||
The conversion rule: | ||
Regula conversiei: | ||
|
||
- Values that are intuitively "empty", like `0`, an empty string, `null`, `undefined`, and `NaN`, become `false`. | ||
- Other values become `true`. | ||
- Valorile care sunt "goale" intuitiv, ca și `0`, un șir gol, `null`, `undefined`, și `NaN`, devin `false`. | ||
- Alte valori devin `true`. | ||
|
||
For instance: | ||
De exemplu: | ||
|
||
```js run | ||
alert( Boolean(1) ); // true | ||
alert( Boolean(0) ); // false | ||
|
||
alert( Boolean("hello") ); // true | ||
alert( Boolean("salut") ); // 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`. | ||
Alte limbaje (anume PHP) tratează `"0"` ca și `false`. Dar în JavaScript, un șir ne-gol este întotdeauna `true`. | ||
|
||
```js run | ||
alert( Boolean("0") ); // true | ||
alert( Boolean(" ") ); // spaces, also true (any non-empty string is true) | ||
alert( Boolean(" ") ); // spații, de asemenea true (orice șir ne-gol este true) | ||
``` | ||
```` | ||
|
||
## Summary | ||
## Rezumat | ||
|
||
The three most widely used type conversions are to string, to number, and to boolean. | ||
Cele mai des folosite tipuri de conversii sunt la șir, la numere, și la boolean | ||
|
||
**`String Conversion`** -- Occurs when we output something. Can be performed with `String(value)`. The conversion to string is usually obvious for primitive values. | ||
**`Conversia Șirului`** -- Apare când afișăm ceva. Poate fi executată cu `String(value)`. Conversia la șir este de obicei destul de evidentă pentru valori primitive. | ||
|
||
**`Numeric Conversion`** -- Occurs in math operations. Can be performed with `Number(value)`. | ||
**`Conversia Numerică`** -- Apare în operațiile matematice. Poate fi executată cu `Number(value)`. | ||
|
||
The conversion follows the rules: | ||
Conversia urmează regulile: | ||
|
||
| Value | Becomes... | | ||
| Valoare | Devine... | | ||
|-------|-------------| | ||
|`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`. | | ||
| `string` | Șirul este citit "așa cum este", spațiile goale din ambele părți sunt ignorate. Un șir gol devine `0`. O eroare transmite `NaN`. | | ||
|
||
**`Boolean Conversion`** -- Occurs in logical operations. Can be performed with `Boolean(value)`. | ||
**`Conversia Boolean`** -- Apare în operațiile logice. Poate fi executată cu `Boolean(value)`. | ||
|
||
Follows the rules: | ||
Urmează regulile: | ||
|
||
| Value | Becomes... | | ||
| Valoare | Devine... | | ||
|-------|-------------| | ||
|`0`, `null`, `undefined`, `NaN`, `""` |`false`| | ||
|any other value| `true` | | ||
|orice altă valoare| `true` | | ||
|
||
|
||
Most of these rules are easy to understand and memorize. The notable exceptions where people usually make mistakes are: | ||
Majoritatea acestor reguli sunt ușor de înțeles și memorat. Excepțiile notabile unde oamenii fac greșeli de obicei sunt: | ||
|
||
- `undefined` is `NaN` as a number, not `0`. | ||
- `"0"` and space-only strings like `" "` are true as a boolean. | ||
- `undefined` este `NaN` ca și număr, nu `0`. | ||
- `"0"` și șirurile mono-spațiale ca și `" "` sunt adevărate ca și boolean. | ||
|
||
Obiectele nu sunt acoperite aici. Ne vom întoarce la ele mai târziu în capitolul <info:object-toprimitive> ce este devotat exclusiv obiectelor după ce învățăm mai multe lucruri elementare despre JavaScript. | ||
|
||
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. | ||
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
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
6 changes: 3 additions & 3 deletions
6
1-js/02-first-steps/08-operators/2-assignment-result/solution.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,5 +1,5 @@ | ||
The answer is: | ||
Răspunsul este: | ||
|
||
- `a = 4` (multiplied by 2) | ||
- `x = 5` (calculated as 1 + 4) | ||
- `a = 4` (multiplicat cu 2) | ||
- `x = 5` (calculat ca și 1 + 4) | ||
|
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
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
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
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.