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
Q

function showMessage(from, _______) {
alert( from + “: “ + text );
}
showMessage(“Ann”);

what to write if you want default value “no text given”

A

function showMessage(from, text = “no text given”) {
alert( from + “: “ + text );
}

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

function showMessage(from, text) {
if (//_____ insert here) {
text = ‘no text given’;
}

insert text to check if undfined

A

function showMessage(from, text) {
if (text === undefined) {
text = ‘no text given’;
}

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

function showMessage(from, text) {


}

insert text to check undefined using and operator ||

A

text = text || ‘no text given’;

28
Q

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

showCount(0);
showCount(null);
showCount();

A

showCount(0); // 0
showCount(null); // unknown
showCount(); // unknown

29
Q

It is possible to use return without a value. That causes the function to ____________

A

exit immediately.

30
Q

If a function does not return a value, it is the same as if it returns __________:

A

undefined

31
Q

function doNothing() {
return;
}
alert( doNothing() === undefined );

above returns?

A

true

32
Q

function doNothing() {
return;
}

the above returns

A

undefined

33
Q

function checkAge(age) {
if (age > 18) {
return true;
} else {
return confirm(‘Did parents allow you?’);
}
}

write both using || &&

A

function checkAge(age) {
return (age > 18) ? true : confirm(‘Did parents allow you?’);
}

function checkAge(age) {
return (age > 18) || confirm(‘Did parents allow you?’);
}

34
Q

function min(a, b) {
return a < b ? a : b;
}

write using if else

A

function min(a, b) {
if (a < b) {
return a;
} else {
return b;
}
}

35
Q

function min(a, b) {
if (a < b) {
return a;
} else {
return b;
}
}

write using ?

A

function min(a, b) {
return a < b ? a : b;
}

36
Q

let sayHi = function() {
alert( “Hello” );
};

above decleration or expresssion?

A

Function Expression

37
Q

there’s no name after the function keyword in a function ________

A

expression

38
Q

function sayHi() {
alert( “Hello” );
}

alert( sayHi );

A

// shows the function code

39
Q

Function ________ have a semicolon ; at the end, but Function ________ do not:

A

Function Expressions

Declarations

40
Q

A Function ________ is created when the execution reaches it and is usable only from that moment.

A

Expression

41
Q

A Function _________ can be called earlier than it is defined.

A

Declaration

42
Q

sayHi(“John”);

function sayHi(name) {
alert( Hello, ${name} );
}

A

// Hello, John

43
Q

sayHi(“John”);

let sayHi = function(name) {
alert( Hello, ${name} );
};

A

// error!

44
Q

Function _________ are created when the execution reaches them. That would happen only in the line (*). Too late.

A

Expressions

45
Q

That’s because a Function ________ is only visible inside the code block in which it resides.

A

Declaration

46
Q

As a rule of thumb, when we need to declare a function, the first thing to consider is _________ syntax

A

Function Declaration

47
Q

Function __________ are processed before the code block is executed. They are visible everywhere in the block.

A

Declarations

48
Q

let double = function(n) { return n * 2 }

write as an arrow

A

let double = n => n * 2;

49
Q

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_________

A

return

50
Q

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

A

curly braces.

51
Q

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.”); }
);

A

function ask(question, yes, no) {
if (confirm(question)) yes();
else no();
}

ask(
“Do you agree?”,
() => alert(“You agreed.”),
() => alert(“You canceled the execution.”)
);

52
Q

arrow functions can’t be used as ________. They can’t be called with new.

A

constructors.

53
Q

arrow functions can’t be used as constructors. They can’t be called with ______.

A

news

54
Q

Arrow functions:

Do not have \_\_\_\_
Do not have \_\_\_\_\_\_
Can’t be called with \_\_\_\_
A

this`
arguments
new

55
Q

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.

A

this

56
Q

JavaScript has four kinds of functions

A

Regular
Generator
Async
Async generator

57
Q

const multiply = new Function(“x”, “y”, “return x * y”);

what type of function?

A

constructor

58
Q

function multiply(x, y) {
return x * y;

what type of function?

A

Declaration

59
Q

const multiply = function (x, y) {
return x * y;

what type of function?

A

Expression

60
Q

const obj = {
multiply(x, y) {
return x * y;
},
};

what type of function?

A

Method

61
Q

The Function() constructor, function expression, and function declaration syntaxes create full-fledged function objects, which can be constructed with ______.

A

new

62
Q

The Function() ______, _____, _______, and function declaration syntaxes create full-fledged function objects, which can be constructed with new.

A

constructor, expression, and declaration

63
Q

The function _______ creates functions that are hoisted.

A

declaration

64
Q

The ______ function and Function() _______ always create anonymous functions, which means they can’t easily call themselves recursively.

A

arrow
constructor

65
Q

three special function parameter syntaxes:

A

-defualt
-rest (…rest)
-deconstructing: allows unpacking elements from arrays, or properties from objects, into distinct variables.

66
Q

In ________ functions inside blocks are scoped to that block.

A

strict mode,