Created
May 2, 2019 15:15
-
-
Save deconstructionalism/eca99de0e81d94e1f79b5ef68be3360b to your computer and use it in GitHub Desktop.
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
| function myFunction (age, name) { | |
| arguments[0] | |
| console.log(`my name is ${name} and my age ${age}`) | |
| } | |
| ` | |
| "function" is the keyword that tells JS that we are about | |
| to define a function | |
| "(stuff1, stuff2...)" contain parameters which say | |
| what local variable will be called in the function block | |
| when they are passed in as arguments when the function is run/called/invoked. ORDER MATTERS! | |
| "{ ... }" function block, defines what the function does | |
| ALL functions return something. if you don't use the "return" keyword to return a value, it automatically returns "undefined" | |
| ` | |
| myFunction(25, 'Bethany') | |
| "my name is Bethany and my age is 25" | |
| /* | |
| default arguments can be used to tell a function what values | |
| to use if arguments are not provided | |
| */ | |
| function addTwoNumbers (num1 = 1, num2 = 3) { | |
| return num1 + num2 | |
| } | |
| addTwoNumbers(4,5) | |
| // 9 | |
| addTwoNumbers() | |
| // 4 | |
| addTwoNumbers(10) | |
| // 13 | |
| /* | |
| different function definition syntaxes | |
| */ | |
| // function declaration syntax | |
| function otherFunction () { | |
| console.log('hi') | |
| } | |
| // function assignment syntax | |
| const otherFunction = function () { | |
| console.log('hi') | |
| } | |
| // ES6 syntax - arrow functions | |
| const otherFunction = () => { | |
| console.log('hi') | |
| } | |
| // single line block can remove {} and put on | |
| // same line as function name - all on one line | |
| const otherFunction = () => console.log('hi') | |
| // additionally, if that single line in the block | |
| // was to return a value, you can remove the return | |
| // keyword as well and it will still return! this | |
| // is called "implicit return" | |
| const otherFunction2 = () => { | |
| return 'hello' | |
| } | |
| const otherFunction2 = () => 'hello' | |
| // when your function has a single param, you | |
| // can remove the parentheses | |
| const thisFunction = (name) => console.log(name) | |
| const thisFunction = name => console.log(name) | |
| // that won't work with more than one param | |
| const thisFunction = (name, age) => console.log(name, age) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment