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.
What is hoisting in JavaScript?
This is JavaScript’s default behavior of moving declarations to the top of the program.
How is var
hoisted in JS?
var declaration is hoisted up and initialized with undefined.
How are let
and const
hoisted in JS?
let and const declarations are hoisted up but not initialized.
how are function
definitions hoisted in JS?
function definitions are also hoisted up and stored as they are.
What is Implicit Binding?
When the value of this
depends on how and where we are doing the calling.
What is Explicit Binding?
Explicit binding is when you force a function to use a certain object as its this.
call()
- for explicit binding
execution: runs instantly
parameter: list of items
apply()
- for explicit binding
execution: runs instantly
parameter: array []
bind()
- explicit binding
execution: assign, use later
parameter: list of items