JS Foundation I Flashcards

JS Engine JS Runtime Interpreters + Compilers Call Stack + Memory Heap Stack Overflow Garbage Collection Memory Leaks Single Threaded

1
Q

What are some of the advantages/disadvantages of writing JavaScript code in a language that compiles to JavaScript?

A

Advantages:

  • fixes some of the longstanding problems in JS & discourages JS anti-patterns
  • Static types are awesome (in the case of TypeScript) for large projects that need to be maintained over time

Disadvantages:

  • Require a build/compile process as browsers only run JavaScript and your code will need to be compiled into JavaScript before being served to browsers.
  • Most developers are not familiar with these languages and will need to learn it. There’s a ramp up cost involved for your team if you use it for your projects.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is event loop? What is the difference between call stack and task queue?

A

The event loop is a single-threaded loop that monitors the call stack and checks if there is any work to be done in the task queue. If the call stack is empty and there are callback functions in the task queue, a function is dequeued and pushed onto the call stack to be executed.

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

What is the Memory Heap?

A
- store and read/write information, where memory allocation happens
Free store, large region
Like bookshelves, cupboard
No order 
Use variables to store 
Data into storage boxes for us
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the Call Stack?

A
  • the engine keep track of where we are in the code, in execution
  • After running the function, it’ll be removed
  • Stores functions & variables as it executes
  • LIFO - Last In First Out
  • Stack pushed on, popped off
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is Stack Overflow?

A
  • Functions nested inside of each other, growing the stack.
  • Gets larger & larger & larger & larger > stack overflow
  • Recursion, fxn calling itself
    Most common way to cause a stack overflow
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Explain Garbage Collection

A

JS is a garbage collected language
Don’t use up all the memory that’s available
Freeze memory in the heap, when memory gets too big to prevent memory leaks

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

Explain Memory Leaks

A

Failure in the program to release/clear out discarded memory, causing failure or laggy performance.

<b>3 common memory leaks:</b>
Global variables
Event listeners
setInterval

B/c memory is limited, be conscious to not have a memory leak or stack overflow

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

Explain JS as a single threaded language

A

Using one hand to eat food, one at a time

JS is synchronous, only 1 thing can happen at a time
Previous func has to finish running before the next one runs

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

Explain JS Runtime

A

Web API - provided by the browser, not native to JS
Asynchronous
Ie: fetch, setTimeout, indexDB
Browsers are helping us create rich web applications
If browser had to use same js thread for all app, it’ll be really slow
Browser uses low level C++ lang
JS will pass onto Web API from call stack when it doesn’t recognize (ie: fetch)

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