Skip to content

Commit

Permalink
Merge pull request javascript-tutorial#371 from brentguf/function-exp…
Browse files Browse the repository at this point in the history
…ressions

Function expressions
  • Loading branch information
iliakan authored Mar 3, 2018
2 parents 84db86f + f7ddc6e commit a894f82
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions 1-js/02-first-steps/15-function-expressions-arrows/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,8 @@ First, the syntax: how to see what is what in the code.
return a + b;
}
```
- *Function Expression:* a function, created inside an expression or inside another syntax construct.

Here, the function is created at the right side of the "assignment expression =":
- *Function Expression:* a function, created inside an expression or inside another syntax construct. Here, the function is created at the right side of the "assignment expression" `=`:

```js
// Function Expression
let sum = function(a, b) {
Expand Down Expand Up @@ -243,7 +242,7 @@ Function Expressions are created when the execution reaches them. That would hap

Sometimes that's handy to declare a local function only needed in that block alone. But that feature may also cause problems.

For instance, let's imagine that we need to declare a function `welcome()` depending on the `age` variable that we get in run time. And then we plan to use it some time later.
For instance, let's imagine that we need to declare a function `welcome()` depending on the `age` variable that we get during runtime. And then we plan to use it some time later.

The code below doesn't work:

Expand Down Expand Up @@ -298,7 +297,7 @@ if (age < 18) {
}
}

// Here we're out of figure brackets,
// Here we're out of curly braces,
// so we can not see Function Declarations made inside of them.

*!*
Expand Down Expand Up @@ -439,15 +438,15 @@ They are very convenient for simple one-line actions, when we're just too lazy t
The examples above took arguments from the left of `=>` and evaluated the right-side expression with them.
Sometimes we need something a little bit more complex, like multiple expressions or statements. It is also possible, but we should enclose them in figure brackets. Then use a normal `return` within them.
Sometimes we need something a little bit more complex, like multiple expressions or statements. It is also possible, but we should enclose them in curly braces. Then use a normal `return` within them.
Like this:
```js run
let sum = (a, b) => { // the figure bracket opens a multiline function
let sum = (a, b) => { // the curly brace opens a multiline function
let result = a + b;
*!*
return result; // if we use figure brackets, use return to get results
return result; // if we use curly braces, use return to get results
*/!*
};
Expand Down Expand Up @@ -475,5 +474,5 @@ So we should use a Function Expression only when a Function Declaration is not f

Arrow functions are handy for one-liners. They come in two flavors:

1. Without figure brackets: `(...args) => expression` -- the right side is an expression: the function evaluates it and returns the result.
2. With figure brackets: `(...args) => { body }` -- brackets allow us to write multiple statements inside the function, but we need an explicit `return` to return something.
1. Without curly braces: `(...args) => expression` -- the right side is an expression: the function evaluates it and returns the result.
2. With curly braces: `(...args) => { body }` -- brackets allow us to write multiple statements inside the function, but we need an explicit `return` to return something.

0 comments on commit a894f82

Please sign in to comment.