JavaScript, DOM, events, and timers

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/23

encourage image

There's no tags or description

Looks like no tags are added yet.

24 Terms

1
New cards

Variables

Prefer const when not reassigning and let for reassignment; both have block scope, unlike var's function scope

2
New cards

Arrays

Arrays have zero-based indices and length

3
New cards

Objects

Objects have named properties.

obj.key and obj['key'] read a property

4
New cards

JS Types

Boolean, number, BigInt, String, Symbol, Null, Undefined

5
New cards

Type Coercion

Strings can trigger type coercion, the + operator adds numbers but concatenates when either operand is a string.

strings and numbers differ under +

6
New cards

Arrow expressions

Arrow expression bodies return implicitly, block bodies need return.

7
New cards

Event-driven Programming

Unlike Java, JS programs have no main. They respond to user actions called events.

8
New cards

DOM

The DOM is a tree of elements and other node. Every HTML element becomes a JS accessible object. JS can inspect and update the DOM.

9
New cards

Query Selector

querySelector('selector') returns the first match or null (no error if no match)

querySelectorAll returns a NodeList of all matches

10
New cards

Callback Function Syntax

knowt flashcard image
11
New cards

Get element

getElementById('name') required you to omit the # when passing id. Returns the DOM object for a node with the given id. ID does not exist, no error.

12
New cards

Text

knowt flashcard image
13
New cards

Managing CSS Classes

knowt flashcard image
14
New cards

Creating & Adding Elements

knowt flashcard image
15
New cards

InnerHTML Flaw

knowt flashcard image
16
New cards

How should you pass a function to addEventListener, and why?

Pass the function reference without parentheses (handleClick), not the call result (handleClick()). Adding parentheses runs the function immediately on page load and passes its return value (usually undefined) instead of the function itself.

17
New cards

How do you pass custom arguments to a handler function inside addEventListener?

Wrap the call in an arrow function:

btn.addEventListener('click', (e) => handleClick(e, id));

18
New cards

What is the difference between event.currentTarget and event.target?

  • event.currentTarget: The element whose listener is actually running (the element .addEventListener() was attached to).

  • event.target: The exact innermost element that triggered the event (which can be a descendant, like a <span> or <svg> inside a <button>).


19
New cards

When should you use event.currentTarget instead of event.target?

  • Use event.currentTarget when you want to modify the element that owns the listener (e.g., disabling a clicked button), so clicking a child icon inside it doesn't target the wrong element.


20
New cards

Why and how do you use the defer attribute on a <script> tag in the <head>?

<script src="app.js" defer></script> downloads the script in parallel while HTML parses, then waits to execute it until the DOM is fully built—preventing null errors when selecting DOM elements. (Note: <script type="module"> is deferred automatically.)


21
New cards

Which timer method runs a callback once after a delay, and which runs it repeatedly? What are their parameters?

  • setTimeout(callback, ms) — Runs callback once after at least ms milliseconds.

  • setInterval(callback, ms) — Runs callback repeatedly every ms milliseconds.

  • Rule: Pass a function reference (tick), not a call (tick())


22
New cards

How do you stop a setTimeout or setInterval before it finishes or repeats again?

  1. Store the ID returned when creating the timer:

    javascript

    const id = setInterval(updateClock, 1000);
    


  2. Pass that ID to clearTimeout(id) or clearInterval(id) to cancel it


23
New cards

Why does this fail to point to the clicked DOM element inside an arrow function handler, and what should you use instead?

  • Why this fails: An arrow function (() => {}) does not have its own this; it inherits this from its outer scope (often window or undefined).

  • What to use instead: event.currentTarget — it unambiguously points to the element whose listener is running, regardless of function type.


24
New cards

Why is event.currentTarget safer and more unambiguous than this or event.target inside an event listener?

event.currentTarget always refers to the element whose listener is actually running, regardless of function syntax or which child element was clicked.