|
1 | | -# Variables |
| 1 | +# Dəyişənlər |
2 | 2 |
|
3 | | -Most of the time, a JavaScript application needs to work with information. Here are two examples: |
4 | | -1. An online shop -- the information might include goods being sold and a shopping cart. |
5 | | -2. A chat application -- the information might include users, messages, and much more. |
| 3 | +Çox vaxt JavaScript applikasiyalarının məlumatlar ilə işləmələri lazımdır. Məsələn: |
| 4 | +1. onlayn mağaza applikasiyasında satılan mallar və alış-veriş səbəti haqqında məlumatlar lazımdır. |
| 5 | +2. çat applikasiyasında isitfadəçilər, ismarıclar və başqa maddələr haqqında məlumatlar lazımdır. |
6 | 6 |
|
7 | | -Variables are used to store this information. |
| 7 | +Məlumatları saxlamaq üçün dəyişənlərdən istifadə edilir. |
8 | 8 |
|
9 | | -## A variable |
| 9 | +## Dəyişən |
10 | 10 |
|
11 | | -A [variable](https://en.wikipedia.org/wiki/Variable_(computer_science)) is a "named storage" for data. We can use variables to store goodies, visitors, and other data. |
| 11 | +[Dəyişən](https://en.wikipedia.org/wiki/Variable_(computer_science)), məlumat üçün üçün "adlı saxlama yeridir." Dəyişənlər ilə mağaza malları, applikasiyaya ziyarət edənlər və digər məlumatları saxlamaq mümkündür. |
12 | 12 |
|
13 | | -To create a variable in JavaScript, use the `let` keyword. |
| 13 | +JavaScript-də dəyişən yaratmaq üçün `let` açar sözündən istifadə edin. |
14 | 14 |
|
15 | | -The statement below creates (in other words: *declares*) a variable with the name "message": |
| 15 | +Aşağıdakı ifadədə "message" adlı dəyişən yaradılır (digər sözlə, *bildirilir* ingiliscə, *declare*): |
16 | 16 |
|
17 | 17 | ```js |
18 | 18 | let message; |
19 | 19 | ``` |
20 | 20 |
|
21 | | -Now, we can put some data into it by using the assignment operator `=`: |
| 21 | +İndi, biz `=` təyinat operatorundan istifadə edərək bu dəyişənə məlumat təyin edə bilərik: |
22 | 22 |
|
23 | 23 | ```js |
24 | 24 | let message; |
25 | 25 |
|
26 | 26 | *!* |
27 | | -message = 'Hello'; // store the string |
| 27 | +message = 'Salam'; // Mətni saxla |
28 | 28 | */!* |
29 | 29 | ``` |
30 | 30 |
|
31 | | -The string is now saved into the memory area associated with the variable. We can access it using the variable name: |
| 31 | +İndi, mətn dəyəri dəyişənə bağlı olan yaddaş sahəsində saxlanılır. Biz, dəyişənin adından istifadə edərək bu dəyəri oxuya bilərik: |
32 | 32 |
|
33 | 33 | ```js run |
34 | 34 | let message; |
35 | | -message = 'Hello!'; |
| 35 | +message = 'Salam!'; |
36 | 36 |
|
37 | 37 | *!* |
38 | | -alert(message); // shows the variable content |
| 38 | +alert(message); // dəyişənin kontentini göstər |
39 | 39 | */!* |
40 | 40 | ``` |
41 | 41 |
|
42 | | -To be concise, we can combine the variable declaration and assignment into a single line: |
| 42 | +Yığcam kod yazmaq üçün biz dəyişənin yaranmasını və təyinatını bir sətirdə birləşdirə bilərik: |
43 | 43 |
|
44 | 44 | ```js run |
45 | | -let message = 'Hello!'; // define the variable and assign the value |
| 45 | +let message = 'Salam!'; // Dəyişəni müəyyənləşdir və dəyəri təyin et |
46 | 46 |
|
47 | | -alert(message); // Hello! |
| 47 | +alert(message); // Salam! |
48 | 48 | ``` |
49 | 49 |
|
50 | | -We can also declare multiple variables in one line: |
| 50 | +Biz, həmçinin bir neçə dəyişəni eyni sətirdə müəyyənləşdirə bilərik: |
51 | 51 |
|
52 | 52 | ```js no-beautify |
53 | | -let user = 'John', age = 25, message = 'Hello'; |
| 53 | +let user = 'Orxan', age = 25, message = 'Salam'; |
54 | 54 | ``` |
55 | 55 |
|
56 | | -That might seem shorter, but we don't recommend it. For the sake of better readability, please use a single line per variable. |
| 56 | +Bunun daha qısa olmasına baxmayaraq biz belə kod yazmağı tövsiyyə etmirik. Oxunaqlığı çoxaltmaq üçün hər dəyişən üçün ayrı sətir işlədin. |
57 | 57 |
|
58 | | -The multiline variant is a bit longer, but easier to read: |
| 58 | +Çox sətirli variantın biraz uzun olmasına baxmayaraq bunu oxumaq daha asandır: |
59 | 59 |
|
60 | 60 | ```js |
61 | | -let user = 'John'; |
| 61 | +let user = 'Orxan'; |
62 | 62 | let age = 25; |
63 | | -let message = 'Hello'; |
| 63 | +let message = 'Salam'; |
64 | 64 | ``` |
65 | 65 |
|
66 | | -Some people also define multiple variables in this multiline style: |
| 66 | +Bəzi proqramçılar bir neçə dəyişəni aşağıdakı formada: |
67 | 67 | ```js no-beautify |
68 | | -let user = 'John', |
| 68 | +let user = 'Orxan', |
69 | 69 | age = 25, |
70 | | - message = 'Hello'; |
| 70 | + message = 'Salam'; |
71 | 71 | ``` |
72 | 72 |
|
73 | | -...Or even in the "comma-first" style: |
| 73 | +...vı ya "vergül-birinci" stilində də müəyyənləşdirirlər: |
74 | 74 |
|
75 | 75 | ```js no-beautify |
76 | | -let user = 'John' |
| 76 | +let user = 'Orxan' |
77 | 77 | , age = 25 |
78 | | - , message = 'Hello'; |
| 78 | + , message = 'Salam'; |
79 | 79 | ``` |
80 | 80 |
|
81 | | -Technically, all these variants do the same thing. So, it's a matter of personal taste and aesthetics. |
| 81 | +Texniki olaraq yuxarıdakı bütün variantlar eyni nəticəni verəcək. Bu səbəbdən, burada vacib olan proqramçıların şəxsi zövqüdür. |
82 | 82 |
|
83 | 83 |
|
84 | | -````smart header="`var` instead of `let`" |
85 | | -In older scripts, you may also find another keyword: `var` instead of `let`: |
| 84 | +````smart header="`let` əvəzinə `var`" |
| 85 | +Siz, köhnə skriptlərdə `let` əvəzinə `var` açar sözünün işlədildiyini görə bilərsiniz: |
86 | 86 |
|
87 | 87 | ```js |
88 | | -*!*var*/!* message = 'Hello'; |
| 88 | +*!*var*/!* message = 'Salam'; |
89 | 89 | ``` |
90 | 90 |
|
91 | | -The `var` keyword is *almost* the same as `let`. It also declares a variable, but in a slightly different, "old-school" way. |
| 91 | +`var` dəyişəni *az qala* `let` ilə eynidir. Bu açar sözü dəyişənin daha "köhnə" üsul ilə müəyyənləşdirir. |
92 | 92 |
|
93 | | -There are subtle differences between `let` and `var`, but they do not matter for us yet. We'll cover them in detail in the chapter <info:var>. |
| 93 | +`let` və `var` arasında olan hiss edilməyən fərqlər var. Lakin, indi bu fərqlər bizi maralandırmır. Biz, bu fərqlər haqqında <info:var> bölməsində detallı danışacağıq. |
94 | 94 | ```` |
95 | 95 |
|
96 | | -## A real-life analogy |
| 96 | +## Real dünyada analogiya |
97 | 97 |
|
98 | | -We can easily grasp the concept of a "variable" if we imagine it as a "box" for data, with a uniquely-named sticker on it. |
| 98 | +"Dəyişən" konsepsiyasını yaxşı anlamaq üçün bunun, üzərində unikal adlı etiketi olan məlumatlar "qutusu" olduğunu fikirləşin. |
99 | 99 |
|
100 | | -For instance, the variable `message` can be imagined as a box labeled `"message"` with the value `"Hello!"` in it: |
| 100 | +Məsələn, `message` dəyişəni daxilində "Salam!" dəyəri olan və `"message"` adı ilə etiketlənən qutudur: |
101 | 101 |
|
102 | 102 |  |
103 | 103 |
|
104 | | -We can put any value in the box. |
| 104 | +Biz qutuda istənilən dəyəri yerləşdirə bilərik. |
105 | 105 |
|
106 | | -We can also change it as many times as we want: |
| 106 | +Əlavə olaraq, biz bu dəyəri istədiyimiz qədər dəyişə bilərik: |
107 | 107 | ```js run |
108 | 108 | let message; |
109 | 109 |
|
110 | | -message = 'Hello!'; |
| 110 | +message = 'Salam!'; |
111 | 111 |
|
112 | | -message = 'World!'; // value changed |
| 112 | +message = 'Dünya!'; // dəyər dəyişdi |
113 | 113 |
|
114 | 114 | alert(message); |
115 | 115 | ``` |
116 | 116 |
|
117 | | -When the value is changed, the old data is removed from the variable: |
| 117 | +Dəyər dəyişdikdə dəyişəndə olan köhnə məlumat silinir: |
118 | 118 |
|
119 | 119 |  |
120 | 120 |
|
121 | | -We can also declare two variables and copy data from one into the other. |
| 121 | +Əlavə olaraq, biz iki dəyişən yaradıb birinin məlumatını o birisinə kopiyalaya bilərik. |
122 | 122 |
|
123 | 123 | ```js run |
124 | | -let hello = 'Hello world!'; |
| 124 | +let hello = 'Salam dünya!'; |
125 | 125 |
|
126 | 126 | let message; |
127 | 127 |
|
128 | 128 | *!* |
129 | | -// copy 'Hello world' from hello into message |
| 129 | +// 'Salam dünya' dəyərini hello dəyişənindən message dəyişəninə kopiyala |
130 | 130 | message = hello; |
131 | 131 | */!* |
132 | 132 |
|
133 | | -// now two variables hold the same data |
134 | | -alert(hello); // Hello world! |
135 | | -alert(message); // Hello world! |
| 133 | +// indi, hər iki dəyişəndə eyni dəyər saxlanılır |
| 134 | +alert(hello); // Salam dünya! |
| 135 | +alert(message); // Salam dünya! |
136 | 136 | ``` |
137 | 137 |
|
138 | | -```smart header="Functional languages" |
139 | | -It's interesting to note that there exist [functional](https://en.wikipedia.org/wiki/Functional_programming) programming languages, like [Scala](http://www.scala-lang.org/) or [Erlang](http://www.erlang.org/) that forbid changing variable values. |
| 138 | +```smart header="Funksional dillər" |
| 139 | +Nəzərinizə çatdırmaq istəyirik ki, [Scala](http://www.scala-lang.org/) və [Erlang](http://www.erlang.org/) kimi [funksional](https://en.wikipedia.org/wiki/Functional_programming) proqramlaşdırma dillərində dəyişənin dəyişilməsinə icazə verilmir. |
140 | 140 |
|
141 | | -In such languages, once the value is stored "in the box", it's there forever. If we need to store something else, the language forces us to create a new box (declare a new variable). We can't reuse the old one. |
| 141 | +Bu dillərdə, dəyər "qutuya" yerləşdirildikdən sonra orada ömürlük qalır. Fərqli məlumat saxlamaq istədikdə proqramlaşdırma dili bizə yeni qutu (dəyişənin yaradılması) yaratmağa məcbur edir. Biz köhnə dəyəri yenidən təyin edə bilmirik. |
142 | 142 |
|
143 | | -Though it may seem a little odd at first sight, these languages are quite capable of serious development. More than that, there are areas like parallel computations where this limitation confers certain benefits. Studying such a language (even if you're not planning to use it soon) is recommended to broaden the mind. |
| 143 | +İlk baxışda bunun biraz qəribə olmasına baxmayaraq bu dillərdə çox ciddi təkmilləşdirmə etmək mümkündür. Bundan əlavə, paralel hesablamalar kimi bəzi tapşırıqlarda bu məhdudiyyətin olmasının faydası var. Fikrinizi genişləndirmək üçün bu formalı dili öyrənməyi (hətta bunu işlətməyi planlaşdırmasanız belə) tövsiyyə edirik. |
144 | 144 | ``` |
145 | 145 |
|
146 | | -## Variable naming [#variable-naming] |
| 146 | +## Dəyişənlərin adlandırılması [#variable-naming] |
147 | 147 |
|
148 | | -There are two limitations on variable names in JavaScript: |
| 148 | +JavaScript-də dəyişənlərin adlandırılmasında iki məhdudiyyət var: |
149 | 149 |
|
150 | | -1. The name must contain only letters, digits, or the symbols `$` and `_`. |
151 | | -2. The first character must not be a digit. |
| 150 | +1. Dəyişən adında yalnız hərflər, rəqəmlər və ya `$` və `_` kimi simvollar ola bilər. |
| 151 | +2. Dəyişən adının ilk hərfi rəqəm ola bilməz. |
152 | 152 |
|
153 | | -Examples of valid names: |
| 153 | +Etibarlı adlar üçün nümunələr: |
154 | 154 |
|
155 | 155 | ```js |
156 | 156 | let userName; |
157 | 157 | let test123; |
158 | 158 | ``` |
159 | 159 |
|
160 | | -When the name contains multiple words, [camelCase](https://en.wikipedia.org/wiki/CamelCase) is commonly used. That is: words go one after another, each word except first starting with a capital letter: `myVeryLongName`. |
| 160 | +Dəyişən adı bir neçə sözdən ibarət olduqda çox zaman [camelCase](https://en.wikipedia.org/wiki/CamelCase) formatından istifadə olunur. Bu formatında ilk sözdən başqa bütün sözlər böyük hərf ilə başlayır: `myVeryLongName`. |
161 | 161 |
|
162 | | -What's interesting -- the dollar sign `'$'` and the underscore `'_'` can also be used in names. They are regular symbols, just like letters, without any special meaning. |
| 162 | +Dəyişən adlarında dollar (`'$'`) altdan xətt (`'_'`) işarələrinin də işlədilə bilməsi maraqlıdır. Bu simvollar, hərflər kimi xüsusi mənası olmayan sadə simvollardır. |
163 | 163 |
|
164 | | -These names are valid: |
| 164 | +Aşağıdakı dəyişən adlar etibarlıdır: |
165 | 165 |
|
166 | 166 | ```js run untrusted |
167 | | -let $ = 1; // declared a variable with the name "$" |
168 | | -let _ = 2; // and now a variable with the name "_" |
| 167 | +let $ = 1; // "$" adlı dəyişən təyin et |
| 168 | +let _ = 2; // "_" adlı dəyişən təyin et |
169 | 169 |
|
170 | 170 | alert($ + _); // 3 |
171 | 171 | ``` |
172 | 172 |
|
173 | | -Examples of incorrect variable names: |
| 173 | +Səhv məlumat adlarının nümunələri: |
174 | 174 |
|
175 | 175 | ```js no-beautify |
176 | | -let 1a; // cannot start with a digit |
| 176 | +let 1a; // dəyişən adı rəqəm ilə başlaya bilməz |
177 | 177 |
|
178 | | -let my-name; // hyphens '-' aren't allowed in the name |
| 178 | +let my-name; // dəyişən adında '-' kimi simvollar ola bilməz |
179 | 179 | ``` |
180 | 180 |
|
181 | 181 | ```smart header="Case matters" |
182 | | -Variables named `apple` and `AppLE` are two different variables. |
| 182 | +`apple` və `AppLE` adları fərqli dəyişənlərə istinad edir. |
183 | 183 | ``` |
184 | 184 |
|
185 | | -````smart header="Non-Latin letters are allowed, but not recommended" |
186 | | -It is possible to use any language, including cyrillic letters or even hieroglyphs, like this: |
| 185 | +````smart header="Latın adlarını işlətmək olar, amma tövsiyyə edilmir" |
| 186 | +Kiril hərfləri və iyeroqlif daxil olmaqla istənilən dildə olan hərfləri işlətmək olar: |
187 | 187 |
|
188 | 188 | ```js |
189 | 189 | let имя = '...'; |
190 | 190 | let 我 = '...'; |
191 | 191 | ``` |
192 | 192 |
|
193 | | -Technically, there is no error here, such names are allowed, but there is an international tradition to use English in variable names. Even if we're writing a small script, it may have a long life ahead. People from other countries may need to read it some time. |
| 193 | +Texniki olaraq, burada heç bir xəta yoxdur. Lakin, beynəlxalq ənənəyə görə dəyişən adları İngiliscə yazılır. Kiçik skript yazsaq belə bu skriptin uzun həyatı ola bilər. Digər ölkələrdə olan proqramistlər bu skripti oxumalı ola bilərlər. |
194 | 194 | ```` |
195 | 195 |
|
196 | | -````warn header="Reserved names" |
197 | | -There is a [list of reserved words](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Keywords), which cannot be used as variable names because they are used by the language itself. |
| 196 | +````warn header="Qorunan adlar" |
| 197 | +JavaScript dilində işlədilən bəzi [qorunan sözləri](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Keywords) dəyişən adı kimi işlətmək olmaz. |
198 | 198 |
|
199 | | -For example: `let`, `class`, `return`, and `function` are reserved. |
| 199 | +Məsələn: `let`, `class`, `return` və `function` sözləri qorunur. |
200 | 200 |
|
201 | | -The code below gives a syntax error: |
| 201 | +Aşağıdakı kodda sintaksis xətası baş verəcək: |
202 | 202 |
|
203 | 203 | ```js run no-beautify |
204 | | -let let = 5; // can't name a variable "let", error! |
205 | | -let return = 5; // also can't name it "return", error! |
| 204 | +let let = 5; // xəta! "let" adlı dəyişən işlətmək olmaz! |
| 205 | +let return = 5; // xəta! "return" adlı dəyişən işlətmək olmaz! |
206 | 206 | ``` |
207 | 207 | ```` |
208 | 208 |
|
209 | | -````warn header="An assignment without `use strict`" |
| 209 | +````warn header="`use strict`-siz təyinat" |
210 | 210 |
|
211 | | -Normally, we need to define a variable before using it. But in the old times, it was technically possible to create a variable by a mere assignment of the value without using `let`. This still works now if we don't put `use strict` in our scripts to maintain compatibility with old scripts. |
| 211 | +Normalda, dəyişəni işlətməmişdən öncə bu dəyişəni yaratmaq lazımdır. Lakin, keçmişdə dəyişəni `let` kimi açar sözü işlətmədən müəyyənləşdirmək mümkün idi. Skriptlərə `use strict` əlavə etmədikdə bu formada olan kodlar işləyəcək. |
212 | 212 |
|
213 | 213 | ```js run no-strict |
214 | | -// note: no "use strict" in this example |
| 214 | +// qeyd: bu nümunədə "use strict" işlədilmir |
215 | 215 |
|
216 | | -num = 5; // the variable "num" is created if it didn't exist |
| 216 | +num = 5; // "num" dəyişəni olmadıqda dəyişən yaranacaq |
217 | 217 |
|
218 | 218 | alert(num); // 5 |
219 | 219 | ``` |
220 | 220 |
|
221 | | -This is a bad practice and would cause an error in strict mode: |
| 221 | +Bunun pis praktika olduğundan bu sizə strikt rejimində xəta verəcək: |
222 | 222 |
|
223 | 223 | ```js |
224 | 224 | "use strict"; |
225 | 225 |
|
226 | 226 | *!* |
227 | | -num = 5; // error: num is not defined |
| 227 | +num = 5; // xəta: num təyin edilməyib |
228 | 228 | */!* |
229 | 229 | ``` |
230 | 230 | ```` |
231 | 231 |
|
232 | | -## Constants |
| 232 | +## Sabit dəyişənlər |
233 | 233 |
|
234 | | -To declare a constant (unchanging) variable, use `const` instead of `let`: |
| 234 | +Sabit (dəyişməyən) dəyişən yaratmaq istəyirsinizsə, `let` əvəzinə `const` işlədin: |
235 | 235 |
|
236 | 236 | ```js |
237 | 237 | const myBirthday = '18.04.1982'; |
238 | 238 | ``` |
239 | 239 |
|
240 | | -Variables declared using `const` are called "constants". They cannot be reassigned. An attempt to do so would cause an error: |
| 240 | +`const` ilə yaranan dəyişənlər "sabit dəyişənlər" adlandırılır. Bu dəyişənləri yenidən təyin etmək mümkün deyil. Dəyişənin dəyərini dəyişmək istədikdə xəta baş verəcək: |
241 | 241 |
|
242 | 242 | ```js run |
243 | 243 | const myBirthday = '18.04.1982'; |
244 | 244 |
|
245 | | -myBirthday = '01.01.2001'; // error, can't reassign the constant! |
| 245 | +myBirthday = '01.01.2001'; // xəta, sabit dəyişəni dəyişmək olmaz! |
246 | 246 | ``` |
247 | 247 |
|
248 | | -When a programmer is sure that a variable will never change, they can declare it with `const` to guarantee and clearly communicate that fact to everyone. |
| 248 | +Proqramçı dəyişənin heç vaxt dəyişməyəcəyindən əmin olduqda dəyişəni `const` ilə təyin edərək bu dəyişənin dəyişməyəcəyini siğortalayıb digər proqramçılara bildirə bilər. |
249 | 249 |
|
250 | 250 |
|
251 | | -### Uppercase constants |
| 251 | +### Böyük hərf ilə yazılmış sabit dəyişənlər |
252 | 252 |
|
253 | 253 | There is a widespread practice to use constants as aliases for difficult-to-remember values that are known prior to execution. |
254 | 254 |
|
|
0 commit comments