Skip to content

Commit ac3ec9c

Browse files
authored
Translate 'Type conversions' page (#30)
1 parent aafd0d0 commit ac3ec9c

File tree

4 files changed

+73
-74
lines changed

4 files changed

+73
-74
lines changed

1-js/02-first-steps/06-type-conversions/1-primitive-conversions-questions/solution.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ undefined + 1 = NaN // (6)
1717
" \t \n" - 2 = -2 // (7)
1818
```
1919

20-
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.
21-
2. The subtraction `-` (like most math operations) only works with numbers, it converts an empty string `""` to `0`.
22-
3. The addition with a string appends the number `5` to the string.
23-
4. The subtraction always converts to numbers, so it makes `" -9 "` a number `-9` (ignoring spaces around it).
24-
5. `null` becomes `0` after the numeric conversion.
25-
6. `undefined` becomes `NaN` after the numeric conversion.
26-
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`.
20+
1. Dodawanie ciągu znaków `"" + 1` rzutuje `1` do typu tekstowego: `"" + 1 = "1"`, a później analogicznie `"1" + 0`.
21+
2. Odejmowanie `-` (jak większość operacji matematycznych) działa wyłącznie z typami liczbowymi i konwertuje pusty napis `""` do `0`.
22+
3. Dodawanie ciągu znaków dołącza (konkatenuje) liczbę `5` do zmiennej.
23+
4. Odejmowanie zawsze rzutuje wartości do liczby, zatem konwertuje `" -9 "` na liczbę `-9` (ignoruje spacje dookoła).
24+
5. `null` rzutowany na liczbę stanie się `0`.
25+
6. `undefined` rzutowany na liczbę stanie się `NaN`.
26+
7. Podczas rzutowania typu tekstowego na liczbowy ignorowane są białe znaki po obydwóch stronach tekstu. W tym przypadku cały tekst składa się z białych znaków: `\t`, `\n` i "zwykłych" spacji pomiędzy nimi. W rezultacie powstały pusty ciąg znaków po rzutowaniu na liczbę da wartość `0`.

1-js/02-first-steps/06-type-conversions/1-primitive-conversions-questions/task.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ importance: 5
22

33
---
44

5-
# Type conversions
5+
# Rzutowanie typów
66

7-
What are results of these expressions?
7+
Jakie będą wyniki poniższych wyrażeń?
88

99
```js no-beautify
1010
"" + 1 + 0
@@ -24,4 +24,4 @@ undefined + 1
2424
" \t \n" - 2
2525
```
2626

27-
Think well, write down and then compare with the answer.
27+
Pomyśl dobrze, zapisz odpowiedzi i porównaj z prawidłowym rozwiązaniem.
Lines changed: 62 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,159 +1,158 @@
1-
# Type Conversions
1+
# Rzutowanie typów
22

3-
Most of the time, operators and functions automatically convert the values given to them to the right type.
3+
W większości przypadków operatory i funkcje automatycznie rzutują przekazywane do nich wartości na właściwy typ.
44

5-
For example, `alert` automatically converts any value to a string to show it. Mathematical operations convert values to numbers.
5+
Na przykład `alert` automatycznie zmieni typ dowolnej wartości do typu tekstowego. Matematyczne operacje rzutują wartości do typów liczbowych.
66

7-
There are also cases when we need to explicitly convert a value to the expected type.
7+
Istnieją jednak przypadki, w których musimy jawnie zmienić typ wartości na inny.
88

9-
```smart header="Not talking about objects yet"
10-
In this chapter, we won't cover objects. Instead, we'll study primitives first. Later, after we learn about objects, we'll see how object conversion works in the chapter <info:object-toprimitive>.
11-
```
9+
```smart header="Nie mówimy jeszcze o obiektach"
10+
W tym rozdziale nie zajmujemy się obiektami. Zamiast tego nauczymy się najpierw typów prostych. Później nauczymy się co nieco o obiektach i w rozdziale pt. "<info:object-toprimitive>" zobaczymy, jak działa rzutowanie obiektów.
1211
13-
## String Conversion
12+
## Rzutowanie do typu tekstowego
1413
15-
String conversion happens when we need the string form of a value.
14+
Rzutowanie do typu tekstowego następuje, kiedy potrzebujemy wartości zmiennej w formie tekstowej.
1615
17-
For example, `alert(value)` does it to show the value.
16+
Na przykład, funkcja `alert(value)` rzutuje wyświetlaną wartość do typu tekstowego.
1817
19-
We can also call the `String(value)` function to convert a value to a string:
18+
Możemy również wywołać funkcję `String(value)`, żeby jawnie rzutować wartość na tekst:
2019
2120
```js run
2221
let value = true;
2322
alert(typeof value); // boolean
2423
2524
*!*
26-
value = String(value); // now value is a string "true"
25+
value = String(value); // teraz wartość "true" jest ciągiem znaków
2726
alert(typeof value); // string
2827
*/!*
2928
```
3029

31-
String conversion is mostly obvious. A `false` becomes `"false"`, `null` becomes `"null"`, etc.
30+
Rzutowanie wartości do tekstu jest bardzo przewidywalne. `false` zostaje `"false"`, typ `null` staje się napisem `"null"` itd.
3231

33-
## Numeric Conversion
32+
## Rzutowanie do typu liczbowego
3433

35-
Numeric conversion happens in mathematical functions and expressions automatically.
34+
Rzutowanie do typu liczbowego następuje automatycznie w wyniku matematycznych wyrażeń i funkcji.
3635

37-
For example, when division `/` is applied to non-numbers:
36+
Na przykład przy dzieleniu (`/`) z udziałem wartości nie będących liczbami:
3837

3938
```js run
40-
alert( "6" / "2" ); // 3, strings are converted to numbers
39+
alert( "6" / "2" ); // 3, wartości tekstowe są zamienane na liczby
4140
```
4241

43-
We can use the `Number(value)` function to explicitly convert a `value` to a number:
42+
Możemy użyć funkcji `Number(value)`, aby jawnie rzutować zmienną `value` na liczbę.
4443

4544
```js run
4645
let str = "123";
4746
alert(typeof str); // string
4847

49-
let num = Number(str); // becomes a number 123
48+
let num = Number(str); // staje się typem liczbowym o wartości 123
5049

5150
alert(typeof num); // number
5251
```
5352

54-
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.
53+
Jawne rzutowanie jest zazwyczaj wymagane, gdy chcemy odczytać wartość ze źródła tekstowego, a w kodzie oczekujemy wartości liczbowej.
5554

56-
If the string is not a valid number, the result of such a conversion is `NaN`. For instance:
55+
Jeśli podana wartość tekstowa nie jest prawidłową liczbą, wynikiem konwersji będzie `NaN`. Dla przykładu:
5756

5857
```js run
59-
let age = Number("an arbitrary string instead of a number");
58+
let age = Number("dowolny ciąg znaków zamiast typu liczbowego");
6059

61-
alert(age); // NaN, conversion failed
60+
alert(age); // NaN, rzutowanie nie powiodło się
6261
```
6362

64-
Numeric conversion rules:
63+
Zasady rzutowania do typu liczbowego:
6564

66-
| Value | Becomes... |
67-
|-------|-------------|
65+
| Wartość | Otrzymamy... |
66+
|---------|-------------|
6867
|`undefined`|`NaN`|
6968
|`null`|`0`|
70-
|<code>true&nbsp;and&nbsp;false</code> | `1` and `0` |
71-
| `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`. |
69+
|<code>true&nbsp;i&nbsp;false</code> | `1` i `0` |
70+
| `string` | Białe znaki z początku i końca są usuwane. Jeśli pozostała wartość napisu jest pusta, wynikiem będzie `0`. W przeciwnym wypadku liczba jest odczytywana z tekstu. Wszystkie nieprawidłowe rzutowania dają `NaN`. |
7271

73-
Examples:
72+
Przykłady:
7473

7574
```js run
7675
alert( Number(" 123 ") ); // 123
77-
alert( Number("123z") ); // NaN (error reading a number at "z")
76+
alert( Number("123z") ); // NaN (błąd podczas rzutowania "z" na liczbę)
7877
alert( Number(true) ); // 1
7978
alert( Number(false) ); // 0
8079
```
8180

82-
Please note that `null` and `undefined` behave differently here: `null` becomes zero while `undefined` becomes `NaN`.
81+
Zauważ, że `null` i `undefined` zachowują się inaczej. `null` jest konwertowany do zera, a `undefined` do `NaN`.
8382

84-
````smart header="Addition '+' concatenates strings"
85-
Almost all mathematical operations convert values to numbers. A notable exception is addition `+`. If one of the added values is a string, the other one is also converted to a string.
83+
````smart header="Operator '+' łączy ciągi znaków"
84+
Większość operacji matematycznych rzutuje wartości do typu liczbowego. Jedynym wyjątkiem jest `+`. Jeśli jedna z dodawanych wartości będzie typu tekstowego, druga również jest rzutowana do typu tekstowego.
8685
87-
Then, it concatenates (joins) them:
86+
Wtedy następuje ich konkatenacja (połączenie):
8887
8988
```js run
90-
alert( 1 + '2' ); // '12' (string to the right)
91-
alert( '1' + 2 ); // '12' (string to the left)
89+
alert( 1 + '2' ); // '12' (tekst po prawej)
90+
alert( '1' + 2 ); // '12' (tekst po lewej)
9291
```
9392
94-
This only happens when at least one of the arguments is a string. Otherwise, values are converted to numbers.
93+
Dzieje się tak w sytuacji, gdy przynajmniej jeden z argumentów jest typu tekstowego. W przeciwnym wypadku wartości są rzutowane na typ liczbowy.
9594
````
9695

97-
## Boolean Conversion
96+
## Rzutowanie do typu logicznego
9897

99-
Boolean conversion is the simplest one.
98+
Rzutowania do typu logicznego są najprostsze.
10099

101-
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)`.
100+
Zachodzą w operacjach logicznych (później poznamy instrukcje warunkowe i inne podobne rzeczy), ale także mogą zostać wywołane z użyciem funkcji `Boolean(value)`.
102101

103-
The conversion rule:
102+
Zasada rzutowania:
104103

105-
- Values that are intuitively "empty", like `0`, an empty string, `null`, `undefined`, and `NaN`, become `false`.
106-
- Other values become `true`.
104+
- Wartości "puste", np. `0`, pusty ciąg znaków, `null`, `undefined` i `NaN`, są konwertowane do `false`.
105+
- Inne wartości są konwertowane do `true`.
107106

108-
For instance:
107+
Na przykład:
109108

110109
```js run
111110
alert( Boolean(1) ); // true
112111
alert( Boolean(0) ); // false
113112

114-
alert( Boolean("hello") ); // true
113+
alert( Boolean("witaj") ); // true
115114
alert( Boolean("") ); // false
116115
```
117116

118-
````warn header="Please note: the string with zero `\"0\"` is `true`"
119-
Some languages (namely PHP) treat `"0"` as `false`. But in JavaScript, a non-empty string is always `true`.
117+
````warn header="Miej na uwadze, że tekst zawierający cyfrę 0 (`\"0\"`) da `true`"
118+
Niektóre języki (np. PHP) traktują `"0"` jako `false`. Ale w JavaScripcie każdy niepusty ciąg znaków daje zawsze `true`.
120119

121120
```js run
122121
alert( Boolean("0") ); // true
123-
alert( Boolean(" ") ); // spaces, also true (any non-empty string is true)
122+
alert( Boolean(" ") ); // spacje, również true (każdy niepusty ciąg znaków daje true)
124123
```
125124
````
126125
127-
## Summary
126+
## Podsumowanie
128127
129-
The three most widely used type conversions are to string, to number, and to boolean.
128+
Trzy najczęściej używane rzutowania dotyczą ciągów znaków, liczb i typów logicznych.
130129
131-
**`String Conversion`** -- Occurs when we output something. Can be performed with `String(value)`. The conversion to string is usually obvious for primitive values.
130+
**`Rzutowanie do typu tekstowego`** -- Zachodzi, gdy coś wypisujemy na ekranie. Może zajść również przy użyciu funkcji `String(value)`. Wynik rzutowania do tekstu jest zazwyczaj oczywisty dla typów prostych.
132131
133-
**`Numeric Conversion`** -- Occurs in math operations. Can be performed with `Number(value)`.
132+
**`Rzutowanie do typu liczbowego`** -- Zachodzi w operacjach matematycznych. Może zajść również przy użyciu funkcji `Number(value)`.
134133
135-
The conversion follows the rules:
134+
Rzutowanie to jest zgodne z zasadami:
136135
137-
| Value | Becomes... |
136+
| Wartość | Otrzymamy... |
138137
|-------|-------------|
139138
|`undefined`|`NaN`|
140139
|`null`|`0`|
141140
|<code>true&nbsp;/&nbsp;false</code> | `1 / 0` |
142-
| `string` | The string is read "as is", whitespaces from both sides are ignored. An empty string becomes `0`. An error gives `NaN`. |
141+
| `string` | Tekst jest odczytywany "jak leci", białe znaki na obydwóch końcach są ignorowane. Pusty ciąg znaków staje się `0`. Błąd konwersji zwraca `NaN`.|
143142
144-
**`Boolean Conversion`** -- Occurs in logical operations. Can be performed with `Boolean(value)`.
143+
**`Rzutowanie do typu logicznego`** -- Zachodzi w operacjach logicznych. Może zajść również przy użyciu funkcji `Boolean(value)`.
145144
146-
Follows the rules:
145+
Rzutowanie jest zgodne z zasadami:
147146
148-
| Value | Becomes... |
147+
| Wartość | Otrzymamy... |
149148
|-------|-------------|
150149
|`0`, `null`, `undefined`, `NaN`, `""` |`false`|
151-
|any other value| `true` |
150+
|Każda inna wartość| `true` |
152151
153152
154-
Most of these rules are easy to understand and memorize. The notable exceptions where people usually make mistakes are:
153+
Większość z tych zasad jest łatwa do zrozumienia i zapamiętania. Warte uwagi, najczęściej popełnianie błędy to:
155154
156-
- `undefined` is `NaN` as a number, not `0`.
157-
- `"0"` and space-only strings like `" "` are true as a boolean.
155+
- `undefined` rzutowane na typ liczbowy daje `NaN`, a nie `0`.
156+
- `"0"` i spacja w ciągu znaków np. `" "` rzutowane na typ logiczny dadzą `true`.
158157
159-
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.
158+
Nie omówiliśmy tu obiektów. Wrócimy do nich później w rozdziale pt. "<info:object-toprimitive>", gdy już poznamy więcej podstaw JavaScriptu.

1-js/02-first-steps/08-comparison/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ alert( '2' > 1 ); // true, string '2' becomes a number 2
7474
alert( '01' == 1 ); // true, string '01' becomes a number 1
7575
```
7676

77-
For boolean values, `true` becomes `1` and `false` becomes `0`.
77+
For boolean values, `true` becomes `1` and `false` becomes `0`.
7878

7979
For example:
8080

0 commit comments

Comments
 (0)