FreeCodeCamp JS interview studying Flashcards

studying https://www.freecodecamp.org/news/javascript-interview-prep-cheatsheet/

1
Q

var

A
  • var is functionally or globally scoped
  • can be redeclared
  • can be redeclared without initialization
  • can be updated
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

let

A
  • let is block scoped
  • cannot be re-declared within it’s scope.
  • can be declared without initialization
  • can be updated
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

const

A
  • const is block scoped
  • cannot be re-declared within it’s scope
  • must be initialized at the time of declaration
  • can never be updated
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

==

A

only checks for the value. performs coercion before checking.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

===

A

checks for value + type. no coercion.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what are the most frequently used array methods in JS?

A

map, filter, find, reduce, forEach

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

The map() array method

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

The filter() array method

A
  • filter creates a NEW array with elements that meet the given condition(s)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

the forEach() array method

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the two ways to make a function in JS?

A
  1. normal function
  2. arrow function
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the 3 types of scope?

A
  • Global (declaration outside of any function)
  • Function (declaration inside a function)
  • Block (declaration inside a block)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a closure?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a lexical environment? How is it related to a closure?

A

It is essentially the surrounding state – the local memory along with the lexical environment of its parent.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the advantages of closures in JavaScript?

A
  • Currying
  • Data hiding/encapsulation
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are the disadvantages of closures in JavaScript?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is hoisting in JavaScript?

A

This is JavaScript’s default behavior of moving declarations to the top of the program.

17
Q

How is var hoisted in JS?

A

var declaration is hoisted up and initialized with undefined.

18
Q

How are let and const hoisted in JS?

A

let and const declarations are hoisted up but not initialized.

19
Q

how are function definitions hoisted in JS?

A

function definitions are also hoisted up and stored as they are.

20
Q

What is Implicit Binding?

A

When the value of this depends on how and where we are doing the calling.

21
Q

What is Explicit Binding?

A

Explicit binding is when you force a function to use a certain object as its this.

22
Q

call()

A
  • for explicit binding
    execution: runs instantly
    parameter: list of items
23
Q

apply()

A
  • for explicit binding
    execution: runs instantly
    parameter: array []
24
Q

bind()

A
  • explicit binding
    execution: assign, use later
    parameter: list of items
25
Q

What are some unique qualities of arrow functions?

A
  • this … the value depends on the lexical scope (the outer function where the arrow fn is defined)
  • arrow functions inherit the parent’s context
26
Q

Prototypes and Prototypal Inheritance in JavaScript

A

Whenever we create anything (like an object or function) in JavaScript, the JS Engine automatically attaches that thing with some properties and methods.

All this comes via prototypes.

__proto__ is the object where JS is putting it all.

27
Q

What is Prototypal Inheritance in JavaScript?

A
28
Q

What is the setTimeout() method?

A

The setTimeout() method calls a function or evaluates an expression after a specified number of milliseconds.

use clearTimeout to stop

29
Q

What is the setInterval() method?

A

The setInterval() method calls a function or evaluates an expression after a specified interval,

use clearInterval to stop the timer

30
Q

Promises in JS

A
  • at the heart of async js
  • can be 1 of 3 states (pending, fulfilled, rejected)
  • (resolve/reject) are conventional names, can be anything
  • advantage of cleaner syntax to avoid “callback hell”
31
Q

Polyfills in JS

A

A polyfill is a piece of code (usually JavaScript on the Web) used to provide modern functionality on older browsers that do not natively support it. MDN

32
Q

async vs defer in JS

A
  • Async and defer are boolean attributes which can be loaded along with the script tags. They are useful for loading external scripts into your web page.

If there are multiple scripts which are dependant on each other, use defer. Defer script are executed in the order which they are defined.

If you want to load external script which is not dependant on the execution of any other scripts, use async.

Note: The async attribute does not guarantee the order of execution of scripts.

33
Q

Debounce vs Throttling in JS

A

So, to summarize, debouncing is when the difference between two keyup events is 300 ms.

And throttling is when the difference between two function calls is 300 ms. Basically, the function is called after a certain interval of time.

34
Q

localStorage in JS

A

localStorage: Data persists even after closing your session

35
Q

sessionStorage

A

sessionStorage: You lose your data when your session is over, like when you close the browser on the tab.