Skip to content

Commit 0957ea0

Browse files
Merge pull request #166
main sync
2 parents b5a2587 + b91be45 commit 0957ea0

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
No difference.
1+
No difference!
2+
3+
In both cases, `return confirm('Did parents allow you?')` executes exactly when the `if` condition is falsy.

1-js/02-first-steps/15-function-basics/article.md

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ The `function` keyword goes first, then goes the *name of the function*, then a
2424

2525
```js
2626
function name(parameter1, parameter2, ... parameterN) {
27-
...body...
27+
// body
2828
}
2929
```
3030

@@ -176,7 +176,7 @@ When a value is passed as a function parameter, it's also called an *argument*.
176176
177177
In other words, to put these terms straight:
178178
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).
180180
- An argument is the value that is passed to the function when it is called (it's a call time term).
181181
182182
We declare functions listing their parameters, then call them passing arguments.
@@ -206,7 +206,13 @@ function showMessage(from, *!*text = "no text given"*/!*) {
206206
showMessage("Ann"); // Ann: no text given
207207
```
208208
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+
```
210216
211217
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:
212218
@@ -225,9 +231,41 @@ In the example above, `anotherFunction()` isn't called at all, if the `text` par
225231
On the other hand, it's independently called every time when `text` is missing.
226232
```
227233

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+
function showMessage(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+
228266
### Alternative default parameters
229267
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.
231269
232270
We can check if the parameter is passed during the function execution, by comparing it with `undefined`:
233271
@@ -422,7 +460,7 @@ These examples assume common meanings of prefixes. You and your team are free to
422460
```smart header="Ultrashort function names"
423461
Functions that are used *very often* sometimes have ultrashort names.
424462

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 `_`.
426464

427465
These are exceptions. Generally function names should be concise and descriptive.
428466
```
@@ -490,7 +528,7 @@ function name(parameters, delimited, by, comma) {
490528
491529
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.
492530
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 side effect.
494532
495533
Function naming:
496534

0 commit comments

Comments
 (0)