FreeCodeCamp JS interview studying Flashcards
studying https://www.freecodecamp.org/news/javascript-interview-prep-cheatsheet/
var
- var is functionally or globally scoped
- can be redeclared
- can be redeclared without initialization
- can be updated
let
- let is block scoped
- cannot be re-declared within it’s scope.
- can be declared without initialization
- can be updated
const
- const is block scoped
- cannot be re-declared within it’s scope
- must be initialized at the time of declaration
- can never be updated
==
only checks for the value. performs coercion before checking.
===
checks for value + type. no coercion.
what are the most frequently used array methods in JS?
map, filter, find, reduce, forEach
The map()
array method
- map creates a new copy of the original array
- use when you want to do something with elements without changing the OG array
- iterates over OG array and takes a callback fn as an arg.
- map tells it what to do with each element
The filter()
array method
- filter creates a NEW array with elements that meet the given condition(s)
the forEach()
array method
- similar to map() with 2 key differences.
- unlike map(), forEach() does NOT return a new array
- you cannot do method chaining with forEach()
- does NOT mutate the original array
What are the two ways to make a function in JS?
- normal function
- arrow function
What are the 3 types of scope?
- Global (declaration outside of any function)
- Function (declaration inside a function)
- Block (declaration inside a block)
What is a closure?
An inner function returned by an outer function that references a variable defined in the outer function, ie. a “closed-over-variable”
Even when functions are returned (in the above case y) they still remember their lexical scope (where it came from)
So, when we finally invoke z, y is invoked. Now, y has to log a so it first tries to find 🔍 it in the local memory but it’s not there. It goes to its parent function. It finds a there.
What is a lexical environment? How is it related to a closure?
It is essentially the surrounding state – the local memory along with the lexical environment of its parent.
What are the advantages of closures in JavaScript?
- Currying
- Data hiding/encapsulation
What are the disadvantages of closures in JavaScript?
Overconsumption of memory or memory leaks can happen.
For example, the closed-over-variable will not be garbage collected. This is because, even if the outer function has run, the returned inner function still has a reference to the closed-over-variable.
Note: Garbage collection basically removes unused variables from the memory automatically.