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
When should local or global variables be used?
Global variables are storing project level data, otherwise, it’s recommended to use local variables to limit changes and keep code clean
~ source Javascript.info
What happens in a local variable if it has the same name as a global variable?
Local variable has precedence i.e. it “shadows” the global variable
What are the 3 different ways to specify default parameter values if none are given when a function is called?
(1) assigning something to parameter with =
. this can be simple to complex like another function
function showMessage(from, text = "no text given") { alert( from + ": " + text ); }
(2) compare parameter with undefined
(3) use || operator
function showMessage(text) {
// if text is undefined or otherwise falsy, set it to ‘empty’
text = text || ‘empty’;
…
}
The two roles of return
when used in a function?
(1) Returns an actual value
(2) Stops code from executing
- value may be returned
- return by itself just triggers an exit
What is the output for an empty return
or if no return is specified?
(1) function doNothing() { /* empty */ }
(2) function doNothing() {
return;
}
returns undefined
What are callbacks or callback functions?
functions we pass as an argument in another function and call/execute later
… like addEventListener??
To increase readability and help make our script maintainable, we should write how many actions per line?
One line, one action
Do functions have to have arguments?
No