Working with Functions Flashcards
What is a function?
Generally speaking, a function is a “subprogram” that can be called by code external (or internal, in the case of recursion) to the function. Like the program itself, a function is composed of a sequence of statements called the function body. Values can be passed to a function as parameters, and the function will return a value.
“Functions - JavaScript | MDN” (developer.mozilla.org). Retrieved August 7, 2023.
Why are functions first class objects in JavaScript?
A programming language is said to have First-class functions when functions in that language are treated like any other variable. For example, in such a language, a function can be passed as an argument to other functions, can be returned by another function and can be assigned as a value to a variable.
“Functions - JavaScript | MDN” (developer.mozilla.org). Retrieved August 7, 2023.
What is a function declaration?
A function definition (also called a function declaration, or function statement) consists of the function
keyword, followed by:
- The name of the function.
- A list of parameters to the function, enclosed in parentheses and separated by commas.
- The JavaScript statements that define the function, enclosed in curly brackets,
{ /* … */ }
.
Example:
function square(number) { return number * number; }
“Function declarations” (developer.mozilla.org). Retrieved August 7, 2023.
What is a function expression?
A function expression is a function defined within an expression.
Example:
const getRectArea = function (width, height) { return width * height; }; console.log(getRectArea(3, 4)); // Expected output: 12
“Function expression - JavaScript | MDN” (developer.mozilla.org). Retrieved August 7, 2023.
How is a function called in JavaScript?
A function is called by its name, followed by parentheses with possible parameters. Example: functionName(parameters);
.
“Calling functions” (developer.mozilla.org). Retrieved August 7, 2023.
What is a recursive function in JavaScript?
A recursive function in JavaScript is a function that calls itself to solve a problem. An example of this is a function that calculates the factorial of a number.
function factorial(n) { if (n === 0 || n === 1) { return 1; } else { return n * factorial(n - 1); } }
You could then compute the factorials of 1 through 5 as follows:
console.log(factorial(1)); // 1 console.log(factorial(2)); // 2 console.log(factorial(3)); // 6 console.log(factorial(4)); // 24 console.log(factorial(5)); // 120
“Calling functions” (developer.mozilla.org). Retrieved August 7, 2023.
What is function hoisting in JavaScript?
Hoisting is JavaScript’s behavior of moving declarations to the top. In the context of functions, even if a function is declared after its call in the code, JavaScript will move the function declaration to the top, so it can be called before its declaration.
Consider the example below:
console.log(square(5)); // 25 function square(n) { return n * n; }
This code runs without any error, despite the square()
function being called before it’s declared. This is because the JavaScript interpreter hoists the entire function declaration to the top of the current scope, so the code above is equivalent to:
// All function declarations are effectively at the top of the scope function square(n) { return n * n; } console.log(square(5)); // 25
“Function hoisting” (developer.mozilla.org). Retrieved August 7, 2023.
Does function hoisting work with function expressions in JavaScript?
No, function hoisting only works with function declarations — not with function expressions. The following code will not work:
console.log(square(5)); // ReferenceError: Cannot access 'square' before initialization const square = function (n) { return n * n; };
“Function hoisting” (developer.mozilla.org). Retrieved August 7, 2023.
Explain function scope
Variables defined inside a function cannot be accessed from anywhere outside the function, because the variable is defined only in the scope of the function. However, a function can access all variables and functions defined inside the scope in which it is defined.
For example, a function defined in the global scope can access all variables defined in the global scope. A function defined inside another function can also access all variables defined in its parent function, and any other variables to which the parent function has access.
“Function scope” (developer.mozilla.org). Retrieved September 6, 2023.
What is a recursive function?
A function that calls itself is called a recursive function.
“Recursion” (developer.mozilla.org). Retrieved September 6, 2023.
How can a function call itself?
A function can refer to and call itself using one of the following:
- The function’s name
- arguments.callee()
- An in-scope variable that refers to the function
For example, consider the following function definition:
const foo = function bar() { // statements go here };
Within the function body, the following are all equivalent:
bar()
arguments.callee()
foo()
“Recursion” (developer.mozilla.org). Retrieved September 6, 2023.
What is the call stack?
A call stack is a mechanism for an interpreter to keep track of its place in a script that calls multiple functions — what function is currently being run and what functions are called from within that function, etc.
- When a script calls a function, the interpreter adds it to the call stack and then starts carrying out the function.
- Any functions that are called by that function are added to the call stack further up, and run where their calls are reached.
- When the current function is finished, the interpreter takes it off the stack and resumes execution where it left off in the last code listing.
- If the stack takes up more space than it was assigned, a “stack overflow” error is thrown.
Note: The call stack is also called function stack in some places.
“Call stack - MDN Web Docs Glossary: Definitions of Web-related terms | MDN” (developer.mozilla.org). Retrieved September 6, 2023.
Properties of nested functions and closures
- The inner function can be accessed only from statements in the outer function.
- The inner function forms a closure: the inner function can use the arguments and variables of the outer function, while the outer function cannot use the arguments and variables of the inner function.
Since the inner function forms a closure, you can call the outer function and specify arguments for both the outer and inner function:
function outside(x) { function inside(y) { return x + y; } return inside; } const fnInside = outside(3); // Think of it like: give me a function that adds 3 to whatever you give it console.log(fnInside(5)); // 8 console.log(outside(3)(5)); // 8
“Nested functions and closures” (developer.mozilla.org). Retrieved September 6, 2023.
Explain clojures
In JavaScript, a closure is a fundamental concept that has to do with how functions and their lexical environments work. Let’s break down the components and principles of a JavaScript closure:
- Function Scope: In JavaScript, each function has its own scope. Variables declared within a function are only accessible within that function, and they are not visible to the outside world.
- Lexical Scope: JavaScript uses lexical scoping, which means that a function’s scope is determined by its location in the source code when it’s defined. This is also referred to as “static scope.”
- Nested Functions: You can define functions inside other functions. When you do this, the inner function has access to the variables and parameters of the outer (enclosing) function. This forms a closure.
“Closures” (developer.mozilla.org). Retrieved September 11, 2023.
What is the arguments
object of a function?
The arguments of a function are maintained in an array-like object. Within a function, you can address the arguments passed to it as follows:
arguments[i];
where i
is the ordinal number of the argument, starting at 0
. So, the first argument passed to a function would be arguments[0]
. The total number of arguments is indicated by arguments.length
.
Using the arguments
object, you can call a function with more arguments than it is formally declared to accept. This is often useful if you don’t know in advance how many arguments will be passed to the function. You can use arguments.length
to determine the number of arguments actually passed to the function, and then access each argument using the arguments object.
Note: The arguments variable is “array-like”, but not an array. It is array-like in that it has a numbered index and a length property. However, it does not possess all of the array-manipulation methods.
“Using the arguments object” (developer.mozilla.org). Retrieved September 11, 2023.