Functions Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What are functions useful for?

A

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++

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

What’s the difference between parameters and arguments?

A

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

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;
}

A

Brief, concise way to to write function expressions, one-line actions and call backs

let name = n => n*2;

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

What’s the syntax for arrow functions?
(1) 1 argument
(2) no arguments
(3) multi-line steps;

A

(1) let name = n => n*2;
(2) let name = () => code;
(3) let name = () => {
code
}

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

What are anonymous functions? Give an example

A

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

What is a function expression? Give an example.

A

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

How do you declare a function?

A

This is a statement

function name(param1, param2 …) {
code;
}

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

What are return values?

A

Value that is returned after a function executes

Not all functions return a value - can be void or undefined

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

What is function scope?

A

Each function is discrete (walled off from other functions) - any variables declared in a function cannot be accessed by other functions

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

When is a return value generally used?

A

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

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

Define ‘invoke’. How would you invoke a function?

A

Invoke in coding means to instruct to run

To call a function, cite its name:
function();

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

If a function is not a structure, what is it?

A

A special kind of value

from: https://javascript.info/function-expressions

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

What’s the primary difference between a function expression vs declaration?

A

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.

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

When should we use function expression over a declaration?

A

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

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

What is a method?

A

A function that is a property of an object.

source: https://developer.mozilla.org/en-US/docs/Glossary/Method

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

When should local or global variables be used?

A

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

17
Q

What happens in a local variable if it has the same name as a global variable?

A

Local variable has precedence i.e. it “shadows” the global variable

18
Q

What are the 3 different ways to specify default parameter values if none are given when a function is called?

A

(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’;

}

19
Q

The two roles of return when used in a function?

A

(1) Returns an actual value

(2) Stops code from executing
- value may be returned
- return by itself just triggers an exit

20
Q

What is the output for an empty return or if no return is specified?

(1) function doNothing() { /* empty */ }

(2) function doNothing() {
return;
}

A

returns undefined

21
Q

What are callbacks or callback functions?

A

functions we pass as an argument in another function and call/execute later

… like addEventListener??

22
Q

To increase readability and help make our script maintainable, we should write how many actions per line?

A

One line, one action

23
Q

Do functions have to have arguments?

A

No