GeezerVison is showing github.com — Declaring Functions notes · VinceHu/javascript@0881deb
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: functions/declare.md +4Lines changed: 4 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -8,6 +8,8 @@ function double(x) { |
8 | 8 | } |
9 | 9 | ``` |
10 | 10 |
|
| 11 | +>*Note:* the function above **may** be referenced before it has been defined. |
| 12 | +
|
11 | 13 | Functions are also values in JavaScript; they can be stored in variables (just like numbers, strings, etc ...) and given to other functions as arguments : |
12 | 14 |
|
13 | 15 | ```javascript |
@@ -16,6 +18,8 @@ var double = function(x) { |
16 | 18 | }; |
17 | 19 | ``` |
18 | 20 |
|
| 21 | +>*Note:* the function above **may not** be referenced before it is defined, just like any other variable. |
| 22 | +
|
19 | 23 | --- |
20 | 24 |
|
21 | 25 | Declare a function named `triple` that takes an argument and returns its triple. |
|
Collapse file
Copy file name to clipboard +44Lines changed: 44 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +# Declaring Functions |
| 2 | + |
| 3 | +Functions, like variables, must be declared. Let's declare a function `double` that accepts an **argument** called `x` and **returns** the double of x : |
| 4 | + |
| 5 | +```javascript |
| 6 | +function double(x) { |
| 7 | + return 2 * x; |
| 8 | +} |
| 9 | +``` |
| 10 | + |
| 11 | +*Note*: the function above **may** be referenced before it has been defined. |
| 12 | + |
| 13 | +Functions are also values in JavaScript; they can be stored in variables (just like numbers, strings, etc ...) and given to other functions as arguments : |
| 14 | + |
| 15 | +```javascript |
| 16 | +var double = function(x) { |
| 17 | + return 2 * x; |
| 18 | +}; |
| 19 | +``` |
| 20 | + |
| 21 | +*Note*: the function above **may not** be referenced before it is defined, just like any other variable. |
| 22 | + |
| 23 | +--- |
| 24 | + |
| 25 | +Declare a function named `triple` that takes an argument and returns its triple. |
| 26 | + |
| 27 | +```js |
| 28 | + |
| 29 | +``` |
| 30 | + |
| 31 | +```js |
| 32 | +var triple = function(x) { |
| 33 | + return x * 3; |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +```js |
| 38 | +assert(triple); |
| 39 | +assert(triple(4) === 12); |
| 40 | +assert(triple(10) === 30); |
| 41 | +``` |
| 42 | + |
| 43 | +--- |
| 44 | + |