Vanilla JavaScript Flashcards

1
Q

How do you throw an error?

A

throw new Error(‘this is the error’)

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

How do you add JavaScript to html?

A

<head>



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

How do you reference a DOM element by its ID?

A

document.getElementById(‘my-id’);

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

How do you reference a DOM element by a CSS selector (both first element or all elements)?

A

document.queySelector(‘div’);
const divItems = document.querySelectorAll(‘div’);
// can then iterate through divItems using forEach or reference elements like divItems[1]

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

How do you reference a DOM element by its CSS class?

A

document.getElementsByClassName(‘my-class’);

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

How do you change the styling of an element, like color?

A

const item = getElementById(‘my-id’);
item.style.color = ‘blue’
OR
item.setAttribute(‘color’, ‘blue’);
// generally shouldn’t do this; should set with CSS instead

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

How do you change the text of an element?

A

element.textContent = ‘My new content’;
OR
element.setAttribute(‘textContent’, ‘My new content’);

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

How do you add and remove CSS classes to an element?

A

element.classList.add(‘my-class’, ‘my-second-class’);
element.classList.remove(‘my-class’);

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

How do you create a new DOM node, like paragraph?

A

const p = document.createElement(‘p’);
p.textContent = ‘Hello’;
document.body.append(p); // can also .prepend(p)

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

How do you create multiple new DOM nodes at once, like list items?

A

const parent = document.querySelector(‘ol’);
const fragment = document.createDocumentFragment();
for (let i = 0; i < 3; i++) {
const li = document.createElement(‘li’);
li.textContent = List item ${i};
fragment.append(li);
}
parent.append(fragment);

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

How do you delete an element from the DOM?

A

element.remove();

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

How do you get the list items within a ul with an id of ‘my-id’?

A

const ul = document.getElementById(‘my-id’);
const listItems = ul.querySelectorAll(‘li’);

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

How would you log to the console if a button was clicked?

A

const button = document.querySelector(‘button’);
const onClick = () => console.log(‘you clicked the button’);
button.addEventListener(‘click’, onClick);

// can remove event listener with button.removeEventListener(‘click’, onClick);
// double click is ‘dblclick’

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

How would you create an event listener for any key that is clicked?

A

window.addEventListener(‘keydown’, (event) => { … })

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

How do you create a promise?

A

const promise = new Promise((resolve, reject) => {
resolve(someValue);
// reject(new Error(‘something went wrong’))
})
promise.then(value => console.log(value)).catch(error => console.log(error))

// a promise is in a “pending” state until it is either resolved (“fulfilled”) or rejected
// you can also chain thens, eg
promise
.then(value => value * 2)
.then(value => value + 1)
.then(value => console.log(value))
.catch(error => console.log(error))
.finally(() => console.log(‘done’))
// finally will always run; it doesn’t take a parameter

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

How can you wait for all promises to be fulfilled before returning?

A

const [value1, value2] = Promise.all([promise1, promise2])

// can also use Promise.any to return first fulfilled promise or Promise.race to return first fulfilled or rejected promise

17
Q

How do you call an endpoint?

A

const response = await fetch(url, {
method: “POST”,
headers: {
“Content-Type”: “application/json”,
},
body: JSON.stringify({ key: “value” }),
});
const data = await response.json();
// wrap the above in a try catch

OR

fetch(url)
.then(response => response.json())
.then(data => console.log(data))

18
Q

What is closure?

A

A function along with a saved reference to the lexical environment in which it was defined; this is set when the function is declared, not when it’s called; the lexical environment is an internal data structure used for keeping track of variable and function names and their values

When an identifier (variable or function) is not defined locally, JS will look to the parent environment, then to the parent’s parent environment and so on up to the global environment

19
Q

What’s the difference between defining a variable using let or var?

A

var is globally scoped; let is locally scoped

20
Q

What is the keyword this?

A

A reference to the context in which the current code is running
At the top level of the file (or a standard function without strict mode), ‘this’ refers to the global object which is the ‘window’
In an object method, ‘this’ refers to the object
Arrow functions do not create their own ‘this’ context, instead they retain the value of the enclosing context

21
Q
A