You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A variable in JavaScript can contain any data. A variable can at one moment be a string and at another be a number:
3
+
Zmienna w JavaScripcie może zawierać różne dane. Zmienna może być w jednej chwili łańcuchem znaków (ang. *string*), a w innym liczbą:
4
4
5
5
```js
6
-
//no error
6
+
//nie ma tu błędów
7
7
let message ="hello";
8
8
message =123456;
9
9
```
10
10
11
-
Programming languages that allow such things are called "dynamically typed", meaning that there are data types, but variables are not bound to any of them.
11
+
Część języków programowania stosuje tak zwane "dynamiczne typowanie", które oznacza, że typy danych zmiennych mogą zmienić się w trakcie działania programu.
12
12
13
-
There are seven basic data types in JavaScript. Here, we'll cover them in general and in the next chapters we'll talk about each of them in detail.
13
+
Wyróżniamy 7 podstawowych typów danych w JavaScripcie. Przedstawimy je teraz ogólnie, w następnych rozdziałach omówimy bardziej szczegółowo.
14
14
15
-
## A number
15
+
## Typ liczbowy
16
16
17
17
```js
18
18
let n =123;
19
19
n =12.345;
20
20
```
21
21
22
-
The*number*type represents both integer and floating point numbers.
22
+
Typ*number*reprezentuje zarówno liczby całkowite, jak i zmiennoprzecinkowe.
23
23
24
-
There are many operations for numbers, e.g. multiplication`*`, division`/`, addition`+`, subtraction`-`, and so on.
24
+
Istnieje wiele operacji na liczbach, np. mnożenie`*`, dzielenie`/`, dodawanie`+`, odejmowanie`-` itd.
25
25
26
-
Besides regular numbers, there are so-called "special numeric values" which also belong to this data type: `Infinity`, `-Infinity` and `NaN`.
26
+
Poza zwykłymi liczbami, wyróżniamy "specjalne wartości liczbowe", które także należą do tego typu danych: `Infinity`, `-Infinity` and `NaN`.
27
27
28
-
-`Infinity`represents the mathematical [Infinity](https://en.wikipedia.org/wiki/Infinity) ∞. It is a special value that's greater than any number.
28
+
-`Infinity`reprezentuje w matematyce [nieskończoność](https://pl.wikipedia.org/wiki/Niesko%C5%84czono%C5%9B%C4%87) ∞. To specjalna wartość, która jest większa niż jakakolwiek inna liczba.
29
29
30
-
We can get it as a result of division by zero:
30
+
Nieskończoność możemy uzyskać w wyniku dzielenia przez 0:
31
31
32
32
```js run
33
33
alert( 1/0 ); // Infinity
34
34
```
35
35
36
-
Or just reference it directly:
36
+
Lub odwołując się do niej bezpośrednio:
37
37
38
38
```js run
39
39
alert( Infinity ); // Infinity
40
40
```
41
-
-`NaN`represents a computational error. It is a result of an incorrect or an undefined mathematical operation, for instance:
41
+
-`NaN`reprezentuje błąd obliczeniowy. Jest wynikiem błędnych bądź niezdefiniowanych działań matematycznych, na przykład:
42
42
43
43
```js run
44
-
alert( "not a number" / 2 ); // NaN, such division is erroneous
44
+
alert( "wartość nieliczbowa" / 2 ); // NaN, takie działanie prowadzi do błędu
45
45
```
46
46
47
-
`NaN` is sticky. Any further operation on `NaN`returns `NaN`:
47
+
Każda operacja z użyciem `NaN`zawsze zwraca `NaN` jako wynik:
48
48
49
49
```js run
50
-
alert( "not a number" / 2 + 5 ); // NaN
50
+
alert( "wartość nieliczbowa" / 2 + 5 ); // NaN
51
51
```
52
52
53
-
So, if there's a `NaN` somewhere in a mathematical expression, it propagates to the whole result.
53
+
Zatem, jeżeli `NaN`znajduje się w wyrażeniu matematycznym, staje się też jego wynikiem końcowym.
54
54
55
-
```smart header="Mathematical operations are safe"
56
-
Doing maths is "safe" in JavaScript. We can do anything: divide by zero, treat non-numeric strings as numbers, etc.
55
+
```smart header="Operacje matematyczne są bezpieczne"
56
+
Przeprowadzanie obliczeń matematycznych w JavaScripcie jest "bezpieczne". Możemy dzielić przez zero, traktować ciągi znaków jako liczby itd.
57
57
58
-
The script will never stop with a fatal error ("die"). At worst, we'll get`NaN`as the result.
58
+
Skrypt nigdy nie zatrzyma się na błędzie krytycznym. W najgorszym wypadku otrzymamy`NaN`jako wynik działania.
59
59
```
60
60
61
-
Special numeric values formally belong to the "number" type. Of course they are not numbers in the common sense of this word.
61
+
Specjalne wartości liczbowe formalnie należą do typu "liczbowego". Oczywiście nie są liczbami w definicji matematycznej.
62
62
63
-
We'll see more about working with numbers in the chapter <info:number>.
63
+
Więcej informacji o pracy z liczbami zawarte jest w rozdziale pt. "<info:number>".
64
64
65
-
## A string
65
+
## Typ tekstowy
66
66
67
-
A string in JavaScript must be surrounded by quotes.
67
+
Ciąg znaków (ang. *string*), zwany także "literałem znakowym" lub "napisem", to typ tekstowy, który zapisujemy przy użyciu cudzysłowów.
68
68
69
69
```js
70
-
let str ="Hello";
71
-
let str2 ='Single quotes are ok too';
72
-
let phrase =`can embed${str}`;
70
+
let str = "Witaj";
71
+
let str2 = 'Można użyć także apostrofów';
72
+
let phrase = `Można dołączyć zmienną ${str}`;
73
73
```
74
74
75
-
In JavaScript, there are 3 types of quotes.
75
+
W JavaScripcie istnieją 3 typy cudzysłowów.
76
76
77
-
1.Double quotes: `"Hello"`.
78
-
2.Single quotes: `'Hello'`.
79
-
3.Backticks: <code>`Hello`</code>.
77
+
1.Cudzysłowy:`"Witaj"`.
78
+
2.Apostrofy:`'Witaj'`.
79
+
3.Grawisy (ang. *backtick*):<code>`Witaj`</code>.
80
80
81
-
Double and single quotes are "simple" quotes. There's no difference between them in JavaScript.
81
+
W JavaScripcie nie ma różnicy między cudzysłowami a apostrofami.
82
82
83
-
Backticks are "extended functionality" quotes. They allow us to embed variables and expressions into a string by wrapping them in `${…}`, for example:
83
+
Grawisy są "rozszerzeniem funkcjonalności" zwykłych apostrofów i cudzysłowów. Pozwalają na dodanie zmiennej i wyrażeń do ciągu znaków poprzez umieszczenie ich wewnątrz `${…}`, przykładowo:
alert( `the result is *!*${1+2}*/!*` ); //the result is 3
91
+
//dołączenie wyrażenia
92
+
alert( `Wynik to *!*${1+2}*/!*` ); //Wynik to 3
93
93
```
94
94
95
-
The expression inside `${…}`is evaluated and the result becomes a part of the string. We can put anything in there: a variable like`name` or an arithmetical expression like `1 + 2` or something more complex.
95
+
Wyrażenie wewnątrz `${…}`zostaje dołączone do części ciągu znaków. Do wyrażenia możemy wstawić cokolwiek: zmienną, na przykład`name`, lub wyrażenie arytmetyczne, jak na przykład `1 + 2`, lub coś bardziej złożonego.
96
96
97
-
Please note that this can only be done in backticks. Other quotes don't have this embedding functionality!
97
+
Warto odnotować, że taki efekt można osiągnąć jedynie przy użyciu grawisów (``). Apostrofy i cudzysłowy nie mają takich możliwości.
98
98
```js run
99
-
alert( "the result is ${1 + 2}" ); //the result is ${1 + 2} (double quotes do nothing)
99
+
alert( "Wynik to ${1 + 2}" ); //Wynik to ${1 + 2} (cudzysłów traktuje ${…} jako część napisu)
100
100
```
101
101
102
-
We'll cover strings more thoroughly in the chapter <info:string>.
102
+
Więcej o ciągach znaków można przeczytać w rozdziale pt. "<info:string>".
103
103
104
-
```smart header="There is no *character* type."
105
-
In some languages, there is a special "character" type for a single character. For example, in the C language and in Java it is `char`.
104
+
```smart header="JavaScript nie posiada typu *znakowego*."
105
+
W niektórych językach istnieje specjalny typ "znakowy", używany do przechowywania pojedynczych znaków. Przykładowo, w językach C i Java możemy użyć typu `char`.
106
106
107
-
In JavaScript, there is no such type. There's only one type: `string`. A string may consist of only one character or many of them.
107
+
W JavaScripcie nie ma takiego typu. Mamy do dyspozycji jedynie `string`. Ciąg znaków może być pusty albo zawierać jeden i więcej znaków.
108
108
```
109
109
110
-
## A boolean (logical type)
110
+
## Typ logiczny
111
111
112
-
The boolean type has only two values: `true`and `false`.
112
+
Typ logiczny (ang. *boolean*) posiada dwie wartości: `true`(prawda) lub `false` (fałsz).
113
113
114
-
This type is commonly used to store yes/no values:`true`means "yes, correct", and`false`means "no, incorrect".
114
+
Ty logiczny jest najczęsciej używany do przechowywania wartości pokroju "tak/nie", gdzie`true`to "tak, prawda", a`false`oznacza "nie, nieprawda".
115
115
116
-
For instance:
116
+
Na przykład:
117
117
118
118
```js
119
-
let nameFieldChecked =true; //yes, name field is checked
120
-
let ageFieldChecked =false; //no, age field is not checked
119
+
let nameFieldChecked =true; //tak, pole "name" jest zaznaczone (ang. *checked*)
120
+
let ageFieldChecked =false; //nie, pole "age" nie jest zaznaczone
121
121
```
122
122
123
-
Boolean values also come as a result of comparisons:
123
+
Wartości typu logicznego mogą być wynikiem porównania:
124
124
125
125
```js run
126
126
let isGreater =4>1;
127
127
128
-
alert( isGreater ); // true (the comparison result is "yes")
We'll cover booleans more deeply in the chapter <info:logical-operators>.
131
+
Więcej informacji o typie logicznym można znaleźć w rozdziale pt. "<info:logical-operators>".
132
132
133
-
## The "null" value
133
+
## Wartość "null"
134
134
135
-
The special `null`value does not belong to any of the types described above.
135
+
Wartość `null`(zwana także "literałem pustym") nie należy do żadnego z wyżej wymienionych typów danych.
136
136
137
-
It forms a separate type of its own which contains only the `null` value:
137
+
Literał pusty posiada własny typ, którego jedyną wartością jest `null`:
138
138
139
139
```js
140
140
let age =null;
141
141
```
142
142
143
-
In JavaScript,`null`is not a "reference to a non-existing object" or a "null pointer" like in some other languages.
143
+
W JavaScripcie`null`nie odnosi się do "nieistniejącego obiektu" lub "wskaźnika zerowego", jak ma to miejsce w innych językach programowania.
144
144
145
-
It's just a special value which represents "nothing", "empty" or "value unknown".
145
+
Jest specjalną wartością, która reprezentuje "nic", "brak wartości" lub "nieznaną wartość".
146
146
147
-
The code above states that `age`is unknown or empty for some reason.
147
+
Kod powyżej zakłada, że wartość zmiennej `age`jest pusta bądź nieznana z jakiegoś powodu.
148
148
149
-
## The "undefined" value
149
+
## Wartość "undefined"
150
150
151
-
The special value `undefined`also stands apart. It makes a type of its own, just like`null`.
151
+
Wartość `undefined`(pol. *niezdefiniowana*), podobnie jak`null`, posiada swój własny typ.
152
152
153
-
The meaning of `undefined`is "value is not assigned".
153
+
Wartość `undefined`oznacza, że "wartość zmiennej nie jest przypisana"
154
154
155
-
If a variable is declared, but not assigned, then its value is`undefined`:
155
+
W przypadku zadeklarowania zmiennej bez przypisania do niej konkretnej wartości, domyślna wartość to`undefined`:
156
156
157
157
```js run
158
158
let x;
159
159
160
-
alert(x); //shows "undefined"
160
+
alert(x); //wyświetla "undefined"
161
161
```
162
162
163
-
Technically, it is possible to assign `undefined`to any variable:
163
+
W zasadzie możliwe jest przypisanie `undefined`do zmiennej:
164
164
165
165
```js run
166
166
let x =123;
@@ -170,28 +170,28 @@ x = undefined;
170
170
alert(x); // "undefined"
171
171
```
172
172
173
-
...But we don't recommend doing that. Normally, we use `null` to assign an "empty" or "unknown" value to a variable, and we use `undefined`for checks like seeing if a variable has been assigned.
173
+
... Jednak nie zalecamy tworzenia zmiennych o wartości `undefined`. Zazwyczaj używamy `null` dla zmiennych bez wartości, `undefined`przydaje się przy sprawdzaniu czy zmienna została przypisana do jakiejś wartości.
174
174
175
-
## Objects and Symbols
175
+
## Obiekty i symbole
176
176
177
-
The`object`type is special.
177
+
Typ`object`jest jedyny w swoim rodzaju.
178
178
179
-
All other types are called "primitive" because their values can contain only a single thing (be it a string or a number or whatever). In contrast, objects are used to store collections of data and more complex entities. We'll deal with them later in the chapter <info:object> after we learn more about primitives.
179
+
Wszystkie inne typy zwane są "prostymi" (ang. *primitive*), ponieważ ich wartości mogą przechowywać tylko jedną rzecz (może to być ciąg znaków, liczba, typ logiczny itd.). W przeciwieństwie do typów prostych, obiekty używane są do przechowywania większych kolekcji danych. Więcej o obiektach omówimy poźniej w rozdziale pt. "<info:object>", po wcześniejszym omówieniu typów prostych.
180
180
181
-
The`symbol`type is used to create unique identifiers for objects. We mention it here for completeness, but we'll study it after objects.
181
+
Typ`symbol`jest używany do tworzenia unikalnych identyfikatorów dla obiektów. Wspominamy o nim tylko dla kompletności tego rozdziału, niemniej zdecydowanie lepiej jest poznać ten typ po zrozumieniu samych obiektów.
182
182
183
-
## The typeof operator[#type-typeof]
183
+
## Operator "typeof"[#type-typeof]
184
184
185
-
The`typeof`operator returns the type of the argument. It's useful when we want to process values of different types differently or just want to do a quick check.
185
+
Operator`typeof`zwraca typ danego argumentu. Jest użyteczny, gdy chcemy przetworzyć wartości różnych typów lub sprawdzić sam typ.
186
186
187
-
It supports two forms of syntax:
187
+
Występują dwa sposoby na użycie tego operatora:
188
188
189
-
1.As an operator: `typeof x`.
190
-
2.As a function:`typeof(x)`.
189
+
1.Jako operator: `typeof x`.
190
+
2.Jako funkcja`typeof(x)`.
191
191
192
-
In other words, it works with parentheses or without them. The result is the same.
192
+
Innymi słowy, bez względu na to, czy użyjemy nawiasów czy nie - wynik jest ten sam.
193
193
194
-
The call to `typeof x`returns a string with the type name:
194
+
Wywołanie `typeof x`zwraca ciąg znaków z nazwą typu sprawdzanej zmiennej:
The last three lines may need additional explanation:
220
+
Ostatnie trzy linijki wymagają dodatkowego wyjaśnienia.
221
221
222
-
1.`Math`is a built-in object that provides mathematical operations. We will learn it in the chapter <info:number>. Here, it serves just as an example of an object.
223
-
2.The result of `typeof null`is `"object"`. That's wrong. It is an officially recognized error in `typeof`, kept for compatibility. Of course, `null` is not an object. It is a special value with a separate type of its own. So, again, this is an error in the language.
224
-
3.The result of `typeof alert`is`"function"`, because`alert`is a function. We'll study functions in the next chapters where we'll also see that there's no special "function" type in JavaScript. Functions belong to the object type. But`typeof`treats them differently, returning`"function"`. That's not quite correct, but very convenient in practice.
222
+
1.`Math`jest wbudowanym obiektem, który daje dostęp do operacji matematycznych. Dowiemy się o nim więcej w rozdziale pt. "<info:number>". W tym przypadku posłużył jako przykład obiektu.
223
+
2.Wynikiem wywołania `typeof null`jest `object`. Jest to znany błąd związany z `typeof`, nie został on jednak poprawiony ze względu na wsteczną kompatybilność. Oczywiście `null` nie jest obiektem - posiada własny typ.
224
+
3.Wynikiem wywołania `typeof alert`jest`"function"` ze względu na to, że`alert`jest po prostu funkcją. O funkcjach napisaliśmy więcej w następnych rozdziałach, gdzie zauważamy, że tak naprawdę nie ma typu "function" w JavaScripcie. Funkcje należą do typu "object". Jednak`typeof`traktuje funkcje inaczej, zwracając`"function"`, co nie jest do końca poprawne, lecz bardzo wygodne w praktyce.
225
225
226
226
227
-
## Summary
227
+
## Podsumowanie
228
228
229
-
There are 7 basic data types in JavaScript.
229
+
W JavaScripcie wyróżniamy 7 podstawowych typów danych.
230
230
231
-
-`number`for numbers of any kind: integer or floating-point.
232
-
-`string`for strings. A string may have one or more characters, there's no separate single-character type.
233
-
-`boolean`for`true`/`false`.
234
-
-`null`for unknown values -- a standalone type that has a single value`null`.
235
-
-`undefined`for unassigned values -- a standalone type that has a single value`undefined`.
236
-
-`object`for more complex data structures.
237
-
-`symbol`for unique identifiers.
231
+
-`number`dla wszystkich liczb: całkowitych lub zmiennoprzecinkowych.
232
+
-`string`dla ciągów znaków. Może być pusty albo zawierać jeden czy więcej znaków; nie ma oddzielnego typu dla pojedynczego znaku.
233
+
-`boolean`dla`true`/`false` (prawda/fałsz).
234
+
-`null`dla pustych wartości -- autonomiczny typ, który posiada jedną wartość:`null`.
235
+
-`undefined`dla niezdefiniowanych wartości -- autonomiczny typ, który posiada jedną wartość:`undefined`.
236
+
-`object`dla bardziej złożonych struktur danych.
237
+
-`symbol`dla unikalnych identyfikatorów.
238
238
239
-
The`typeof`operator allows us to see which type is stored in a variable.
239
+
Operator`typeof`pozwala na sprawdzenie typu zmiennej.
240
240
241
-
-Two forms: `typeof x`or`typeof(x)`.
242
-
-Returns a string with the name of the type, like`"string"`.
243
-
-For `null`returns`"object"` -- this is an error in the language, it's not actually an object.
241
+
-Istnieją dwie formy: `typeof x`lub`typeof(x)`.
242
+
-Zwraca ciąg znaków z nazwą danego typu, na przykład`"string"`.
243
+
-Dla wartości `null`zwraca`"object"` -- jest to błąd w JavaScripcie, ponieważ `null` nie jest typu "object".
244
244
245
-
In the next chapters, we'll concentrate on primitive values and once we're familiar with them, we'll move on to objects.
245
+
W następnych rozdziałach skupimy się na typach prostych, a gdy już będziemy z nimi zaznajomieni, poznamy obiekty.
0 commit comments