Functions Flashcards

1
Q

What is the invocation context of a function ?

A

It is the value of the ‘this’ keyword.
Usually, when a function that is a property of an object is invoked on (or through) this object, that object is the invocation context or the ‘this’ value for the function.

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

Javascript functions are closures. What does it mean ?

A

It mean that javascript functions can be nested within other functions, and they have access to any variables that are in scope where there are defined.

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

What are the different ways to create functions in javascript ?

A
  • Using the function keyword either as a declaration or as an expression
  • Using the shorthand syntax (arrow functions)
  • In object litterals and class definitions, using the shorthand syntax for declaring methods. As well as getters and setters.
  • Using the Function constructor
  • Using specialized kind of functions as : generators (function*) and async functions.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does the following mean: Function declarations are hoisted ?

A

Function declarations are hoisted to the top of the enclosing script, function or block so that functions defined in this way may be invoked from code that appear before the definition.

It means that all of the function declared in a block of javascript code will be defined throughout that block, and they will be defined before the javascript interpreter begins to execute any of the code in that block.

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

Functions are objects.
True or False ?

A

True

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

What is a function in JavaScript?

A

A block of JavaScript code that is defined once but may be executed any number of times.

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

What are parameters in a JavaScript function?

A

Identifiers that work as local variables for the body of the function.

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

What are arguments in a function invocation?

A

Values provided to a function’s parameters during invocation.

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

What is the return value of a function?

A

The value computed by the function using its argument values.

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

What does the ‘this’ keyword represent in a function invocation?

A

The invocation context of the function.

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

What is a method in JavaScript?

A

A function assigned to a property of an object.

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

What are constructors in JavaScript?

A

Functions designed to initialize a newly created object.

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

How are JavaScript functions treated in the language?

A

JavaScript functions are objects and can be manipulated by programs.

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

What is a closure in JavaScript?

A

A function that has access to its own scope, the outer function’s scope, and the global scope.

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

What is the most straightforward way to define a JavaScript function?

A

Using the function keyword.

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

What are arrow functions in JavaScript?

A

A compact syntax for defining functions introduced in ES6, using the => syntax.

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

What is the syntax for a function declaration?

A

function keyword, followed by an identifier, parentheses with parameters, and curly braces with statements.

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

What is hoisting in JavaScript functions?

A

Function declaration statements are moved to the top of their enclosing block, allowing them to be invoked before their definition.

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

What happens if a function does not contain a return statement?

A

It executes until the end and returns undefined.

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

True or False: In ES6, function declarations are only allowed at the top level.

A

False.

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

What is the difference between function declarations and function expressions?

A

Declarations are hoisted and can be called before their definition; expressions are not hoisted.

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

Fill in the blank: Arrow functions use the _______ syntax to separate parameters from the function body.

A

=>

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

What must be included when returning an object literal from an arrow function?

A

The object literal must be inside parentheses.

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

What is the significance of the ‘this’ keyword in arrow functions?

A

Arrow functions inherit the value of ‘this’ from the surrounding context.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
What is a nested function in JavaScript?
A function defined within another function.
26
How do nested functions access variables?
They can access parameters and variables of the outer function.
27
28
29
What is a nested function?
A nested function is simply a function defined inside another function.
30
Provide an example of a nested function.
function outer() { function inner() { console.log("I'm nested!"); } inner(); }
31
What is a closure?
A closure is a function that remembers and has access to variables from the scope in which it was defined, even after that scope has exited.
32
Provide an example of a closure.
function outer() { let count = 0; return function inner() { count++; console.log(count); } } const counter = outer(); counter(); // 1 counter(); // 2
33
What is the key difference between a nested function and a closure?
All closures are nested functions, but not all nested functions are closures.
34
When does a nested function become a closure?
A nested function becomes a closure only when it accesses variables from its outer scope after the outer function has returned.
35
True or False: All nested functions are closures.
False
36
True or False: A closure is a structural concept.
False
37
Fill in the blank: A nested function is a function defined ______ another function.
inside
38
Fill in the blank: A closure is a function that remembers and has access to variables from the ______ in which it was defined.
scope
39
What does a closure allow a function to do after the outer function has returned?
Access variables from the outer function's scope.
40
What determines the value of `this` inside a regular function in JavaScript?
How the function is called
41
How do arrow functions handle the value of `this`?
They inherit the `this` value from the environment in which they were defined
42
What is the term for the way arrow functions handle `this`?
Lexical scoping
43
In the example of a regular function, what does `this` refer to when calling `person.greet()`?
The object `person`
44
What happens when a regular function is called without an object context?
This becomes the global object, so `this.name` is undefined
45
What will `person.greet()` output if `greet` is defined as an arrow function?
Hi, I'm undefined
46
Why does `this` not refer to `person` in the arrow function example?
Because the arrow function inherits `this` from its surrounding context
47
What is a typical use case for arrow functions?
Callbacks, especially in classes or objects where you want `this` to refer to the enclosing object
48
In the `Timer` example, what does `this` refer to when using an arrow function in `setInterval`?
The Timer instance
49
Can arrow functions be used as constructors?
No
50
What will happen if you try to use an arrow function with `new`?
TypeError: Person is not a constructor
51
What is a key difference between regular functions and arrow functions regarding `prototype`?
Regular functions have a `prototype`, arrow functions do not
52
Fill in the blank: Arrow functions are mostly used for _______ and _______.
small functions and callbacks
53
True or False: Arrow functions are good for methods.
False
54
What output does the `Timer` example produce every second?
1, 2, 3, 4...
55
What is the main advantage of using arrow functions in callbacks?
They provide lexical `this`
56
In the summary table, which type of function is good for methods?
Regular Function
57
In the summary table, which type of function is marked as usually not good for methods?
Arrow Function
58
59
60
What does `this` refer to in JavaScript?
`this` refers to the execution context — the object that is currently executing the function.
61
What does `this` refer to in the global context (outside any function or class)?
In browsers: `window`, in Node.js: `global`.
62
What is the value of `this` inside a non-strict mode function?
In browsers: `window`.
63
What is the value of `this` inside a strict mode function?
`undefined`.
64
What does `this` refer to inside an object method?
The object the method was called on.
65
What happens to `this` in arrow functions?
Arrow functions do not bind their own `this`, they inherit it from their lexical scope.
66
What does `this` refer to when using a constructor function?
The newly created object.
67
What does `this` refer to inside a class method?
The instance of the class.
68
What do the methods call(), apply(), and bind() do in relation to `this`?
They let you manually control what `this` points to.
69
What is the difference between call() and apply()?
Both immediately invoke the function with `this` set to the first argument, but apply() takes an array of arguments.
70
What does `this` refer to in a regular function inside setTimeout?
Global object (in browsers).
71
What does `this` refer to in an event handler using a regular function?
The DOM element triggering the event.
72
What does `this` refer to in an event handler using an arrow function?
Lexical `this`, often `window`.
73
Fill in the blank: When a function is used with ______, `this` refers to the newly created object.
[new]
74
True or False: In strict mode, `this` is always `undefined`.
False.
75
What is the value of `this` in a DOM inline event handler?
The button element.
76
List the contexts and their corresponding `this` values from the summary table.
* Global scope (non-strict): Global object (window or global) * Global scope (strict mode): undefined * Inside a method: The object the method was called on * Inside a regular function: Depends on call site * Inside a constructor: The newly created object * Inside an arrow function: Inherits from enclosing scope * With call, apply, or bind: The explicitly provided object * In event handler (regular func): The DOM element triggering the event * In event handler (arrow func): Lexical this, often window * In setTimeout / setInterval: Global object unless arrow function * Inside class methods: The instance of the class.
77
What are nested functions in JavaScript?
Functions that are defined within other functions.
78
What can nested functions access?
Parameters and variables of the function they are nested within.
79
When is the body of a JavaScript function executed?
When the function is invoked.
80
How many ways can JavaScript functions be invoked?
Five ways: * As functions * As methods * As constructors * Indirectly with call() and apply() * Implicitly via language features.
81
What is an invocation expression in JavaScript?
A function expression followed by parentheses containing arguments.
82
What happens to argument expressions in a function invocation?
They are evaluated and the resulting values become the arguments to the function.
83
What is the return value of a function invocation if it reaches the end without a return statement?
Undefined.
84
What does the syntax f?.(x) do in JavaScript?
Invokes the function only if it is not null or undefined.
85
What is the invocation context in non-strict mode?
The global object.
86
What is a recursive function?
A function that calls itself.
87
What is the stack in relation to recursive functions?
A structure that keeps track of execution contexts for each function call.
88
What does a method in JavaScript refer to?
A function stored in a property of an object.
89
How is a method invoked?
Using the syntax o.m(); where o is the object and m is the method.
90
What is the significance of the this keyword in method invocations?
It refers to the object through which the method is invoked.
91
What is method chaining?
Invoking multiple methods on the same object in a single expression.
92
What does the keyword new signify in a function invocation?
It indicates a constructor invocation.
93
What is a constructor function's typical behavior regarding return values?
It initializes a new object and returns it implicitly.
94
What is the purpose of the call() and apply() methods in JavaScript?
To invoke a function indirectly and specify the this value.
95
What can cause implicit function invocations in JavaScript?
Features like getters/setters, toString(), valueOf(), iterables, and tagged template literals.
96
True or False: Functions defined as expressions are hoisted.
False.
97
Fill in the blank: A _______ function is one that calls itself.
recursive