DOM Element Object & JS Scope Flashcards

1
Q

.appendChild()

.replaceChild()

.removeChild()

A

methods to add/replace/remove children nodes to an element

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

.innerHTML

.innerText

.textContent

A

element.innerHTML //sets or returns the content of an element
//will process HTML use carefully

element.innerText //the text content without text spacing and tags, except script and style elements

element.textContent //sets or returns only textual content of a node

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

.id

.className

.tagName

.classList

A

.id //sets or returns the element id

.className //sets or returns the element class

.tagName // read-only returns the tag name

.classList // returns a list of the element’s class names

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

.focus()

.blur()

A

element.focus() //gives focus to an element
document.getElementById(“id”).focus();

element.blur() //removes focus from an element

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

element.nodeName

element.nodeType

element.nodeValue

A

.nodeName // returns the name of a node header, etc.

.nodeType // returns the type of a node

.nodeValue // sets or returns the value of a node (text between tags)

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

element.style

A

sets or returns style attribute of an element
element.style.attribute = “”;

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

element.remove()

A

removes the element from the DOM

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

element.setAttribute();

element.removeAttribute();

A

//sets / changes the attribute for an element
element.setAttribute(“class”, “demo”);

element.removeAttribute(“href”);

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

element.appendChild(“li”);

A

appends a child element

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

element.click();

A

performs a click event on the element

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

element.title

A

sets or returns the value of the title attribute

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

What are the three possible scopes of variables?

A

Block
Function | Local
Global

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

Block Scope

A

variables declared inside a { } block
that cannot be accessed outside of it
{
let x = 2;
}

instantiated with let or const
a nested block can access the variable if it comes after.

introduced in ES6

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

Local Scope

A

Variables declared within a JavaScript function, become LOCAL to the function.

function name() {
let myName = ‘Joe’;
}
only exists within the function

Local variables are created when a function starts, and deleted when the function is completed.

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

Function Scope

A

JavaScript has function scope: Each function creates a new scope.

Variables defined inside a function are not accessible (visible) from outside the function.

Variables declared with var, let and const are quite similar when declared inside a function. As they’ll all have function scope.

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

Global Scope

A

Global variables can be accessed from anywhere in a JavaScript program.

DOM collections also have global scope

Window object has global scope

Assigned variables that are not declared have global scope

function name() {
carName = ‘Volvo’; //global scope
}

17
Q

Why are global variables dangerous?

A

Your global variables (or functions) can overwrite window variables (or functions).
Any function, including the window object, can overwrite your global variables and functions.

Global variables can bleed out of conditional (if) blocks.

18
Q

What values are falsy?

A

false,
zero: 0, 0.0, 0x0, -0, 0n
(string.length === 0): “” `` ‘’
null
undefined
NaN

Objects are falsy if and only if they have the [[IsHTMLDDA]] internal slot, only document.all has this.

19
Q

What values are truthy?

A

true,
existing objects: ({}), ([]), (new Date())
non-zero numbers: 42, -2, Infinity, 12n
(string.length > 0): “0”, “text”, “false”