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/15-function-basics/article.md
+31Lines changed: 31 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -225,6 +225,37 @@ On the other hand, it's independently called every time when `text` is missing.
225
225
226
226
```
227
227
228
+
````smart header="Default parameters in old JavaScript code"
229
+
Several years ago, JavaScript didn't support the syntax for default parameters.
230
+
231
+
So people used some other ways to specify default values, that you meet in old scripts.
232
+
233
+
For example, an explicit check for `undefined`:
234
+
235
+
```js
236
+
functionshowMessage(from, text) {
237
+
*!*
238
+
if (text ===undefined) {
239
+
text ='no text given';
240
+
}
241
+
*/!*
242
+
243
+
alert( from +": "+ text );
244
+
}
245
+
```
246
+
247
+
...Or using the `||` operator:
248
+
249
+
```js
250
+
functionshowMessage(from, text) {
251
+
// If the value of text is falsy, assign the default value
252
+
text = text ||'no text given';
253
+
...
254
+
}
255
+
```
256
+
````
257
+
258
+
228
259
### 매개변수 기본값을 설정할 수 있는 또 다른 방법
229
260
230
261
가끔은 함수 선언부에서 매개변수 기본값을 설정하는 것 대신 함수가 실행되는 도중에 기본값을 설정하는 게 논리에 맞는 경우가 생기기도 합니다. [영문 변경] Sometimes it makes sense to assign default values for parameters not in the function declaration, but at a later stage.
0 commit comments