From 721f799f2818275b57417e8f4546b01baf22bba0 Mon Sep 17 00:00:00 2001 From: Paul <38412377+hwanderlust@users.noreply.github.com> Date: Sat, 13 Oct 2018 03:18:53 -0400 Subject: [PATCH] More syntax examples (#18647) Breaks down the different ways you can write arrow functions for those new to it --- .../javascript/arrow-functions/index.md | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/client/src/pages/guide/english/javascript/arrow-functions/index.md b/client/src/pages/guide/english/javascript/arrow-functions/index.md index 1166aea85d..a8ce0a8eac 100644 --- a/client/src/pages/guide/english/javascript/arrow-functions/index.md +++ b/client/src/pages/guide/english/javascript/arrow-functions/index.md @@ -27,6 +27,23 @@ var multiply = (x, y) => x * y; You no longer need the `function` and `return` keywords, or even the curly brackets. +```javascript +// everything included +const multiply = function(x, y) => { return x * y }; + +// remove "function" +const multiply = (x, y) => { return x * y }; + +// remove curly brackets and "return" ==> this way it returns implicitly +const multiply = (x, y) => x * y; + +// if you only have one argument/parameter +const multiplyBy2 = x => x * 2; + +// combined with the ternary operator, but note it's not a looker! +const addOrMultiply = (x, y, mathOperator) => mathOperator.toLowerCase() === 'add' ? x + y : x * y; +``` + ### A simplified `this` Before arrow functions, new functions defined their own `this` value. To use `this` inside a traditional function expression, we have to write a workaround like so: @@ -63,4 +80,4 @@ var p = new Person(); #### Further Reading -MDN link \ No newline at end of file +MDN link