Functions Flashcards

1
Q

Functions that are part of objects are called ________.

A

methods

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

function hello(name = ‘Chris’) {
console.log(Hello ${name}!);
}

hello(‘Ari’); returns?
hello(); returns

A

// Hello Ari!
// Hello Chris!

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

Unlike function declaration, function expressions are ___________

A

not hoisted

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

Unlike function declaration, ___________ are not hoisted

A

function expressions

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

if your arrow function needs to return a value, and contains only one line, you can also omit the__________.

A

return statement

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

Arrow functions don’t have their own bindings to ____ , ______ ,______ and should not be used as methods.

A

this, arguments, or super,

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

Arrow functions cannot be used as _________. Calling them with new throws a _________ . T

A

constructors
TypeError

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

Arrow functions cannot use ________ within their body and cannot be created as generator functions.

A

Yield

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

// Traditional anonymous function
(function (a) {
return a + 100;
});

write as arrow

A

a => a+100;

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

// Traditional anonymous function
(function (a, b) {
return a + b + 100;
});

write as arrow

A

(a, b) => a + b + 100;

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

// Traditional anonymous function (no parameters)
(function () {
return a + b + 100;
});

write as arrow

A

() => a + b + 100;

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

// Traditional anonymous function
(function (a, b) {
const chuck = 42;
return a + b + chuck;
});

write as arrow

A

// Arrow function

(a, b) => {
const chuck = 42;
return a + b + chuck;
};

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

If the arrow function needs to call itself, use a named ____________ instead.

A

function expression

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

// Traditional Function
function bob(a) {
return a + 100;
}

write as arrow

A

const bob2 = (a) => a + 100;

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

const func2 = (x, y) => {

};

what is missing

A

return x + y;

In a concise body, only a single expression is specified, which becomes the implicit return value. In a block body, you must use an explicit return statement.

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

const func = () => { foo: 1 };
// Calling func() returns undefined!

how to fix?

A

const func = () => ({ foo: 1 });

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

For similar reasons, the_______, _______, _______methods are not useful when called on arrow functions, because arrow functions establish this based on the scope the arrow function is defined within, and the this value does not change based on how the function is invoked.

A

call(), apply(), and bind()

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

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

function expression or function declaration.?

A

function declaration.

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

let userName = ‘John’;

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

showMessage(); returns?

A

// Hello, John

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

let userName = ‘John’;

function showMessage() {
userName = “Bob”; /

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

alert( userName );
showMessage();
alert( userName );

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
21
Q

let userName = ‘John’;

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

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

A

// 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
22
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
23
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
24
Q

We can specify the so-called “default” (to use if omitted) value for a parameter in the function declaration, using _______:

A

=

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
function showMessage(from, _______) { alert( from + ": " + text ); } showMessage("Ann"); what to write if you want default value "no text given"
function showMessage(from, text = "no text given") { alert( from + ": " + text ); }
26
function showMessage(from, text) { if (//_____ insert here) { text = 'no text given'; } insert text to check if undfined
function showMessage(from, text) { if (text === undefined) { text = 'no text given'; }
27
function showMessage(from, text) { ... } insert text to check undefined using and operator ||
text = text || 'no text given';
28
function showCount(count) { alert(count ?? "unknown"); } showCount(0); showCount(null); showCount();
showCount(0); // 0 showCount(null); // unknown showCount(); // unknown
29
It is possible to use return without a value. That causes the function to ____________
exit immediately.
30
If a function does not return a value, it is the same as if it returns __________:
undefined
31
function doNothing() { return; } alert( doNothing() === undefined ); above returns?
true
32
function doNothing() { return; } the above returns
undefined
33
function checkAge(age) { if (age > 18) { return true; } else { return confirm('Did parents allow you?'); } } write both using || &&
function checkAge(age) { return (age > 18) ? true : confirm('Did parents allow you?'); } function checkAge(age) { return (age > 18) || confirm('Did parents allow you?'); }
34
function min(a, b) { return a < b ? a : b; } write using if else
function min(a, b) { if (a < b) { return a; } else { return b; } }
35
function min(a, b) { if (a < b) { return a; } else { return b; } } write using ?
function min(a, b) { return a < b ? a : b; }
36
let sayHi = function() { alert( "Hello" ); }; above decleration or expresssion?
Function Expression
37
there’s no name after the function keyword in a function ________
expression
38
function sayHi() { alert( "Hello" ); } alert( sayHi );
// shows the function code
39
Function ________ have a semicolon ; at the end, but Function ________ do not:
Function Expressions Declarations
40
A Function ________ is created when the execution reaches it and is usable only from that moment.
Expression
41
A Function _________ can be called earlier than it is defined.
Declaration
42
sayHi("John"); function sayHi(name) { alert( `Hello, ${name}` ); }
// Hello, John
43
sayHi("John"); let sayHi = function(name) { alert( `Hello, ${name}` ); };
// error!
44
Function _________ are created when the execution reaches them. That would happen only in the line (*). Too late.
Expressions
45
That’s because a Function ________ is only visible inside the code block in which it resides.
Declaration
46
As a rule of thumb, when we need to declare a function, the first thing to consider is _________ syntax
Function Declaration
47
Function __________ are processed before the code block is executed. They are visible everywhere in the block.
Declarations
48
let double = function(n) { return n * 2 } write as an arrow
let double = n => n * 2;
49
Sometimes we need a more complex function, with multiple expressions and statements. In that case, we can enclose them in curly braces. The major difference is that curly braces require a_________
return
50
Sometimes we need a more complex function, with multiple expressions and statements. In that case, we can enclose them in ________ The major difference is that curly braces require a return
curly braces.
51
function ask(question, yes, no) { if (confirm(question)) yes(); else no(); } ask( "Do you agree?", function() { alert("You agreed."); }, function() { alert("You canceled the execution."); } );
function ask(question, yes, no) { if (confirm(question)) yes(); else no(); } ask( "Do you agree?", () => alert("You agreed."), () => alert("You canceled the execution.") );
52
arrow functions can’t be used as ________. They can’t be called with new.
constructors.
53
arrow functions can’t be used as constructors. They can’t be called with ______.
news
54
Arrow functions: Do not have ____ Do not have ______ Can’t be called with ____
this` arguments new
55
The ________ keyword refers to the object that the function is accessed on — it does not refer to the currently executing function, so you must refer to the function value by name, even within the function body.
this
56
JavaScript has four kinds of functions
Regular Generator Async Async generator
57
const multiply = new Function("x", "y", "return x * y"); what type of function?
constructor
58
function multiply(x, y) { return x * y; what type of function?
Declaration
59
const multiply = function (x, y) { return x * y; what type of function?
Expression
60
const obj = { multiply(x, y) { return x * y; }, }; what type of function?
Method
61
The Function() constructor, function expression, and function declaration syntaxes create full-fledged function objects, which can be constructed with ______.
new
62
The Function() ______, _____, _______, and function declaration syntaxes create full-fledged function objects, which can be constructed with new.
constructor, expression, and declaration
63
The function _______ creates functions that are hoisted.
declaration
64
The ______ function and Function() _______ always create anonymous functions, which means they can't easily call themselves recursively.
arrow constructor
65
three special function parameter syntaxes:
-defualt -rest (...rest) -deconstructing: allows unpacking elements from arrays, or properties from objects, into distinct variables.
66
In ________ functions inside blocks are scoped to that block.
strict mode,