Functions Flashcards
List all ways to create a function
Function declaration:
function myFunction() {}
Function expression:
const myFunction = function() {};
Arrow function expression:
const myFunction = () => {};
Function constructor:
const myFunction = new Function('arg1', 'arg2', 'function body');
What are the advantages of using arrow functions in JavaScript?
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.
What is a function declaration?
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.
What is a function expression in JavaScript?
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.
What is an arrow function expression in JavaScript?
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.
What is the difference between a function declaration and a function expression?
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.
What is a higher-order function (HOF)?
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.
What is a callback function?
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.
What is the difference between call() and apply() methods?
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.
What is a closure?
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.
What is a function?
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.
What is a method?
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.
What is the difference between a function and a method?
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.
What is the this keyword?
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).
What is the difference between a named function and an anonymous function?
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.