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
Copy file name to clipboardExpand all lines: 1-js/02-first-steps/07-operators/article.md
+76-76Lines changed: 76 additions & 76 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,139 +30,139 @@ Prieš tęsiant, išsiaiškinkime įprastinę terminologiją.
30
30
31
31
## Eilutės sujungimas, binarinis +
32
32
33
-
Now, let's see special features of JavaScript operators that are beyond school arithmetics.
33
+
Dabar susipažinkime su ypatingomis JavaScript savybėmis, kurios pranoksta mokyklos aritmetiką.
34
34
35
-
Usually, the plus operator `+` sums numbers.
35
+
Dažniausiai operatorius pliusas `+`sumuoja skaičius.
36
36
37
-
But, if the binary `+` is applied to strings, it merges (concatenates) them:
37
+
Bet jeigu binarinis `+`pritaikomas eilutėms, pliusas jas sulieja (sujungia):
38
38
39
39
```js
40
40
let s = "my" + "string";
41
41
alert(s); // mystring
42
42
```
43
43
44
-
Note that if one of the operands is a string, the other one is converted to a string too.
44
+
Atkreipkite dėmesį, jeigu vienas iš operandų yra eilutė, tai kitas taip pat paverčiamas eilute.
45
45
46
-
For example:
46
+
Pavyzdžiui:
47
47
48
48
```js run
49
49
alert( '1' + 2 ); // "12"
50
50
alert( 2 + '1' ); // "21"
51
51
```
52
52
53
-
See, it doesn't matter whether the first operand is a string or the second one. The rule is simple:if either operand is a string, the other one is converted into a string as well.
53
+
Kaip matote nėra svarbu ar pirmasis operandas ar antrasis yra eilutė. Taisyklė paprasta: jeigu bent vienas operandas yra eilutė, kitas taip pat paverčiamas eilute.
54
54
55
-
However, note that operations run from left to right. If there are two numbers followed by a string, the numbers will be added before being converted to a string:
55
+
Tačiau pastebėkite, kad operacijos vyksta iš kairės į dešinę. Jeigu pirmiau yra du numeriai, kuriuos seka eilutė, numeriai bus susumuoti ir tik tada paversti į eilutę:
56
56
57
57
58
58
```js run
59
59
alert(2 + 2 + '1' ); // "41" and not "221"
60
60
```
61
61
62
-
String concatenation and conversion is a special feature of the binary plus `+`. Other arithmetic operators work only with numbers and always convert their operands to numbers.
62
+
Eilutės sujungimas ir konversija yra ypatinga binarinio pliuso savybė `+`. Kiti aritmetiniai operatoriai veikia tik su skaičiais ir visada konvertuoja savo operandus į skaičius.
63
63
64
-
For instance, subtraction and division:
64
+
Pavyzdžiui, atimtis ir dalyba:
65
65
66
66
```js run
67
67
alert( 2 - '1' ); // 1
68
68
alert( '6' / '2' ); // 3
69
69
```
70
70
71
-
## Numeric conversion, unary+
71
+
## Skaitinė konversija, unarinis+
72
72
73
-
The plus `+`exists in two forms: the binary form that we used above and the unary form.
73
+
Pliusas `+`egzistuoja dviejose formose: binarinėje formoje, kurią matėme aukščiau ir unarinėje formoje.
74
74
75
-
The unary plus or, in other words, the plus operator `+` applied to a single value, doesn't do anything to numbers. But if the operand is not a number, the unary plus converts it into a number.
75
+
Unarinis pliusas arba kitaip sumos operatorius `+`, pritaikytas vienetinei vertei, nieko nedaro skaičiams. Bet jeigu operandas nėra skaičius, unarinis pliusas paverčia jį skaičiumi.
76
76
77
-
For example:
77
+
Pavyzdžiui:
78
78
79
79
```js run
80
-
// No effect on numbers
80
+
// Jokio efekto skaičiams
81
81
let x = 1;
82
82
alert( +x ); // 1
83
83
84
84
let y = -2;
85
85
alert( +y ); // -2
86
86
87
87
*!*
88
-
// Converts non-numbers
88
+
// Paverčia ne skaičius į skaičius
89
89
alert( +true ); // 1
90
90
alert( +"" ); // 0
91
91
*/!*
92
92
```
93
93
94
-
It actually does the same thing as `Number(...)`, but is shorter.
94
+
Tiesą sakant jis daro tą patį kaip`Number(...)`, bet daug trumpesne versija.
95
95
96
-
The need to convert strings to numbers arises very often. For example, if we are getting values from HTML form fields, they are usually strings. What if we want to sum them?
96
+
Poreikis eilutes pakeisti skaičiais pasitaiko labai dažnai. Pavyzdžiui kai gauname vertes iš HTMLformų laukelių, jie dažniausiai yra eilutės. Kas jeigu mums reikia jas susumuoti?
97
97
98
-
The binary plus would add them as strings:
98
+
Binarinis pliusas juos sujungtų į eilutę:
99
99
100
100
```js run
101
101
let apples = "2";
102
102
let oranges = "3";
103
103
104
-
alert( apples + oranges ); // "23", the binary plus concatenates strings
From a mathematician's standpoint, the abundance of pluses may seem strange. But from a programmer's standpoint, there's nothing special: unary pluses are applied first, they convert strings to numbers, and then the binary plus sums them up.
122
+
Iš matematiko perspektyvos gausybė pliusų gali atrodyti keistai. Bet iš programuotojo pozicijos, tai nėra nieko ypatingo: unariniai pliusai pritaikomi pirmi, jie pakeičia eilutes į skaičius, o tada binarinis pliusas juos susumuoja.
123
123
124
-
Why are unary pluses applied to values before the binary ones? As we're going to see, that's because of their *higher precedence*.
124
+
Kodėl unariniai pliusai pritaikomi vertėms pirmiau nei binarinis? Kaip pamatysime vėliau, taip nutinka dėl jų *aukštesnio prioriteto* (ang. *higher precedence*).
125
125
126
-
## Operator precedence
126
+
## Operatorių pirmenybė
127
127
128
-
If an expression has more than one operator, the execution order is defined by their *precedence*, or, in other words, the default priority order of operators.
128
+
Jeigu išraiška turi daugiau operatorių nei vieną, jų vykdymo tvarką yra nustatoma pagal jų *pirmenybę*, arba kitaip sakant, iš ankto numatytąjį operatorių prioritetą.
129
129
130
-
From school, we all know that the multiplication in the expression `1 + 2 * 2`should be calculated before the addition. That's exactly the precedence thing. The multiplication is said to have *a higher precedence* than the addition.
130
+
Dar mokykloje sužinojome, kad daugyba tokioje išraiškoje `1 + 2 * 2`turėtų būti suskaičiuojama prieš sudėtį. Tai ir yra pirmenybė. Yra tariama, kad daugyba turi *aukštesnį prioritetą* negu sudėtis.
131
131
132
-
Parentheses override any precedence, so if we're not satisfied with the default order, we can use them to change it. For example, write`(1 + 2) * 2`.
132
+
Skliausteliai perrašo bet kokią pirmenybę, tad jeigu mums netinka numatyta tvarka, mes galime juos panaudoti, kad tai pakeistume. Pavyzdžiui rašydami`(1 + 2) * 2`.
133
133
134
-
There are many operators inJavaScript. Every operator has a corresponding precedence number. The one with the larger number executes first. If the precedence is the same, the execution order is from left to right.
134
+
JavaScript turi daug operatorių. Kiekvienas operatorius turi atitinkamą pirmenybės numerį. Tas kas turi aukštenį numerį įvykdomas pirmiau. Jeigu prioritetas vienodas, įvykdymo eilė yra iš kairės į dešinę.
135
135
136
-
Here's an extract from the [precedence table](https://developer.mozilla.org/en/JavaScript/Reference/operators/operator_precedence) (you don't need to remember this, but note that unary operators are higher than corresponding binary ones):
136
+
Štai ištrauka iš [pirmenybės lenteltės](https://developer.mozilla.org/en/JavaScript/Reference/operators/operator_precedence) (jums nereikia šito prisiminti, bet žinokite, kad unariniai operatoriai yra aukščiau už atitinkamus binarinius operatorius):
137
137
138
-
|Precedence|Name|Sign|
138
+
|Pirmenybė|Pavadinimas|Ženklas|
139
139
|------------|------|------|
140
140
|...|...|...|
141
-
|16|unary plus|`+`|
142
-
|16|unary negation|`-`|
143
-
|14|multiplication|`*`|
144
-
|14|division|`/`|
145
-
|13|addition|`+`|
146
-
|13|subtraction|`-`|
141
+
|16|unarinis pliusas|`+`|
142
+
|16|unarinis minusas|`-`|
143
+
|14|daugyba|`*`|
144
+
|14|dalyba|`/`|
145
+
|13|sudėtis|`+`|
146
+
|13|atimtis|`-`|
147
147
|...|...|...|
148
-
|3|assignment|`=`|
148
+
|3|asignavimas|`=`|
149
149
|...|...|...|
150
150
151
-
As we can see, the "unary plus" has a priority of`16`which is higher than the `13`of"addition" (binary plus). That's why, in the expression `"+apples + +oranges"`, unary pluses work before the addition.
151
+
Kaip matome, "unarinis pliusas" turi `16`pirmenybę, kuri yra didesnė nei "sudėties"`13`(binarinis pliusas). Dėl tos priežasties, išraiškoje `"+apples + +oranges"`, unariniai pliusai suveikia pirmiau už sudėtį.
152
152
153
-
## Assignment
153
+
## Asignavimas
154
154
155
-
Let's note that an assignment `=`is also an operator. It is listed in the precedence table with the very low priority of`3`.
155
+
Atkreipkite dėmesį, kad asignavimas (kitaip užduoties operatorius) `=`taip pat yra operatorius. Lentelėje jis įrašytas kaip turintis labai žemą pirmenybę`3`.
156
156
157
-
That's why, when we assign a variable, like `x = 2 * 2 + 1`, the calculations are done first and then the `=` is evaluated, storing the result in `x`.
157
+
Dėl tos priežasties kai mes priskiriame kintamąjį, kaip`x = 2 * 2 + 1`, visų pirma suskaičiuojama vertė ir tik tada `=`yra įvertinamas bei rezultatas patalpinamas į`x`.
158
158
159
159
```js
160
160
let x = 2 * 2 + 1;
161
161
162
162
alert( x ); // 5
163
163
```
164
164
165
-
It is possible to chain assignments:
165
+
Asignavimus įmanoma sujungti į grandinę:
166
166
167
167
```js run
168
168
let a, b, c;
@@ -176,14 +176,14 @@ alert( b ); // 4
176
176
alert( c ); // 4
177
177
```
178
178
179
-
Chained assignments evaluate from right to left. First, the rightmost expression `2 + 2` is evaluated and then assigned to the variables on the left: `c`, `b` and `a`. At the end, all the variables share a single value.
179
+
Sujungti į grandinę asignavimai įvertinami iš dešinės į kairę. Visų pirmą įvykdoma dešiniausiai esanti išraiška `2 + 2`ir priskiriama kintamiesiems esantiems dešinėje:`c`, `b`ir`a`. Galų gale visi kintamieji dalinasi viena verte.
180
180
181
-
````smart header="The assignment operator `\"=\"` returns a value"
182
-
An operator always returns a value. That's obvious for most of them like addition `+`or multiplication`*`. But the assignment operator follows this rule too.
Operatorius visada grąžina vertę. Tai yra akivaizdu tokiems kaip sudėtis `+`arba daugyba`*`. Bet asignavimas taip pat seka šita taisykle.
183
183
184
-
The call `x = value`writes the `value`into`x`*and then returns it*.
184
+
Šaukimas `x = value`įrašo `value`į`x`*ir tada ją grąžina*.
185
185
186
-
Here's a demo that uses an assignment as part of a more complex expression:
186
+
Štai pavyzdys su asignavimu sudėtingesnėje išraiškoje:
187
187
188
188
```js run
189
189
let a = 1;
@@ -197,79 +197,79 @@ alert( a ); // 3
197
197
alert( c ); // 0
198
198
```
199
199
200
-
In the example above, the result of expression `(a = b + 1)` is the value which was assigned to `a` (that is `3`). It is then used for further evaluations.
200
+
Pavyzdyje aukščiau, išraiškos `(a = b + 1)`rezultatas yra vertė, kuri buvo priskirta `a` (tai yra`3`). Vėliau ji naudojama tolesniuose vertinimuose.
201
201
202
-
Funny code, isn't it? We should understand how it works, because sometimes we see it inJavaScript libraries, but shouldn't write anything like that ourselves. Such tricks definitely don't make code clearer or readable.
202
+
Kodas yra juokingas, juk taip? Turėtume suprasti kaip jis veikia, nes kartais tokius dalykus randame JavaScript bibliotekose, bet patys tokių geriau nerašykite. Tokie triukai tikrai nepaverčia kodo aiškesniu ir įskaitomesniu.
203
203
````
204
204
205
-
## Remainder%
205
+
## Liekana%
206
206
207
-
The remainder operator `%`, despite its appearance, is not related to percents.
207
+
Liekanos operatorius `%`, nepaisant jo išvaizdos, jis nėra susijęs su procentais.
208
208
209
-
The result of`a % b`is the remainder of the integer division of`a`by`b`.
209
+
Šios išraiškos `a % b`rezultatas yra liekana po sveikų skaičių dalybos `a`iš`b`.
210
210
211
-
For instance:
211
+
Pavyzdžiui:
212
212
213
213
```js run
214
-
alert( 5 % 2 ); // 1 is a remainder of 5 divided by 2
215
-
alert( 8 % 3 ); // 2 is a remainder of 8 divided by 3
216
-
alert( 6 % 3 ); // 0 is a remainder of 6 divided by 3
214
+
alert( 5 % 2 ); // 1 yra liekana kai 5 padalinami iš 2
215
+
alert( 8 % 3 ); // 2 yra liekana kai 8 padalinami iš 3
216
+
alert( 6 % 3 ); // 0 yra liekana kai 6 padalinami iš 3
217
217
```
218
218
219
-
## Exponentiation**
219
+
## Kėlimas laipsniu**
220
220
221
-
The exponentiation operator`**`is a recent addition to the language.
221
+
Kėlimo laipsniu (ang. exponentiation) operatorius`**`yra naujas kalbos priedas.
222
222
223
-
For a natural number `b`, the result of`a ** b`is `a`multiplied by itself`b`times.
223
+
Natūraliajam skaičiui `b`, kai `a ** b`rezultatas yra `a`padaugintas iš savęs`b`kartus.
224
224
225
-
For instance:
225
+
Pavyzdžiui:
226
226
227
227
```js run
228
228
alert( 2 ** 2 ); // 4 (2 * 2)
229
229
alert( 2 ** 3 ); // 8 (2 * 2 * 2)
230
230
alert( 2 ** 4 ); // 16 (2 * 2 * 2 * 2)
231
231
```
232
232
233
-
The operator works for non-integer numbers as well.
233
+
Operatorius veikia ir su trupmenomis.
234
234
235
-
For instance:
235
+
Pavyzdžiui:
236
236
237
237
```js run
238
-
alert( 4 ** (1/2) ); // 2 (power of 1/2 is the same as a square root, that's maths)
239
-
alert( 8 ** (1/3) ); // 2 (power of 1/3 is the same as a cubic root)
238
+
alert( 4 ** (1/2) ); // 2 (laipsnis 1/2 yra taip pat kaip traukti iš šaknies, tai matematika)
239
+
alert( 8 ** (1/3) ); // 2 (laipsnis 1/3 taip pats kaip kubinė šaknis)
240
240
```
241
241
242
-
## Increment/decrement
242
+
## Padidėjimas/sumažėjimas
243
243
244
244
<!-- Can't use -- in title, because built-in parse turns it into – -->
245
245
246
-
Increasing or decreasing a number by one is among the most common numerical operations.
246
+
Skaičiaus padidinimas arba sumažinimas vienetu yra viena dažniausiai naudojamų įprastinių skaičių operacijų.
247
247
248
-
So, there are special operators for it:
248
+
Tad tam netgi yra specialūs operatoriai:
249
249
250
-
- **Increment** `++` increases a variable by 1:
250
+
- **Padidinimas** `++` (ang. increment) kintamąjį padidina 1-u:
251
251
252
252
```js run no-beautify
253
253
let counter = 2;
254
-
counter++; // works the same as counter = counter + 1, but is shorter
254
+
counter++; // veikia taip pat kaip counter = counter + 1, bet yra trumpesnis
255
255
alert( counter ); // 3
256
256
```
257
-
- **Decrement** `--` decreases a variable by 1:
257
+
- **Sumažinimas** `--` (ang. decrement) sumažina kintamajį 1-u:
258
258
259
259
```js run no-beautify
260
260
let counter = 2;
261
-
counter--; // works the same as counter = counter - 1, but is shorter
261
+
counter--; // veikia taip kaip counter = counter - 1, bet yra trumpesnis
262
262
alert( counter ); // 1
263
263
```
264
264
265
265
```warn
266
-
Increment/decrement can only be applied to variables. Trying to use it on a value like `5++` will give an error.
266
+
Padidinimas/sumažinimas gali būti taikomas tik kintamiesiems. Bandymas jį naudoti vertei kaip pavyzdžiui `5++` sukels klaidą.
267
267
```
268
268
269
-
The operators `++` and `--` can be placed either before or after a variable.
269
+
Operatoriai `++` ir `--` gali būti dedami tiek prieš, tiek ir po kintamojo.
270
270
271
-
- When the operator goes after the variable, it is in "postfix form": `counter++`.
272
-
- The "prefix form" is when the operator goes before the variable: `++counter`.
271
+
- Kai operatorius eina po kintamojo, jis yra "priedėlio formoje" (ang. "postfix form"): `counter++`.
272
+
- "Priešdėlio forma" (ang. "prefix form") yra tada kai operatorius eina prieš kintamąjį: `++counter`.
273
273
274
274
Both of these statements do the same thing: increase `counter` by `1`.
0 commit comments