Functions Flashcards
What is an arrow function?
It is something like this,
let func = (arg1, arg2, …, argN) => expression;
what is the arrow function of this
let sum = function(a, b) { return a + b; };
It arrow (or shorter version is this)
let sum = (a, b) => a + b;
Rewrite this with arrow function.
function ask(question, yes, no) { if (confirm(question)) yes(); else no(); }
ask( "Do you agree?", function() { alert("You agreed."); }, function() { alert("You canceled the execution."); } );
function ask(question, yes, no) { if (confirm(question)) yes(); else no(); }
ask( "Do you agree?", () => alert("You agreed."), () => alert("You canceled the execution.") );
What is a function expression?
It is a function that allow us to create a function in the middle of an expression.
for example:
let sayHi = function() { alert( "Hello" ); };
What is a function?
- is a simple set of instruction
- reusable
- perform one action
- basic building blocs of a program
Functions have 2 specific things
-function declaration:
function name(parameters){ body }
-function call:
name(arguments)
what is the point of having function with parameters?
It is because different arguments can be passed into the function with a function call and can be changed every time
Does an arrow function need a return?
No arrow functions have implicit returns ( they return the value without having to type return)