JS behind the scenes Flashcards

1
Q

What is the DOM?

A

The DOM stands for the Document Object Model. It is the structured representation of a HTML document and is used to connect webpages to scripts like those used in JS.

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

Is the DOM apart of JS?

A

No the DOM is not apart of JS. JS and the DOM are two separate things that interact.

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

How do we select HTML elements in JS?

A
We can select HTML elements by using the document.queryselector method. It uses the form:
var x = document.getElementById("myDiv");
x.querySelector("p").innerHTML = "Hello World!";

The above selects the HTML element paragraph(‘p’) from the CSS element x which in this case is a Div.

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

What is MEAN and MERN stack development and what does each component responsible for?

A

MEAN and MERN stack development are a group of technologies that interact using JS to make a comprehensive application.
M - MongoDB makes use of JS to connect to a database providing the back end.
E - ExpressJS is a framework for nodeJS that helps handle client side server request.
A - Angular is a frontend framework that allows us to load one page in our browser that handles the entire website.
R - React is a JS library that allows us to build interactive UIs by loading certain components at a time.
N - node.JS is a runtime environment for JS that allows you to build applications without needing a browser.

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

What do we need to add to code so that it can react to events such as a ‘click’?

A

You must add an event listener. Event listeners are pieces of code that are attached to components in HTML or CSS that wait for a specific event to happen involving that component and execute a function.

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

What is a modal window?

A

A modal window is a window that appears as an overlay.

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

Using code, how can we add the ‘hidden’ property to an element called ‘modal’?

A

We can use the code:

modal.classList.add(‘hidden’);

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

How would I code an event listener that listens for a keypress on a modal element and performs function DoThis with argument x?

A

modal.addEventListener(‘keydown’, DoThis(x)

{ };

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

Write a piece of code that dynamically changes the src of an element called ‘diceEl’ to a pictures (named dice-x.png) of the result that corresponds to the dice roll variable dice_roll?

A

To dynamically change the picture in diceEl associated with the dice roll result stored in dice_roll you would use.
diceEl.src = dice-${dice_roll}.png;

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

Is JS low-level?

A

No JS is high-level as it automatically manages a computers hardware resources for us.

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

Give an example of a programming paradigm and why is JS multi-paradigm?

A

Functional programming or Object-Oriented programming are examples of programming paradigms. JS uses a multiparadigm approach because you can use FP, OOP and Procedural programming with it.

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

Does JS use classes and instances?

A

No. JS is a prototype based language. It uses objects and these objects can be used as the template for new objects or specify their own object properties.

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

Can JS pass functions into other functions?

A

Yes. Functions in JS are treated as variables which means we can return them and pass them to other functions. The eventListener is a good example of this.

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

Is JS strongly typed?

A

No JS uses type coercion so it will try to distinguish each variables datatype at runtime.

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

What feature makes JS non-blocking?

A

The event loop allows JS to take long running tasks and run them on a separate thread in the background while other tasks are executed. Then once they are finished it moves them back through the main thread. This means that JS is non-blocking.

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

What are two components of the JS engine and their functions?

A

The callstack which is where code is put and executed.

The heap where objects in memory are stored.

17
Q

What is the difference between compilation and interpretation?

A

Compilation is the act of transforming source code into a portable file of machine code then executing this machine code.
Interpretation is the act of using an interpreter to run through source code and execute it line by line.

18
Q

What is the AST?

A

The AST is the abstract syntax tree. It is the structure that is made when code is grouped into related content and checked for syntax errors.

19
Q

What are the steps of JIT compilation?

A

Just-in-time compilation follows these steps:

  1. Code is parsed into AST.
  2. The AST is then compiled into machine code.
    2b. Code is compiled into very basic unoptimized code so that execution can begin then as this is happening parts of the code are optimised by the engine and unoptimized code is swapped for optimized code.
  3. The machine code is executed immediately in the call stack.
20
Q

Does node.JS used Web APIs?

A

No it doesn’t. nodeJS allows us to run JS applications without a browser so it doesn’t have access to WEB APIs.

21
Q

What is the structure of a fetch request?

A

fetch requests use a chain of ‘.then’ keywords followed by a statement and a ‘.catch’ to catch errors.

fetch('https://example.com/profile', {
  method: 'POST', // or 'PUT'
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify(data),
})
.then(response => response.json())
.then(data => {
  console.log('Success:', data);
})
.catch((error) => {
  console.error('Error:', error);
});
22
Q

What is a fetch request and why do we use them?

A

Fetch requests are a simple API for ‘fetching’ resources and managing web requests.

23
Q

What does a fetch request return?

A

A fetch request returns a promise.

24
Q

What are the four statuses of a promise?

A

fulfilled - promise succeeded
rejected - promise rejected
pending - not fulfilled or rejected
settled - has either fulfilled or rejected?