Pink API Flashcards

1
Q

What “DOMContentLoaded” used in combination with?

A

document and addEventListener

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

What is the use of “DOMContentLoaded” event?

A

Ensure that your code executes only after the HTML document has been completely loaded and parsed.
It allows you to safely manipulate the DOM elements.

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

Give me a practical use (lines of code) for DOMContentLoaded.

A

document.addEventListener(‘DOMContentLoaded’, function() {
console.log(‘DOM fully loaded and parsed’);
});

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

What’s the difference between DOMContentLoaded and “load” event?

A

The load event waits for all assets like images and stylesheets to load, DOMContentLoaded only waits for the HTML and DOM structure to be ready.

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

What is for in loop ?

A

Only used for an object (with keys and values); loop through keys of an object

for (let key in person) {
console.log(key + “: “ + person[key]);
}

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

What is event delegation?

A

Event delegation is a technique in JavaScript where we delegate the responsibility of handling an event to a parent element (we can avoid multiple event listeners for individual elements)

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

What are key concepts of event delegation?

A
  1. Event Bubbling:

When an event is triggered on an element, it starts from that element and “bubbles” up through its ancestors in the DOM tree until it reaches the root (usually the document object).

  1. Attaching a Single Event Listener:

Instead of adding event listeners to multiple child elements, you attach a single event listener to a parent element.

  1. Event Targeting:

When an event occurs, you can identify the actual element that triggered the event using the EVENT.TARGET property.

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

Explain this code

container.addEventListener(“mouseover”, (event) => {…}

A

Anytime when adding an event listener, “mouseover” is the EVENT TYPE (that i am listening for),
(event) is the parameter for the arrow function; this parameter represents the event object that is automatically passed to the function when the event occurs. You can name this parameter anything you want

(event) is not only a parameter for a callback function, but also an object created by browser when event (“mouseover”) occurred.

Event Object (event):

When the mouseover event occurs (i.e., when the mouse enters an element inside container), the browser creates an event object.
This event object is automatically passed as an argument to the callback function (event) => { … }.
You use event inside the callback function to access information about the event, such as event.target (the element that triggered the event), event.clientX/event.clientY (mouse coordinates), etc.
Handling Multiple Events:

You can attach the same or different callback functions to handle different events (like “click”, “keydown”, etc.) for the same element.
Each event handler function will receive an event object specific to the event type it is handling.
Therefore, (event) is necessary after “mouseover” to denote that the following arrow function (event) => { … } expects to receive an event object as its parameter. This allows you to access properties and methods of the event object inside your function, enabling you to respond to and manipulate elements based on the event that occurred (in this case, changing the background color based on the class of the element h

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

Difference between html structure and DOM

A

HTML structure: static markup; made of elements

DOM: live, interactive version of html created by browser; object-based representation of the HTML structure; Document OBJECT model, made of elements (which are objects with properties and methods) ; Each element in DOM is an OBJECT

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

Are elements same as node?

A

No; elements are a TYPE of nodes ;

Element Node: Represents an HTML element
Text Node: Represents the text inside an element.
Comment Node: Represents comments in the HTML.
Document Node: Represents the entire document.

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

Explain HTML box model

A

Each element in HTML is a box

Content: The actual content of the element.
Padding: The space between the content and the border.
Border: The boundary surrounding the padding and content.
Margin: The space outside the border.

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

Explain variables.

A

A variable is a concept in JavaScript (and other programming languages) used to store data. Variables do not exist in HTML.

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

let paragraph = document.createElement(“p”);
paragraph.textContent = “Hello, World!”;

Why do i need to create a variable and store the html element?

A

I dynamically created a new HTML element in JS (which is paragraph) but it is in memory until you add it to the DOM.

  1. Create in memory
  2. Store in a variable
  3. Append to DOM (appendChild or insertBefore)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Is a variable I created in js considered an object?

A

In JavaScript, not every variable is an object by default. It DEPENDS on what it stores; if primitive data types, NOT an object; if reference data types, YES It is an OBJECT

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