Functions Flashcards

1
Q

function showMessage() {
alert( ‘Hello everyone!’ );
}

function deceleration or expression?

A

deceleration

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

let userName = ‘John’;

function showMessage() {
let message = ‘Hello, ‘ + userName;
alert(message);
}

showMessage(); // returns and why?

A

Hello, John

A variable declared inside a function is only visible inside that function.

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

function showMessage() {
let message = “Hello, I’m JavaScript!”;
alert( message );
}

showMessage(); // returns? and why
alert( message ); // returns? and why?

A

Hello, I’m JavaScript!
variable in function

<– Error! The variable is local to the function
variable not accessible to function.

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

A function can access an outer variable as well,

True or False

A

True

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

let userName = ‘John’;
function showMessage() {
let message = userName;
alert(message);
}

showMessage(); // returns and why?

A

John

A function can access an outer variable.

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

a function can access and alter a outer variable?

True or False

A

True

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

let userName = ‘John’;

function showMessage() {
userName = “Bob”;

let message = ‘Hello, ‘ + userName;
alert(message);
}

alert( userName ); // returns?
showMessage();
alert( userName ); // returns?

A

John

before the function call

Bob,

the value was modified by the function

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

In a function the outer variable is only used if ______

A

there’s no local one.

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

let userName = ‘John’;

function showMessage() {
let userName = “Bob”;
let message = userName;
alert(message);
}

showMessage(); // returns and why?

alert( userName ); // returns and why?

A

Bob
the function will create and use its own userName

John,
unchanged, the function did not access the outer variable

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

Variables declared outside of any function, such as the outer userName in the code above, are called _______

A

global

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

Global variables are visible from any function unless_____

A

shadowed by locals

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

_____ variables are visible from any function

A

Global

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

A ______ is the variable listed inside the parentheses in the function declaration

A

parameter

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

An ________ is the value that is passed to the function when it is called

A

argument

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

If a function is called, but an argument is not provided, then the corresponding value becomes __________.

A

undefined

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

function showCount(count) {
alert(count ?? “unknown”);
}

showCount(0); // returns?

A

0

17
Q

function showCount(count) {
alert(count ?? “unknown”);
}

showCount(null); // returns?
showCount(); // returns?

A

unknown

unknown

18
Q

function sum(a, b) {
return a + b;
}
// how to store the return in a variable named ‘result’?
alert( result ); // 3

A

let result = sum(1, 2);

19
Q

When a function directive reaches return what happens?

A

function stops and return value.

20
Q

There may be many occurrences of return in a single function.

True or false

A

true

using a conditional statement.

21
Q

A function with an empty return or without it returns ______

A

undefined

22
Q

function doNothing() { /* empty */ }

alert( doNothing() === undefined ); // returns?

A

true

23
Q

function doNothing() {
return;
}
alert( doNothing() === undefined ); // returns?

A

true

24
Q

If we want the returned expression to wrap across multiple lines, we should start it at the same line as return. Or at least put _______

A

the opening parentheses