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
@@ -176,7 +176,7 @@ When a value is passed as a function parameter, it's also called an *argument*.
176
176
177
177
In other words, to put these terms straight:
178
178
179
-
- A parameter is the variable listed inside the parentheses in the function declaration (it's a declaration time term)
179
+
- A parameter is the variable listed inside the parentheses in the function declaration (it's a declaration time term).
180
180
- An argument is the value that is passed to the function when it is called (it's a call time term).
181
181
182
182
We declare functions listing their parameters, then call them passing arguments.
@@ -206,7 +206,13 @@ function showMessage(from, *!*text = "no text given"*/!*) {
206
206
showMessage("Ann"); // Ann: no text given
207
207
```
208
208
209
-
Now if the `text` parameter is not passed, it will get the value `"no text given"`
209
+
Now if the `text` parameter is not passed, it will get the value `"no text given"`.
210
+
211
+
The default value also jumps in if the parameter exists, but strictly equals `undefined`, like this:
212
+
213
+
```js
214
+
showMessage("Ann", undefined); // Ann: no text given
215
+
```
210
216
211
217
Here `"no text given"` is a string, but it can be a more complex expression, which is only evaluated and assigned if the parameter is missing. So, this is also possible:
212
218
@@ -225,9 +231,41 @@ In the example above, `anotherFunction()` isn't called at all, if the `text` par
225
231
On the other hand, it's independently called every time when `text` is missing.
226
232
```
227
233
234
+
````smart header="Default parameters in old JavaScript code"
235
+
Several years ago, JavaScript didn't support the syntax for default parameters. So people used other ways to specify them.
236
+
237
+
Nowadays, we can come across them in old scripts.
238
+
239
+
For example, an explicit check for `undefined`:
240
+
241
+
```js
242
+
function showMessage(from, text) {
243
+
*!*
244
+
if (text ===undefined) {
245
+
text ='no text given';
246
+
}
247
+
*/!*
248
+
249
+
alert( from +": "+ text );
250
+
}
251
+
```
252
+
253
+
...Or using the `||` operator:
254
+
255
+
```js
256
+
functionshowMessage(from, text) {
257
+
// If the value of text is falsy, assign the default value
258
+
// this assumes that text == "" is the same as no text at all
259
+
text = text ||'no text given';
260
+
...
261
+
}
262
+
```
263
+
````
264
+
265
+
228
266
### Alternative default parameters
229
267
230
-
Sometimes it makes sense to assign default values for parameters not in the function declaration, but at a later stage.
268
+
Sometimes it makes sense to assign default values for parameters at a later stage after the function declaration.
231
269
232
270
We can check if the parameter is passed during the function execution, by comparing it with `undefined`:
233
271
@@ -422,7 +460,7 @@ These examples assume common meanings of prefixes. You and your team are free to
422
460
```smart header="Ultrashort function names"
423
461
Functions that are used *very often* sometimes have ultrashort names.
424
462
425
-
For example, the [jQuery](http://jquery.com) framework defines a function with `$`. The [Lodash](http://lodash.com/) library has its core function named `_`.
463
+
For example, the [jQuery](https://jquery.com/) framework defines a function with `$`. The [Lodash](https://lodash.com/) library has its core function named `_`.
426
464
427
465
These are exceptions. Generally function names should be concise and descriptive.
428
466
```
@@ -490,7 +528,7 @@ function name(parameters, delimited, by, comma) {
490
528
491
529
To make the code clean and easy to understand, it's recommended to use mainly local variables and parameters in the function, not outer variables.
492
530
493
-
It is always easier to understand a function which gets parameters, works with them and returns a result than a function which gets no parameters, but modifies outer variables as a side-effect.
531
+
It is always easier to understand a function which gets parameters, works with them and returns a result than a function which gets no parameters, but modifies outer variables as a sideeffect.
0 commit comments