Skip to content

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 30 commits into from
Nov 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
39d00b2
Update article.md
Gabitzzz Nov 26, 2022
5008a0e
Update article.md
Gabitzzz Nov 26, 2022
9ac94af
Update 1-js/02-first-steps/07-type-conversions/article.md
Gabitzzz Nov 27, 2022
8084a93
Update 1-js/02-first-steps/07-type-conversions/article.md
Gabitzzz Nov 27, 2022
4887660
Update 1-js/02-first-steps/07-type-conversions/article.md
Gabitzzz Nov 27, 2022
6d21296
Update 1-js/02-first-steps/07-type-conversions/article.md
Gabitzzz Nov 27, 2022
ecbea80
Update 1-js/02-first-steps/07-type-conversions/article.md
Gabitzzz Nov 27, 2022
542fe33
Update 1-js/02-first-steps/07-type-conversions/article.md
Gabitzzz Nov 27, 2022
cb3d429
Update 1-js/02-first-steps/07-type-conversions/article.md
Gabitzzz Nov 27, 2022
6859bfe
Update 1-js/02-first-steps/07-type-conversions/article.md
Gabitzzz Nov 27, 2022
797d94e
Update solution.md
Gabitzzz Nov 27, 2022
2bc46af
Update task.md
Gabitzzz Nov 27, 2022
7b9aa74
Update solution.md
Gabitzzz Nov 27, 2022
c56949e
Update 1-js/02-first-steps/07-type-conversions/article.md
Gabitzzz Nov 27, 2022
91ba012
Update 1-js/02-first-steps/07-type-conversions/article.md
Gabitzzz Nov 27, 2022
a997f19
Update 1-js/02-first-steps/07-type-conversions/article.md
Gabitzzz Nov 27, 2022
b6bb4a3
Update 1-js/02-first-steps/07-type-conversions/article.md
Gabitzzz Nov 27, 2022
6354489
Update 1-js/02-first-steps/07-type-conversions/article.md
Gabitzzz Nov 27, 2022
b9daeaf
Update 1-js/02-first-steps/07-type-conversions/article.md
Gabitzzz Nov 27, 2022
26d0690
Update 1-js/02-first-steps/07-type-conversions/article.md
Gabitzzz Nov 27, 2022
042d40c
Update 1-js/02-first-steps/07-type-conversions/article.md
Gabitzzz Nov 27, 2022
42c0af0
Update 1-js/02-first-steps/07-type-conversions/article.md
Gabitzzz Nov 27, 2022
135b3de
Update 1-js/02-first-steps/07-type-conversions/article.md
Gabitzzz Nov 27, 2022
b0a77b9
Update 1-js/02-first-steps/07-type-conversions/article.md
Gabitzzz Nov 27, 2022
53543ca
Update 1-js/02-first-steps/07-type-conversions/article.md
Gabitzzz Nov 27, 2022
76a33db
Update task.md
Gabitzzz Nov 27, 2022
4d82f99
Update solution.md
Gabitzzz Nov 27, 2022
26ed690
Update 1-js/02-first-steps/07-type-conversions/article.md
Gabitzzz Nov 27, 2022
98b8510
Type Conversions
Gabitzzz Nov 27, 2022
46093f7
Update task.md
Gabitzzz Nov 27, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 59 additions & 57 deletions 1-js/02-first-steps/07-type-conversions/article.md
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&nbsp;and&nbsp;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&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`. |
| `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.
12 changes: 7 additions & 5 deletions 1-js/02-first-steps/08-operators/1-increment-order/solution.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

The answer is:
Răspunsul este:


- `a = 2`
- `b = 2`
Expand All @@ -9,10 +10,11 @@ The answer is:
```js run no-beautify
let a = 1, b = 1;

alert( ++a ); // 2, prefix form returns the new value
alert( b++ ); // 1, postfix form returns the old value
alert( ++a ); // 2, forma prefix afișează noua valoare
alert( b++ ); // 1, forma sufix afișează vechea valoare


alert( a ); // 2, incremented once
alert( b ); // 2, incremented once
alert( a ); // 2, incrementat odată
alert( b ); // 2, incrementat odată
```

4 changes: 2 additions & 2 deletions 1-js/02-first-steps/08-operators/1-increment-order/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 5

---

# The postfix and prefix forms
# Forme sufix si prefix

What are the final values of all variables `a`, `b`, `c` and `d` after the code below?
Care sunt valorile finale ale variabilelor `a`, `b`, `c` și `d` după codul de mai jos?

```js
let a = 1, b = 1;
Expand Down
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)

5 changes: 3 additions & 2 deletions 1-js/02-first-steps/08-operators/2-assignment-result/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ importance: 3

---

# Assignment result
# Rezultatul sarcinii

Care sunt valorile lui `a` și `x` dupa codul de mai jos?

What are the values of `a` and `x` after the code below?

```js
let a = 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ undefined + 1 = NaN // (6)
" \t \n" - 2 = -2 // (7)
```

1. The addition with a string `"" + 1` converts `1` to a string: `"" + 1 = "1"`, and then we have `"1" + 0`, the same rule is applied.
2. The subtraction `-` (like most math operations) only works with numbers, it converts an empty string `""` to `0`.
3. The addition with a string appends the number `5` to the string.
4. The subtraction always converts to numbers, so it makes `" -9 "` a number `-9` (ignoring spaces around it).
5. `null` becomes `0` after the numeric conversion.
6. `undefined` becomes `NaN` after the numeric conversion.
7. Space characters, are trimmed off string start and end when a string is converted to a number. Here the whole string consists of space characters, such as `\t`, `\n` and a "regular" space between them. So, similarly to an empty string, it becomes `0`.
1. Adunarea cu un șir `"" + 1` convertește `1` într-un șir: `"" + 1 = "1"`, iar apoi avem `"1" + 0`, aceeași regulă este aplicată
2. Scăderea `-` (ca și majoritatea operațiilor matematica) funcționează doar cu numere, convertește un sir gol `""` în `0`.
3. Adunarea cu un șir alipește numărul `5` la șir
4. Scăderea întotdeauna convertește în numere, astfel va face `" -9 "` un număr `-9` (ignorănd spațiile din jurului lui).
5. `null` devine `0` după conversia numerică.
6. `undefined` devine `NaN` după conversia numerică.
7. Caracterele spații sunt tăiate din începutul si sfârșitului un șir când un șir este convertit într-un număr. Aici tot șirul constă în caractere spații ca și `\t`, `\n` și un spațiu "normal" între ele. Deci, similar cu un șir gol, devine `0`.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 5

---

# Type conversions
# Conversii de tip

What are results of these expressions?
Care sunt rezultatele acestor expresii?

```js no-beautify
"" + 1 + 0
Expand Down