-
-
Notifications
You must be signed in to change notification settings - Fork 660
[Concept]: Arrow Functions #1414
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
4240791
Arrow Function Concept
pertrai1 cac0d3e
Continue arrow function concept
pertrai1 bcfa1dc
Finish first draft
pertrai1 0cf5411
fix config concept
pertrai1 f99680d
add more examples
pertrai1 bc05efa
Fix up comments
pertrai1 7cbbca1
Add to fruit picker concept
pertrai1 edd2569
add to main config.json
pertrai1 178fd91
remove wrapping of sentences
pertrai1 f066519
fix link for javascript.info
pertrai1 540581d
update fruit picker objectives
pertrai1 2f0d17f
modify fn using single param
pertrai1 fe35713
some wording fixes
junedev 56f6113
use new intro as blurb
junedev 5fb9c09
restructure exercise intro
junedev d0a66c2
add arrow functions as prerequisites where needed
junedev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "blurb": "Besides function declarations and function expressions, JavaScript also has another very concise syntax for defining a function. These functions are called arrow functions.", | ||
| "authors": ["pertrai1"], | ||
| "contributors": [] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| # About | ||
|
|
||
| Besides function declarations and function expressions, JavaScript also has another very concise syntax for defining a function. | ||
| These functions are called _arrow functions_. | ||
|
|
||
| In this concept will focus on the syntax used to write an arrow function. | ||
| There are differences in the way that an arrow function works, such as _this_ binding, that will be covered in other concepts. | ||
|
|
||
| Here is a comparison between a function declaration and an arrow function. | ||
|
|
||
| ```javascript | ||
| function addUpTwoNumbers(num1, num2) { | ||
| return num1 + num2; | ||
| } | ||
|
|
||
| // function keyword removed and => added | ||
| const addUpTwoNumbers = (num1, num2) => { | ||
| return num1 + num2; | ||
| }; | ||
| ``` | ||
|
|
||
| Above, you will see that the arrow function syntax: | ||
|
|
||
| 1. removes the keyword `function` | ||
| 2. has declared the identifier `addUpTwoNumbers` as a `const` | ||
| 3. adds a fat arrow `=>` | ||
|
|
||
| If the function body contains only a return statement, like in the example above, the `{}` and the `return` keyword can be omitted. | ||
|
|
||
| <!-- prettier-ignore-start --> | ||
| ```javascript | ||
| const addUpTwoNumbers = (num1, num2) => { return num1 + num2 }; | ||
|
|
||
| // can be shortened to | ||
| const addUpTwoNumbers = (num1, num2) => num1 + num2; | ||
| // braces {} and return removed | ||
| ``` | ||
| <!-- prettier-ignore-end --> | ||
|
|
||
| In the special case of only returning an object from an arrow function, parenthesis are needed around the object to be able to omit the return statement. | ||
|
|
||
| ```javascript | ||
| // explicit return of object | ||
| const addUpTwoNumbers = (num1, num2) => { | ||
| return { num1, num2 }; | ||
| }; | ||
|
|
||
| // implicit return of object | ||
| const addUpTwoNumbers = (num1, num2) => ({ num1, num2 }); | ||
| ``` | ||
|
|
||
| The use of parenthesis around parameters depends on the number of parameters. | ||
|
|
||
| <!-- prettier-ignore-start --> | ||
| ```javascript | ||
| // one parameter does not need parenthesis | ||
| const square = num => num * num; | ||
|
|
||
| // two or more parameters need to be wrapped in parenthesis | ||
| const addUpTwoNumbers = (num1, num2) => num1 + num2; | ||
| ``` | ||
| <!-- prettier-ignore-end --> | ||
|
|
||
| Other concepts such as [Rest Parameters][concept-rest] and [Destructuring][concept-destructure] can also be used with an arrow function. | ||
|
|
||
| [concept-rest]: /tracks/javascript/concepts/rest-and-spread | ||
| [concept-destructure]: /tracks/javascript/concepts/array-destructuring |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| # Introduction | ||
|
|
||
| Besides function declarations and function expressions, JavaScript also has another very concise syntax for defining a function. | ||
| These functions are called _arrow functions_. | ||
|
|
||
| In this concept will focus on the syntax used to write an arrow function. | ||
| There are differences in the way that an arrow function works, such as _this_ binding, that will be covered in other concepts. | ||
|
|
||
| Here is a comparison between a function declaration and an arrow function. | ||
|
|
||
| ```javascript | ||
| function addUpTwoNumbers(num1, num2) { | ||
| return num1 + num2; | ||
| } | ||
|
|
||
| // function keyword removed and => added | ||
| const addUpTwoNumbers = (num1, num2) => { | ||
| return num1 + num2; | ||
| }; | ||
| ``` | ||
|
|
||
| Above, you will see that the arrow function syntax: | ||
|
|
||
| 1. removes the keyword `function` | ||
| 2. has declared the identifier `addUpTwoNumbers` as a `const` | ||
| 3. adds a fat arrow `=>` | ||
|
|
||
| If the function body contains only a return statement, like in the example above, the `{}` and the `return` keyword can be omitted. | ||
|
|
||
| <!-- prettier-ignore-start --> | ||
| ```javascript | ||
| const addUpTwoNumbers = (num1, num2) => { return num1 + num2 }; | ||
|
|
||
| // can be shortened to | ||
| const addUpTwoNumbers = (num1, num2) => num1 + num2; | ||
| // braces {} and return removed | ||
| ``` | ||
| <!-- prettier-ignore-end --> | ||
|
|
||
| In the special case of only returning an object from an arrow function, parenthesis are needed around the object to be able to omit the return statement. | ||
|
|
||
| ```javascript | ||
| // explicit return of object | ||
| const addUpTwoNumbers = (num1, num2) => { | ||
| return { | ||
| num1, | ||
| num2, | ||
| }; | ||
| }; | ||
|
|
||
| // implicit return of object | ||
| const addUpTwoNumbers = (num1, num2) => ({ num1, num2 }); | ||
| ``` | ||
|
|
||
| The use of parenthesis around parameters depends on the number of parameters. | ||
|
|
||
| <!-- prettier-ignore-start --> | ||
| ```javascript | ||
| // one parameter does not need parenthesis | ||
| const square = num => num * num; | ||
|
|
||
| // two or more parameters need to be wrapped in parenthesis | ||
| const addUpTwoNumbers = (num1, num2) => num1 + num2; | ||
| ``` | ||
| <!-- prettier-ignore-end --> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| [ | ||
| { | ||
| "url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions", | ||
| "description": "MDN: Arrow function expressions" | ||
| }, | ||
| { | ||
| "url": "https://javascript.info/arrow-functions-basics", | ||
| "description": "javascript.info: Arrow functions, the basics" | ||
| } | ||
| ] | ||
junedev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.