Advanced Javascript Flashcards
What is the main idea behind functional programming?
Functional programming aims to bring the precision, consistency, and predictability of mathematical functions into our programming environment. This means striving for “pure” functions that always produce the same output given the same input and do not cause side effects, like altering global state. This emphasis on predictability and consistency can make functional programs easier to test, debug, and understand.
What are the three main concepts behind functional programming?
The three main concepts behind functional programming are immutability, separation of functions and data, and the use of first-class functions.
Immutability: This is the idea that once a data structure is created, it cannot be changed. Any ‘changes’ to the structure result in a new structure being created, leaving the original untouched. This avoids issues with shared state and reduces bugs, especially in concurrent and parallel programming.
Separation of functions and data: In functional programming, data and functions are kept separate. Functions take in data as arguments and produce new data as output, without modifying the original data.
First-class functions: This means functions in the language are treated as any other variable. They can be assigned to variables, stored in data structures, passed as arguments to other functions, and returned as values from other functions.
What is immutability in the context of functional programming?
In functional programming, immutability refers to the concept that once a value is assigned to a variable, it cannot be changed. This means that once a variable is defined, its value stays the same throughout the entire program.
What are the advantages of using immutability in functional programming?
Predictability: With immutability, a variable’s value is constant once assigned, which eliminates concerns about variables’ values changing unpredictably during the execution of a program. This consistency makes the program easier to understand and debug.
Single Source of Truth: An immutable set of data serves as a single source of truth.
Mutation tracking: Immutability facilitates the process of identifying changes in data over time by making use of reference and value equality, which simplifies the identification of changes.
In functional programming, how do you manage situations where you need to update a value, given the concept of immutability?
In functional programming, when a value needs to be updated, a new variable is defined to represent the updated data, leaving the original data unchanged. This way, immutability is preserved, and you still have access to the original data, making it easier to trace the changes and understand the evolution of the data.
What does the separation of data and functions mean in functional programming?
The concept of separating data and functions refers to the idea that data (like variables, arrays, and objects) and functions (the operations we can apply to data) exist independently of each other. Functions operate on data by taking it as input and returning a modified copy (respecting immutability) rather than altering the original data in-place.
What is the effect of the rule of immutability on functions in functional programming?
In functional programming, the rule of immutability states that data cannot be changed once it’s created. This principle directly impacts how functions operate on data. Instead of altering the original data functions in functional programming return a modified copy of the data. This approach respects immutability and prevents side effects (unexpected changes to program state), leading to code that is easier to understand, test, and debug.
What is a first-class function?
A first-class function is a function that can be treated like any other data type in a programming language. This means that first-class functions can be assigned to variables, stored in data structures, passed as arguments to other functions, and returned as results from other functions.
What is the limitation of the const keyword in enforcing immutability in JavaScript?
In JavaScript, while the const keyword prevents reassigning a variable to a new value, it does not ensure complete immutability, especially when it comes to arrays and objects. We can still change the individual elements of an array or properties of an object that’s been declared with const.
What is ESLint and how can it help ensure immutability in JavaScript?
ESLint is a tool for identifying and reporting patterns found in ECMAScript/JavaScript code. It can be used to enforce coding rules and styles. When used with the ESLint-plugin-immutable, it can help in identifying and preventing mutations in JavaScript code, thereby enforcing immutability.
What is a closure in JavaScript?
A closure in JavaScript is a feature where a function returned from another function retains access to the scope of the function that created and returned it. This means that the returned function can still reference variables from the outer function’s scope, even after the outer function has completed execution.
Why are closures useful in JavaScript?
In programming, a closure is a combination of a function and the lexical environment in which it was created. It allows a function to remember and access its lexical scope, even when that function is executing outside its lexical scope.
Closures are useful because they enable data encapsulation, provide a way to maintain persistent state within functions, allow for the creation of private variables and functions, and facilitate the use of callback functions by preserving the context and state of operations.
What is the single responsibility principle and why is it important?
The single responsibility principle is a programming concept that suggests every module, class, or function should have responsibility over a single part of the functionality provided by the software. This principle is important as it makes code more readable, maintainable, and easier to test or debug, as each piece of code has only one job to do.
What are higher-order functions in JavaScript?
Higher-order functions in JavaScript are functions that can receive other functions as arguments and/or return them as results.
What is the reduce function in JavaScript used for ?
The reduce function in JavaScript is used to transform an array into a single value, such as the sum, average, or product of its elements.
What is partial application in programming and how is it beneficial?
Partial application is a technique in programming where a function that takes multiple arguments is transformed into a function with fewer arguments by fixing some of the arguments to certain values. Partial application can be beneficial when we use a function often in our code and some of the arguments rarely change. By fixing those arguments, we can create a more specific function with fewer arguments to manage.
function multiply(x, y, z) { return x * y * z; } function multiplyByTwoAndThree(z) { return multiply(2, 3, z); } let result = multiplyByTwoAndThree(4);
What is recursion in programming?
Recursion in programming refers to a method where a function calls itself to solve a problem. Each time the function calls itself, it does so with a smaller or simpler version of the problem until it reaches a base condition which stops the recursion.
What is ‘recursion depth’ and why is it important?
Recursion depth refers to the number of times a recursive function calls itself before reaching the base case. It’s an important consideration in recursion as deep recursion can lead to a stack overflow error, particularly in languages or environments that don’t optimize for recursive calls.
What do the call and apply methods of a function in JavaScript do?
The call and apply methods in JavaScript are used to invoke a function. The call method takes arguments separately, while the apply method takes arguments as an array. Both methods can also take an initial argument to set the value of ‘this’ within the function
Call example:
~~~
object.objectMethod.call( objectInstance, arguments )
~~~
Apply example:
~~~
object.objectMethod.apply(objectInstance, arrayOfArguments)
~~~