Functions in JavaScript Flashcards
What is a function?
A function is a named set of instructions that you can call elsewhere in your code.
What is an argument and parameter? How are they different and used?
An argument is a value that gets passed to the function when it’s called. A parameter is the variable that holds the place of an argument when the function is defined.
For example:
function hello(name) { return 'Hello ' + name + '!'; }
console.log(hello(‘Thomas’)); // => “Hello Thomas!”
This function abstracts out the name. We now can call it and pass in any value (argument) for name, and it will output a string that says “Hello “ + the value of name.
What is the call signature?
A call signature refers to the arguments or parameters — if any — that get passed to a function. It’s the parenthesis after the name of the function.
When you define a function and call/invoke it, are they two different things?
Yes, you must define it first, and then call upon it later in your code. For example:
function alertHelloWorld() { alert('Hello world'); }
alertHelloWorld();
The last line above is calling the function alertHelloWorld().
How do you create a function declaration?
Add the keyword function, space, and then the name of the function followed by the call signature. Add an argument or parameter inside the call signature (if necessary), and then two curly brackets. Inside the brackets, you define the behavior of the function.
function alertHelloWorld() { alert('Hello world'); }
What is a return statement?
A return statement causes a function to return the value to the right of the “return” keyword. We can assign values returned by a function to a variable (const hiTom = hello(“Thomas”)) or pass them as inputs to other function calls (console.log(hello(“Thomas”))).
How do you create a function expression?
Add a variable keyword, and then the name of the variable. Then add the equal sign followed by function and the argument or parameter in parentheses. Then the curly brackets. For example:
const myFunction = function(arg1, arg2) { console.log("myFunction"); }
What are default function parameters?
Default function parameters allow named parameters to be initialized with default values if no value or undefined is passed. Consider the following function:
function tenPower(power = 2) { return 10 ** power; }
tenPower(); // => 100
tenPower(1); // => 10
tenPower(3); // => 1000
We’ve set a default value for power to be 2 (power = 2). This allows us to call the function without explicitly passing in a value for power. If we don’t pass a value for power, we’ll get the default value of 2, otherwise, we’ll get the value we pass into our function.
What are arrow functions?
A more compact way to create functions. It was introduced as part of ES6. For example:
const add = (num1, num2) => num1 + num2; add(2, 3); // => 5
// same as above const addAlt = function(num1, num2) { return num1 + num2; }
In add, we enclose the function parameters in parentheses. Next, we have the fat arrow =>. Finally, we have a single statement that will be returned by the function. Even though there is no explicit return keyword here, the value num1 + num2 will be returned by this function.
When should you use a function?
Use a function when you know you will call it in your code three times or more. Otherwise, don’t use a function.
When should you use a return statement vs. a console.log statement?
return statements are a flexible and can be used by other functions in your code. However, console.log statements are not flexible and can’t be used somewhere else in your code.
What is the difference between a function and object?
All the code in a function stays within the function. It’s best when you are dealing with sensitive data.
However, objects have a global scope. The code in an object can be used or called anywhere in your code. Don’t use an object when dealing with sensitive data.