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
Some people also define multiple variables in this multiline style:
66
+
어떤 사람들은 다음과 같이 여러 줄에 걸쳐 변수를 정의하기도 합니다.
74
67
75
-
>>>>>>> upstream/master
76
68
```js no-beautify
77
69
let user ='John',
78
70
age =25,
@@ -96,37 +88,23 @@ let user = 'John'
96
88
*!*var*/!* message ='Hello';
97
89
```
98
90
99
-
<<<<<<< HEAD
100
91
`var`는 `let`과 *거의* 동일하게 동작합니다. `var`도 `let`처럼 변수를 선언하는 데 쓰이죠. 다만 `var`는 '오래된' 방식입니다.
101
92
102
93
`let`과 `var`의 미묘한 차이점에 대해선 추후 <info:var> 에서 자세히 다루도록 하겠습니다. 지금 시점에선 이 차이점이 중요하지 않기 때문에 넘어가도록 합시다.
103
-
=======
104
-
The `var` keyword is *almost* the same as `let`. It also declares a variable but in a slightly different, "old-school" way.
105
-
106
-
There are subtle differences between `let` and `var`, but they do not matter to us yet. We'll cover them in detail in the chapter <info:var>.
107
-
>>>>>>> upstream/master
108
94
````
109
95
110
96
## 현실 속의 비유
111
97
112
98
'상자' 안에 데이터를 저장하는데, 이 상자에는 특별한 이름표가 붙어 있다고 상상해 봅시다. 이렇게 하면 '변수'를 좀 더 쉽게 이해할 수 있습니다.
113
99
114
-
<<<<<<< HEAD
115
100
예를 들어, 변수 `message`는 `message`라는 이름표가 붙어있는 상자에 `"Hello!"`라는 값을 저장한 것이라고 생각할 수 있습니다.
116
-
=======
117
-
For instance, the variable `message` can be imagined as a box labelled `"message"` with the value `"Hello!"` in it:
118
-
>>>>>>> upstream/master
119
101
120
102

121
103
122
104
상자 속엔 어떤 값이든지 넣을 수 있습니다.
123
105
124
-
<<<<<<< HEAD
125
-
원하는 만큼 값을 변경할 수도 있습니다.
126
-
=======
127
-
We can also change it as many times as we want:
106
+
다음과 같이 원하는 만큼 값을 변경할 수도 있습니다.
128
107
129
-
>>>>>>> upstream/master
130
108
```js run
131
109
let message;
132
110
@@ -172,21 +150,12 @@ let message = "That"; // SyntaxError: 'message' has already been declared
172
150
따라서 변수는 딱 한 번만 선언하고, 선언한 변수를 참조할 때는 `let` 없이 변수명만 사용해 참조해야 합니다.
173
151
````
174
152
175
-
<<<<<<< HEAD
176
153
```smart header="함수형 언어"
177
-
[함수형(functional)](https://en.wikipedia.org/wiki/Functional_programming) 프로그래밍 언어는 변숫값 변경을 금지합니다. [스칼라(Scala)](http://www.scala-lang.org/)와 [얼랭(Erlang)](http://www.erlang.org/)은 대표적인 함수형 언어입니다.
178
-
=======
179
-
```smart header="Functional languages"
180
-
It's interesting to note that there exist so-called [pure functional](https://en.wikipedia.org/wiki/Purely_functional_programming) programming languages, such as [Haskell](https://en.wikipedia.org/wiki/Haskell), that forbid changing variable values.
181
-
>>>>>>> upstream/master
154
+
흥미롭게도, [하스켈(Haskell)](https://en.wikipedia.org/wiki/Haskell)과 같이 변숫값 변경을 금지하는, 이른바 [순수 함수형(pure functional)](https://en.wikipedia.org/wiki/Purely_functional_programming) 프로그래밍 언어도 있습니다.
182
155
183
156
이들 언어에서는 '상자 속에' 값이 일단 저장되면, 그 값을 영원히 유지합니다. 다른 값을 저장하고 싶다면 새로운 상자를 만들어야(새 변수를 선언해야)만 합니다. 이전 변수를 재사용할 수 없습니다.
184
157
185
-
<<<<<<< HEAD
186
-
처음 봤을 땐 좀 이상해 보일 수 있지만, 함수형 언어는 중대한 개발에 상당히 적합한 언어입니다. 이런 제약이 장점으로 작용하는 병렬 계산(parallel computation)과 같은 영역도 있죠. 당장은 사용할 계획이 없더라도 이런 언어를 공부하는 것은 시야를 넓히는 데 도움이 되므로, 학습을 권유 드립니다.
187
-
=======
188
-
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.
189
-
>>>>>>> upstream/master
158
+
처음 봤을 땐 좀 이상해 보일 수 있지만, 함수형 언어는 중대한 개발에 상당히 적합한 언어입니다. 이런 제약이 장점으로 작용하는 병렬 계산(parallel computation)과 같은 영역도 있죠.
190
159
```
191
160
192
161
## 변수 명명 규칙
@@ -203,11 +172,7 @@ let userName;
203
172
let test123;
204
173
```
205
174
206
-
<<<<<<< HEAD
207
175
여러 단어를 조합하여 변수명을 만들 땐 [카멜 표기법(camelCase)](https://en.wikipedia.org/wiki/CamelCase)이 흔히 사용됩니다. 카멜 표기법은 단어를 차례대로 나열하면서 첫 단어를 제외한 각 단어의 첫 글자를 대문자로 작성합니다. `myVeryLongName`같이 말이죠.
208
-
=======
209
-
When the name contains multiple words, [camelCase](https://en.wikipedia.org/wiki/CamelCase) is commonly used. That is: words go one after another, with each word except the first starting with a capital letter: `myVeryLongName`.
210
-
>>>>>>> upstream/master
211
176
212
177
달러 기호 `'$'` 와 밑줄 `'_'` 를 변수명에 사용할 수 있다는 점이 조금 특이하네요. 이 특수 기호는 일반 글자처럼 특별한 의미를 지니진 않습니다.
213
178
@@ -228,32 +193,19 @@ let 1a; // 변수명은 숫자로 시작해선 안 됩니다.
228
193
let my-name; // 하이픈 '-'은 변수명에 올 수 없습니다.
229
194
```
230
195
231
-
<<<<<<< HEAD
232
196
```smart header="대·소문자 구별"
233
-
`apple`와 `AppLE`은 서로 다른 변수입니다.
234
-
```
235
-
236
-
````smart header="비 라틴계 언어도 변수명에 사용할 수 있지만 권장하진 않습니다."
237
-
키릴 문자, 심지어 상형문자도 변수명에 사용할 수 있습니다. 모든 언어를 변수명에 사용할 수 있죠.
238
-
=======
239
-
```smart header="Case matters"
240
-
Variables named `apple` and `APPLE` are two different variables.
197
+
`apple`와 `APPLE`은 서로 다른 변수입니다.
241
198
```
242
199
243
-
````smart header="Non-Latin letters are allowed, but not recommended"
244
-
It is possible to use any language, including Cyrillic letters, Chinese logograms and so on, like this:
245
-
>>>>>>> upstream/master
200
+
````smart header="비 라틴계 문자도 변수명에 사용할 수 있지만 권장하진 않습니다."
201
+
키릴 문자나 한자를 비롯한 어떤 언어의 문자든 변수명에 사용할 수 있습니다. 다음과 같이 말이죠.
246
202
247
203
```js
248
204
let имя = '...';
249
205
let 我 = '...';
250
206
```
251
207
252
-
<<<<<<< HEAD
253
208
위 코드에는 기술적인 에러가 없습니다. 변수명도 유효합니다. 하지만 영어를 변수명에 사용하는 것이 국제적인 관습이므로, 변수명은 영어를 사용해서 만들길 권유 드립니다. 다른 나라 사람이 스크립트를 볼 경우 등을 대비해 장기적인 안목을 가지고 코드를 작성합시다.
254
-
=======
255
-
Technically, there is no error here. Such names are allowed, but there is an international convention 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 sometime.
myBirthday = '01.01.2001'; // error, can't reassign the constant!
309
261
```
310
262
311
-
<<<<<<< HEAD
312
263
변숫값이 절대 변경되지 않을 것이라 확신하면, 값이 변경되는 것을 방지하면서 다른 개발자들에게 이 변수는 상수라는 것을 알리기 위해 `const`를 사용해 변수를 선언하도록 합시다.
313
264
314
-
=======
315
-
When a programmer is sure that a variable will never change, they can declare it with `const` to guarantee and communicate that fact to everyone.
316
-
>>>>>>> upstream/master
317
265
318
266
### 대문자 상수
319
267
320
-
<<<<<<< HEAD
321
268
기억하기 힘든 값을 변수에 할당해 별칭으로 사용하는 것은 널리 사용되는 관습입니다.
322
-
=======
323
-
There is a widespread practice to use constants as aliases for difficult-to-remember values that are known before execution.
324
-
>>>>>>> upstream/master
325
269
326
270
이런 상수는 대문자와 밑줄로 구성된 이름으로 명명합니다.
327
271
@@ -346,61 +290,36 @@ alert(color); // #FF7F00
346
290
347
291
그렇다면 언제 일반적인 방식으로 상수를 명명하고, 언제 대문자를 사용해서 명명해야 하는 걸까요? 명확히 짚고 넘어갑시다.
348
292
349
-
<<<<<<< HEAD
350
293
'상수'는 변수의 값이 절대 변하지 않음을 의미합니다. 그중에는 (빨간색을 나타내는 16진수 값처럼) 코드가 실행되기 전에 이미 그 값을 알고 있는 상수도 있고, 런타임 과정에서 *계산되지만* 최초 할당 이후 값이 변하지 않는 상수도 있습니다.
351
294
352
-
예시:
353
-
=======
354
-
Being a "constant" just means that a variable's value never changes. But some constants are known before execution (like a hexadecimal value for red) and some constants are *calculated* in run-time, during the execution, but do not change after their initial assignment.
295
+
예를 들면 다음과 같습니다.
355
296
356
-
For instance:
357
-
358
-
>>>>>>> upstream/master
359
297
```js
360
298
const pageLoadTime = /* 웹페이지를 로드하는데 걸린 시간 */;
361
299
```
362
300
363
-
<<<<<<< HEAD
364
301
`pageLoadTime`의 값은 페이지가 로드되기 전에는 정해지지 않기 때문에 일반적인 방식으로 변수명을 지었습니다. 하지만 이 값은 최초 할당 이후에 변경되지 않으므로 여전히 상수입니다.
365
302
366
303
정리하자면, 대문자 상수는 '하드 코딩한' 값의 별칭을 만들 때 사용하면 됩니다.
367
-
=======
368
-
The value of `pageLoadTime` is not known before the page load, so it's named normally. But it's still a constant because it doesn't change after the assignment.
369
-
370
-
In other words, capital-named constants are only used as aliases for "hard-coded" values.
371
-
>>>>>>> upstream/master
372
304
373
305
## 바람직한 변수명
374
306
375
307
변수에 관한 매우 중요한 사실이 한 가지 더 있습니다.
376
308
377
309
변수명은 간결하고, 명확해야 합니다. 변수가 담고있는 것이 무엇인지 잘 설명할 수 있어야 하죠.
378
310
379
-
<<<<<<< HEAD
380
311
변수의 이름을 짓는 것은 프로그래밍에서 가장 중요하고 복잡한 기술 중 하나입니다. 변수명만 슬쩍 봐도 초보자가 코드를 작성했는지, 노련한 개발자가 작성했는지 알 수 있습니다.
381
312
382
313
실제 프로젝트에선 맨 처음부터 완전히 독립적인 코드를 작성하기보다 기존 코드의 틀을 변경하고 확장하는데 대부분의 시간을 보냅니다. 작성했던 코드를 얼마 후에 다시 봤을 때, 정보에 알맞은 이름이 적혀있으면 정보를 더 쉽게 찾을 수 있습니다. 다시 말해, 변수가 올바른 이름을 가졌을 때 말이죠.
383
-
=======
384
-
Variable naming is one of the most important and complex skills in programming. A glance at variable names can reveal which code was written by a beginner versus an experienced developer.
385
-
386
-
In a real project, most of the time is spent modifying and extending an existing code base rather than writing something completely separate from scratch. When we return to some code after doing something else for a while, it's much easier to find information that is well-labelled. Or, in other words, when the variables have good names.
387
-
>>>>>>> upstream/master
388
314
389
315
그러므로 변수를 선언하기 전에 내가 지은 변수의 이름이 괜찮은지 숙고해 주시기 바랍니다.
390
316
391
317
아래는 변수 명명 시 참고하기 좋은 규칙입니다.
392
318
393
-
<<<<<<< HEAD
394
319
- `userName` 이나 `shoppingCart`처럼 사람이 읽을 수 있는 이름을 사용하세요.
395
320
- 무엇을 하고 있는지 명확히 알고 있지 않을 경우 외에는 줄임말이나 `a`, `b`, `c`와 같은 짧은 이름은 피하세요.
396
321
- 최대한 서술적이고 간결하게 명명해 주세요. `data`와 `value`는 나쁜 이름의 예시입니다. 이런 이름은 아무것도 설명해주지 않습니다. 코드 문맥상 변수가 가리키는 데이터나 값이 아주 명확할 때에만 이런 이름을 사용합시다.
397
322
- 자신만의 규칙이나 소속된 팀의 규칙을 따르세요. 만약 사이트 방문객을 'user'라고 부르기로 했다면, 이와 관련된 변수를 `currentVisitor`나 `newManInTown`이 아닌 `currentUser`나 `newUser`라는 이름으로 지어야 합니다.
398
-
=======
399
-
- Use human-readable names like `userName` or `shoppingCart`.
400
-
- Stay away from abbreviations or short names like `a`, `b`, and `c`, unless you know what you're doing.
401
-
- Make names maximally descriptive and concise. Examples of bad names are `data` and `value`. Such names say nothing. It's only okay to use them if the context of the code makes it exceptionally obvious which data or value the variable is referencing.
402
-
- Agree on terms within your team and in your mind. If a site visitor is called a "user" then we should name related variables `currentUser` or `newUser` instead of `currentVisitor` or `newManInTown`.
403
-
>>>>>>> upstream/master
404
323
405
324
간단해 보이나요? 그렇게 보이긴 합니다. 그러나 실전에서 서술적이고 간결한 변수명을 짓는 것은 간단하지 않습니다. 그럼, 화이팅!
0 commit comments