Functions in JavaScript Flashcards

1
Q

What is a function?

A

A function is a named set of instructions that you can call elsewhere in your code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is an argument and parameter? How are they different and used?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the call signature?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

When you define a function and call/invoke it, are they two different things?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you create a function declaration?

A

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');
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a return statement?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you create a function expression?

A

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");
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are default function parameters?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are arrow functions?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

When should you use a function?

A

Use a function when you know you will call it in your code three times or more. Otherwise, don’t use a function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

When should you use a return statement vs. a console.log statement?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the difference between a function and object?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly