Functions Flashcards

1
Q

List all ways to create a function

A

Function declaration:

function myFunction() {}

Function expression:

const myFunction = function() {};

Arrow function expression:

const myFunction = () => {};

Function constructor:

const myFunction = new Function('arg1', 'arg2', 'function body');
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the advantages of using arrow functions in JavaScript?

A

More concise and easier to read;
Lexically-bound of “this” keyword;
Cannot be used as constructors (helps prevent certain types of errors).

Limitations: not able to bind a new this context using call or apply.

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

What is a function declaration?

A

It’s a way to create a named function using the “function” keyword:

function myFunction() {}

Function declarations are hoisted to the top of the scope, which means that they can be called before they are declared.

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

What is a function expression in JavaScript?

A

It’s a way to define a function by assigning it to a variable:

const myFunction = function() { }

Function expressions are not hoisted, so they must be defined before they are called.

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

What is an arrow function expression in JavaScript?

A

It’s a way to define a function using an arrow (=>) instead of the function keyword:

const myFunction = () => { }

Arrow functions have a lexically-bound this keyword and are more concise than traditional function expressions.

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

What is the difference between a function declaration and a function expression?

A

Function declarations are hoisted to the top of their scope, while function expressions are not.

Meaning: function declarations can be called before they are defined, but function expressions must be defined before they are called.

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

What is a higher-order function (HOF)?

A

It’s a function that takes 1 or more functions as arguments or returns a function as its result.

They are a key feature of functional programming, and they can be used to create more modular, reusable, and expressive code.

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

What is a callback function?

A

It’s a function that is passed as an argument to another function and is called when the parent function completes its operation.

They are commonly used for async operations such as event handling, timers, and AJAX requests.

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

What is the difference between call() and apply() methods?

A

Both are methods that allow you to call a function with a specific “this” context and a set of arguments.

The main difference is the way they accept arguments:

“call()” takes the “this” context as the first argument followed by a list of arguments
“apply()” takes the “this” context as the first argument followed by an array of arguments.

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

What is a closure?

A

It’s a function that has access to its parent function’s variables and parameters, even after the parent function has returned.

Closures are created when a function is defined inside another function and returned to the outside world.

Allow to create private variables and methods, and to avoid namespace collisions.

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

What is a function?

A

It’s a block of code that performs a specific task.

Functions are reusable pieces of code that can be called multiple times from different parts of a program.

They can take parameters and return values.

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

What is a method?

A

It’s a function that is a property of an object.

Methods are used to perform actions on objects or to retrieve information from objects.

Methods are defined inside the object and can be called using dot notation on the object.

Example:
myObject.myMethod() // call the method myMethod() on the object myObject.

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

What is the difference between a function and a method?

A

The difference is in the way they are called and the object they belong to.

A function is a standalone block of code that can be called from anywhere in the program
A method is a function that is a property of an object and is called using dot notation on the object.

Additionally, methods have access to the properties and other methods of the object they belong to, while functions do not.

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

What is the this keyword?

A

It refers to the object that the method is called on.

When a method is called using dot notation, the this keyword is set to the object that the method belongs to.

this can be used inside the method to access the properties and methods of the object.

When a function is called standalone, the this keyword is set to the global object (window in a web browser or global in Node.js).

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

What is the difference between a named function and an anonymous function?

A

Named function
- has a name and can be called by that name from anywhere in the program.
Anonymous function
- does not have a name and is usually declared as an expression.
- are often used as callback functions or as arguments to other functions.

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

What is the difference between a function and a method?

A

a function is standalone, a method is a function that belongs to an object

17
Q

What is the this keyword?

A

refers to the object that the method is called on

18
Q

What is an anonymous function?

A

a function without a name

19
Q

What is a named function?

A

a function with a name

20
Q

What’s a typical use case for anonymous functions?

A

They can be used in IIFEs to encapsulate some code within a local scope so that variables declared in it do not leak to the global scope.

(function () {
  // Some code here.
})();

As a callback that is used once and does not need to be used anywhere else. The code will seem more self-contained and readable when handlers are defined right inside the code calling them, rather than having to search elsewhere to find the function body.

setTimeout(function () {
  console.log('Hello world!');
}, 1000);

Arguments to functional programming constructs or Lodash (similar to callbacks).

const arr = [1, 2, 3];
const double = arr.map(function (el) {
  return el * 2;
});
console.log(double); // [2, 4, 6]
21
Q

Explain the difference between synchronous and asynchronous functions.

A
  • Sync functions are blocking while async functions are not.
  • In sync functions, statements complete before the next statement is run. In this case, the program is evaluated exactly in order of the statements and execution of the program is paused if one of the statements take a very long time.
  • Async functions usually accept a callback as a parameter and execution continue on the next line immediately after the asynchronous function is invoked. The callback is only invoked when the asynchronous operation is complete and the call stack is empty.
  • Heavy duty operations such as loading data from a web server or querying a database should be done asynchronously so that the main thread can continue executing other operations instead of blocking until that long operation to complete (in the case of browsers, the UI will freeze).

Function Declaration

foo(); // 'FOOOOO'
function foo() {
  console.log('FOOOOO');
};

Function Expression

foo(); // Uncaught TypeError: foo is not a function
var foo = function () {
  console.log('FOOOOO');
};
22
Q

Explain the differences on the usage of foo between function foo() {} and var foo = function() {}

A

1st = function declaration
2nd = function expression.

function declarations have its body hoisted but the bodies of function expressions are not (they have the same hoisting behavior as variables).

If you try to invoke a function expression before it is defined, you will get an Uncaught TypeError: XXX is not a function error.

23
Q

Can you give an example of a curry function and why this syntax offers an advantage?

A

Currying is a pattern where a function with more than one parameter is broken into multiple functions that, when called in series, will accumulate all of the required parameters one at a time.

This technique can be useful for making code written in a functional style easier to read and compose. It’s important to note that for a function to be curried, it needs to start out as one function, then broken out into a sequence of functions that each accepts one parameter.