Functions Flashcards
What is a function?
It’s a set of instructions,
you want to run over and over again.
How does a function begin?
Let’s look at the structure of a function.

using the function keyword
What is the function name in this example?

[goToCoffeeShop]
What is the function parameter?

(drink)
What role does the curly braces serve?

start and end of function
What is an argument?
An argument is a value (primitive or object)
passed as input to a function.
How do you call a function?
use the function name followed by parenthesis and semi-colon
goToCoffeeShop();
Why use functions?
To avoid unnecessary repetition and effort
To avoid having to re-enter code in multiple places
What does this function do?

It generates a random number between 1 and 6.
In this example, where are the functions in this snippet?

Find by ()
floor is a function
random is a function
Will this code run?

No
The function hasn’t been called
What does the alert(); function do?
It creates a simple pop up window

How do you call a function?
functionName();
Why will this JavaScript run successfully?

The function is being called
What’s the advantage of using functions?
It produces multiple results,
without repeating code.
How is the function being called in this example?

alertRandom();
How do you call a function multiple times in a row?
repeat the function call
In this example, how many times is the function being called?

4 times
When the function is called 4 times, how is the number appearing on the screen?
It’s basically creating 4 popups, one after the other.
Each popup has a randomly generated number.
What is a return?
A return is a function that gives you the value of a variable.
Is return more flexible?
Yes
You can manipulate output as alert, console,
or store a value in a variable.
In this example, why does alert removed and return added?
In the original function, the alert was hardwired into it; this limits its functionality to only one way to output the code.
Why was the function renamed?

It no longer made sense to use alert in
the function name, as it was going to
be capable of more than that.
How do you call a function outcome from an alert?
alert( functionName() );

How do you store the value of a function in a variable?
variable vName = functionName();

In this example, how was the function stored in a variable?

const dieRoll = getRandomNumber();
What's a practical example of using a function in the context of an eCommerce store?
You could create a function to calculate
the total price of an item, including taxes
and shipping costs in an online store.
Is it common to use returns in JavaScript?
Yes it is common to use returns
It’s common to use return values to use in mulitple ways.
If you don’t specify a return value what happens?
It produces an error message: undefined
Is it possible to have multiple return statements?
Yes
You can combine return statements to accomplish an objective.
What is a multiple return statement?
How is it helpful to have multiple return
statements in the context of an online web form?
You can write a function that checks whether or not a form field is empty in a web form, then give the user a message on what they need to do next to complete the form.
Should the return statement(s) be at the
beginning or end of the function?
Return statements should be at the
end of the function.
Can returns retrieve multiple bits of information?
No
Only one type, one value at a time
What are the 4 types of values a return can retrieve?
CBS N
Content of a variable
Booleans
Strings
Numbers
What’s the first step?

Create a function & name it
function isFieldEmpty
What’s step 2?

Create a variable that equals to the info ID of the document.
const field = document.querySelector(‘#info’);
What’s the third step logic?
Construct an if/else statement sequence that returns either true or false (booleans) depending upon whether or not variable value is correct.
What’s the code for the 3rd step?
if else conditional statement consists of:
if (!field.value) {
return true;
} else {
return false;
}

Can a conditional statement take two paths?
No
It’s either true or false, not both.
What is step 4?

Create a variable named fieldtest
which stores the value of the function

What is Step 5 in this example?

Create a conditional statement that evaluates whether or not there is a value in the field. True or false.
if (fieldTest) {
alert(‘Please provide your information.’);
}

What is an argument?
An argument is a value passed as input to a function.
What do parameters work like?
They have alot in common with ….
Variables
What is a parameter?
It’s a value passed to the function, so it can run a program.
Why would you use upper in place of a number?

It gives you flexibility to establish what upper is each time the function is called via an argument.
In the variable const randomNumber,
why was upper added there?

6 was replaced with upper to change it from a fixed number to a parameter to let multiple values establish the upper limit.
What is the return doing?

This is the part of the function that’s producing the return value mechanism. It’s allowing the info to be ‘sent back’ any number of ways when the function is called upon.
Why are there multiple console.logs with randomNumbers?

It shows you how the arguments passed when the function is called is truly returning values in a range.
In this example, which is the parameter?

The parameter is within the function.
(upper)
Which is the argument, in this example?

The number specified when the function is called
6
100
1000
10
Can a function have multiple parameters?
Yes
Multiple parameters can be established in a function
Do you need multiple arguments, when multiple parameters are established in a function?
Yes
They should line up to avoid producing errors
1:1 ratio of parameters to arguments
Do parameters need quotation marks within a function?

No
What separates parameters when multiple parameters are defined in the function?

commas
,
How do you update the programming to include more than 1 parameter?
${item};
What’s the term for the ${pastry}; in the programming?
Expression
When you establish 2 expressions ${drink} or ${pastry} in the programming, do you need to specify two arguments also?
Yes
The function needs to have 2 arguments to run properly
When you call a function, what punctuation do you need to specify the arguments?
Each argument should be wrapped in single quotation marks
goToCoffeeShop( ‘Expresso’, ‘Donut’);
With multiple arguments, should the comma be inside or outside of the string?
Outside the string
goToCoffeeShop( ‘Latte’,‘Scone’);
Can return specify more than 1 value?

No
Would this function return what we need it to return?

No
It is missing string interpolation for the return value
How does string interpolation help the return perform what is needed?

Using string interpolation, we can get the return area to specify the parameters we need for the program.
How is string interpolation being used in this program?

Using expressions + string interpolation, the area and unit are defined within return
Why isn’t every parameter listed as a return?

The calc is multiplying two values (width * length); it’s meant to condense into 1 value, rather than 2 measurements.
How do you call this function?
functionName(#, #, ‘measurement’);
getArea(10, 20, ‘sq ft’);

What would the console print out with getArea(10, 20, ‘sq ft’); ?

200 sq ft
Should you create many parameters?
No
It complicates things at several places,
more to keep track of things
What’s too many parameters/arguments?
Beyond 4 or 5, parameters it’s better to use other methods as it gets tedious to match up parameters/arguments for the program.
Is it possible that as a website code grows, you’ll accidentally or intentionally use the same variable name?
Yes, it is possible to reuse variable names
Do functions create the scope in which variables work?
Yes
Variable names are contained within their function.
What’s variable scope?
It’s the programming constraint a variable operates within.
How are scopes helpful within JavaScript?
Scopes prevent conflicts and overriding of other programs.
How many times is the variable named person defined in this code?

2 times
same const
same variable name
What is a function scope?
Scopes defined within a scope is a function scope
Is function greeting a global scope or functional scope?

Functional scope
Is let person = ‘Lee’; a functional scope or global scope?

Global scope, not created within a function
What’s a best practice for global variables
to prevent reassignment?
Use const for global variables to prevent
accidental usage inside of functions
What is a function declaration?
It’s a function keyword with function name
function LearnBrainScape or
function getRandomNumber(upper)
What is a function expression?
Function expressions let you assign a function to a variable
What is an anonymous function?
An anonymous function without a name after the keyword function.
With an anonymous function where does the name come from?
It comes from a variable name
Can you call a function declaration before it loads?
Yes
You could use console.log(functionName() );
before it loads and it will return the value from the program
What is hoisting?
Lifting function declarations
through the code, towards the top, near global scope
Does a function expression get hoisted?
No, function expressions are not hoisted –
It is only active when the code reaches that level.
If you call a function expression before it’s defined you’ll get an _____
error
What does this error message mean?
Uncaught referenceError: cannot
access ‘getRandsomNumber’ before initialization
It means the call came before the function expression.
Function expressions have to be defined
first, before they’ll called upon.
Is this a function expression?

Yes, it is a function expression
With a function expression, is the function value stored in a variable

Yes
the function value is stored in a variable
How do you know this is a function expression?

There is no name after the function keyword, just a parameter
It has an equal sign assigning it a named variable
When should you use functions?
When there’s repetition in the code
Find yourself writing same code over and over
Functions can help you group reusable codes
How do you create an arrow?
=>
When converting a function into an arrow, what changes in the syntax?

function() is replaced with () =>

Are arrow functions anonymous?
Arrow Functions Inherently anonymous
Why are arrow functions like function expressions?
Arrow functions behave like function expressions because they are not hoisted up in the code.
When are arrow functions called, inline or hoisted?
Arrow functions respond only when JS reaches the line of code it’s on; it is not hoisted.
Are function declarations hoisted?
Functional Declarations are hoisted, unlike arrow functions
Do you need parenthesis when you have 1 parameter
specified in an arrow function?
No

What does this arrow function do?

It takes the specified value and multiples it
Do you need parenthesis with multiple parameters
in an arrow function?

Yes
How can this one line statement be made shorter in syntax?

Remove curly braces

If you have a single line funciton with no parameters do you need parenthesis before the arrow token?
Yes
Why does a single-line arrow function with no parameteres require parenthesis before the arrow token?
With a single-line arrow function with NO parameters it requires parentheses before the arrow token to avoid JS syntax errors.
Looking at the code snippets, which is
correct top or bottom? Why?

Bottom is correct
Because with a single line, no parameters, in arrow function you need () between the = sign and arrow token
Why is it helpful to establish
default values in function parameters?
If for any reason, you do not pass a specific argument to a function, the function falls back to the default value.
Does this code snippet expect an argument?

Yes
It is expecting an argument
relating to the (name)
If you enter the function name without specifying an argument, then what happens?
You get an error message, saying undefined.

What happens if you don’t specify a function argument?
It can break your entire program.
Why would you want to specify a default function argument?
To prevent the program from not working altogether.
What is the default argument in this JS snippet?

student
Why does it work correctly?

The argument is being passed as a string to the function allowing it to provide the greeting with the specified name.
How do you specify a default argument?

You modify the parameter with an equal sign and string to give a default argument to the function.
Can a function have multiple parameters?
Yes, it is possible

How do you establish default parameters?

in the parameter you use the = sign to assign the value
and string to define the default parameter
Even with established arguments, can you
leave a blank when calling function?
No, you can’t use blanks with default arguments
the arguments are still expected
What word can you put in for an
argument when you want the default response?
undefined
What happens when undefined is used to call the function?

Undefined works as a placeholder preventing an error
This means it will use the default argument in the function
How do you begin a JS comment?
/**

In a long comment, what symbol is used
to mark the beginning of a new line of comments?
* symbol starts each line after initial comment opens

What purpose does the @param tag serve?

@param tag provides name, type, and description of function parameter
Should you use one or many param tags per parameter?
Use one @param tag for each parameter you define
What’s the purpose of the @return tag in a comment context?

@return documents the value the function returns with type and description
Why are comments helpful to document code?
Using a consistent and descriptive commenting approach makes your functions more predictable. Other developers who need to learn about your functions will have a faster and easier way to identify each part of a function.
Why is this comment well documented?

Each function component has a clear, concise, defined
purpose in the explanation found within the comment.