Functions Flashcards
What are functions useful for?
Re-useable code blocks
Efficient computation with built-in functions
e.g. myArray.join(‘ ‘)or
Math.random() are part of JS but in another language like C++
What’s the difference between parameters and arguments?
Parameters are placeholder for future arguments to be passed to a function.
function name(parameter1)
Arguments are actual values that are passed to the function as a parameter
function name('Bob');
What are arrow functions? What three things can you apply it to?
How would you express the below as an arrow function?
function name(n) {
return n*2;
}
Brief, concise way to to write function expressions, one-line actions and call backs
let name = n => n*2;
What’s the syntax for arrow functions?
(1) 1 argument
(2) no arguments
(3) multi-line steps;
(1) let name = n => n*2;
(2) let name = () => code;
(3) let name = () => {
code
}
What are anonymous functions? Give an example
They don’t have an associated variable name, often used where a function expects to get another function as a parameter
For example, the function addEventListener() expects 2 arguments to be passed type and listener, where listener is usually another function.
Without using anon functions, we need to specify a separate function for logkey:
addEventListener('keydown', logkey); function logKey(event) { console.log(`You pressed "${event.key}".`); }
We can write it like this with anonymous functions:
textBox.addEventListener('keydown', function(event) { console.log(`You pressed "${event.key}".`); });
What is a function expression? Give an example.
An expression is a function created inside an expression or another syntax contstruct
E.g. assigning a function to a variable name
let sayHi = function() { alert( "Hello" ); };
Allow anonymous functions
How do you declare a function?
This is a statement
function name(param1, param2 …) {
code;
}
What are return values?
Value that is returned after a function executes
Not all functions return a value - can be void
or undefined
What is function scope?
Each function is discrete (walled off from other functions) - any variables declared in a function cannot be accessed by other functions
When is a return value generally used?
For a function that is an intermediate step in a calculation e.g. the result it gets is stored in a variable to be used later on
Define ‘invoke’. How would you invoke a function?
Invoke in coding means to instruct to run
To call a function, cite its name:
function();
If a function is not a structure, what is it?
A special kind of value
from: https://javascript.info/function-expressions
What’s the primary difference between a function expression vs declaration?
A Function Expression is created when the execution reaches it and is usable only from that moment.
A Function Declaration can be called earlier than it is defined.
When should we use function expression over a declaration?
We should default to using a declaration at first before considering an expression: flexible organisation - we can call anywhere, easier to locate in a script
What is a method?
A function that is a property of an object.
source: https://developer.mozilla.org/en-US/docs/Glossary/Method