Podcast
Questions and Answers
Which of the following best describes the primary role of JavaScript in web development?
Which of the following best describes the primary role of JavaScript in web development?
- Styling the aesthetic appearance of web page elements.
- Defining the structure and layout of web pages.
- Enhancing user interface interactivity and dynamic content. (correct)
- Managing server-side operations and database interactions.
In what sequence does a web browser typically execute JavaScript code embedded within an HTML document?
In what sequence does a web browser typically execute JavaScript code embedded within an HTML document?
- Parallel execution across all available browser threads.
- Top-to-bottom order as the browser parses the HTML. (correct)
- Random order based on script load times.
- Based on the priority flags set within the JavaScript code.
Which of the following statements accurately describes how JavaScript handles whitespace?
Which of the following statements accurately describes how JavaScript handles whitespace?
- Whitespace is significant and alters the functional behavior of the code.
- JavaScript throws an error when excessive whitespace is detected.
- JavaScript condenses all whitespace to a single space character.
- JavaScript generally ignores extra spaces, tabs, and line breaks, using spacing to improve readability. (correct)
Given that JavaScript is case-sensitive, what would be the outcome of declaring two variables named myVariable
and MyVariable
in the same scope?
Given that JavaScript is case-sensitive, what would be the outcome of declaring two variables named myVariable
and MyVariable
in the same scope?
What is the key distinction between an expression and a statement in JavaScript?
What is the key distinction between an expression and a statement in JavaScript?
Which of the following is a valid method for declaring a variable in JavaScript that cannot be reassigned after its initial assignment?
Which of the following is a valid method for declaring a variable in JavaScript that cannot be reassigned after its initial assignment?
In JavaScript, what is the purpose of the typeof
operator?
In JavaScript, what is the purpose of the typeof
operator?
What is the expected behavior when a JavaScript function reaches a return
statement?
What is the expected behavior when a JavaScript function reaches a return
statement?
Given the concept of function hoisting in JavaScript, what is the key implication for function declarations?
Given the concept of function hoisting in JavaScript, what is the key implication for function declarations?
What is meant by 'lexical scope' in JavaScript?
What is meant by 'lexical scope' in JavaScript?
The innerText
property is used to dynamically insert text into an HTML element using JavaScript.
The innerText
property is used to dynamically insert text into an HTML element using JavaScript.
In JavaScript, whitespace, including extra spaces, tabs, and line breaks, is always significant and affects how the code is interpreted.
In JavaScript, whitespace, including extra spaces, tabs, and line breaks, is always significant and affects how the code is interpreted.
In JavaScript, a statement is the same as an expression.
In JavaScript, a statement is the same as an expression.
In javascript the const
keyword can be used to define a variable, and that variable can be reassigned a new value later in the script.
In javascript the const
keyword can be used to define a variable, and that variable can be reassigned a new value later in the script.
JavaScript functions must be defined before they are called, and function hoisting will promote the function declaration to the top of the script.
JavaScript functions must be defined before they are called, and function hoisting will promote the function declaration to the top of the script.
A global variable in JavaScript is accessible only within the function in which it is declared.
A global variable in JavaScript is accessible only within the function in which it is declared.
JavaScript handles different data types in the same way, eliminating the necessity for type conversion in mathematical operations.
JavaScript handles different data types in the same way, eliminating the necessity for type conversion in mathematical operations.
In JavaScript, the parseInt()
function converts a string to an integer, stopping the conversion and ignoring alphabetic characters after the numeric part.
In JavaScript, the parseInt()
function converts a string to an integer, stopping the conversion and ignoring alphabetic characters after the numeric part.
In JavaScript, the addition operator (+) can only be used for summing numbers, whereas concatenation of strings uses a different operator.
In JavaScript, the addition operator (+) can only be used for summing numbers, whereas concatenation of strings uses a different operator.
Relational comparison operators such as >, <, >=, and <= in JavaScript can be used effectively to compare values of different data types, including strings and numbers.
Relational comparison operators such as >, <, >=, and <= in JavaScript can be used effectively to compare values of different data types, including strings and numbers.
Given the following JavaScript object:
const myObject = { name: "Example", process: function() { return "Running"; } };
Which of the following is the correct way to execute the process
method?
Given the following JavaScript object:
const myObject = { name: "Example", process: function() { return "Running"; } };
Which of the following is the correct way to execute the process
method?
When creating a Date
object in JavaScript, which of the following is true regarding the time that is captured by default?
When creating a Date
object in JavaScript, which of the following is true regarding the time that is captured by default?
Consider a JavaScript object book
with a property title
. Which method correctly updates the title
from "Old Title" to "New Title"?
Consider a JavaScript object book
with a property title
. Which method correctly updates the title
from "Old Title" to "New Title"?
What happens when you use a single numeric argument with the new Array(number)
constructor in JavaScript?
What happens when you use a single numeric argument with the new Array(number)
constructor in JavaScript?
How do you remove a property from a JavaScript object?
How do you remove a property from a JavaScript object?
Which of the following is the correct way to represent a new 'Car' object with properties 'brand' as 'Toyota' and 'year' as 2024 in JavaScript?
Which of the following is the correct way to represent a new 'Car' object with properties 'brand' as 'Toyota' and 'year' as 2024 in JavaScript?
What is the primary difference between accessing object properties using dot notation versus bracket notation in JavaScript?
What is the primary difference between accessing object properties using dot notation versus bracket notation in JavaScript?
Given a JavaScript array numbers = [10, 20, 30, 40, 50]
, which loop will correctly output each element of the array along with its index?
Given a JavaScript array numbers = [10, 20, 30, 40, 50]
, which loop will correctly output each element of the array along with its index?
Which of the following statements is true about JavaScript built-in objects like String
, Number
, and Boolean
?
Which of the following statements is true about JavaScript built-in objects like String
, Number
, and Boolean
?
Consider a scenario where you need to extract the month from a Date
object in JavaScript. Which method should you use, and what does it return?
Consider a scenario where you need to extract the month from a Date
object in JavaScript. Which method should you use, and what does it return?
In JavaScript, an object can store multiple, unrelated pieces of data.
In JavaScript, an object can store multiple, unrelated pieces of data.
Object properties can only be accessed using dot notation.
Object properties can only be accessed using dot notation.
In JavaScript, objects are static and cannot be modified once they are created.
In JavaScript, objects are static and cannot be modified once they are created.
An array is a built-in JavaScript object that stores multiple values in a single variable.
An array is a built-in JavaScript object that stores multiple values in a single variable.
All built-in JavaScript objects, including Math
, inherit from Object.prototype
.
All built-in JavaScript objects, including Math
, inherit from Object.prototype
.
If car
is an object with a year
property, the code car.year = 2025
will always create a new property named year2025
on the car object.
If car
is an object with a year
property, the code car.year = 2025
will always create a new property named year2025
on the car object.
Using new Array(10)
creates an array with a single element, the number 10.
Using new Array(10)
creates an array with a single element, the number 10.
JavaScript Date
objects represent date, time, and timezone information.
JavaScript Date
objects represent date, time, and timezone information.
The getMonth()
method of a JavaScript Date object returns the month as a number from 1 to 12.
The getMonth()
method of a JavaScript Date object returns the month as a number from 1 to 12.
JavaScript objects are assigned properties as a semi-colon separated list of name:value pairs within ()
parenthesis.
JavaScript objects are assigned properties as a semi-colon separated list of name:value pairs within ()
parenthesis.
Which of the following is the primary purpose of control structures in JavaScript?
Which of the following is the primary purpose of control structures in JavaScript?
What happens in a JavaScript switch
statement when none of the case
expressions match the switch expression, and no default
case is provided?
What happens in a JavaScript switch
statement when none of the case
expressions match the switch expression, and no default
case is provided?
In the context of a JavaScript for
loop, what is the role of the 'modifier' expression?
In the context of a JavaScript for
loop, what is the role of the 'modifier' expression?
What distinguishes a do...while
loop from a while
loop in JavaScript?
What distinguishes a do...while
loop from a while
loop in JavaScript?
In JavaScript, how does the break
statement affect the execution of a loop?
In JavaScript, how does the break
statement affect the execution of a loop?
What is the function of the continue
statement within a loop in JavaScript?
What is the function of the continue
statement within a loop in JavaScript?
Consider a JavaScript if
statement used to check if a user is an administrator: if (isAdmin) { // admin actions }
. Which of the following best describes the condition being evaluated?
Consider a JavaScript if
statement used to check if a user is an administrator: if (isAdmin) { // admin actions }
. Which of the following best describes the condition being evaluated?
How can you include multiple conditions in an if
statement in JavaScript?
How can you include multiple conditions in an if
statement in JavaScript?
What is the expected output of the following code snippet?
let i = 0;
while (i < 5) {
if (i === 3) {
break;
}
console.log(i);
i++;
}
What is the expected output of the following code snippet?
let i = 0;
while (i < 5) {
if (i === 3) {
break;
}
console.log(i);
i++;
}
What is the primary reason for using the default
case in a switch
statement?
What is the primary reason for using the default
case in a switch
statement?
Control structures primarily manage data storage rather than the sequence of execution in a program.
Control structures primarily manage data storage rather than the sequence of execution in a program.
In JavaScript, an if
statement can only execute code if a given condition is false.
In JavaScript, an if
statement can only execute code if a given condition is false.
Using an else
block in conjunction with an if
statement provides an alternative execution path when the condition is true.
Using an else
block in conjunction with an if
statement provides an alternative execution path when the condition is true.
In a switch
statement, the default
block is executed when none of the case
values match the switch expression.
In a switch
statement, the default
block is executed when none of the case
values match the switch expression.
Omitting a break
statement at the end of a case
in a switch
block prevents the execution of subsequent case
blocks.
Omitting a break
statement at the end of a case
in a switch
block prevents the execution of subsequent case
blocks.
The getDay()
method returns the day of the month as a number.
The getDay()
method returns the day of the month as a number.
In a for
loop, the modifier statement is executed only once at the beginning of the loop.
In a for
loop, the modifier statement is executed only once at the beginning of the loop.
Both while
loops and for
loops are post-test loops, evaluating the condition after executing the statements inside the loop.
Both while
loops and for
loops are post-test loops, evaluating the condition after executing the statements inside the loop.
A do...while
loop is guaranteed to execute its code block at least once, regardless of whether the condition is initially true or false.
A do...while
loop is guaranteed to execute its code block at least once, regardless of whether the condition is initially true or false.
The continue
statement immediately terminates the loop and transfers control to the statement following the loop.
The continue
statement immediately terminates the loop and transfers control to the statement following the loop.
What does the Window.screen.colorDepth
property in JavaScript provide?
What does the Window.screen.colorDepth
property in JavaScript provide?
Given a webpage with multiple images, how does JavaScript's HTML DOM API allow you to access the second image element?
Given a webpage with multiple images, how does JavaScript's HTML DOM API allow you to access the second image element?
Why might accessing elements via index in component arrays (like document.images[0]
) be considered an 'old approach' in modern JavaScript development?
Why might accessing elements via index in component arrays (like document.images[0]
) be considered an 'old approach' in modern JavaScript development?
When using document.write()
to add content to a webpage, what is a key consideration regarding its use?
When using document.write()
to add content to a webpage, what is a key consideration regarding its use?
If you have a dynamically created <select>
element in JavaScript, how can you determine the index of the currently selected option?
If you have a dynamically created <select>
element in JavaScript, how can you determine the index of the currently selected option?
In JavaScript, what is the primary distinction between using innerHTML
and innerText
when modifying the content of an HTML element?
In JavaScript, what is the primary distinction between using innerHTML
and innerText
when modifying the content of an HTML element?
What is the significance of the return value of the window.confirm()
method in JavaScript?
What is the significance of the return value of the window.confirm()
method in JavaScript?
In the context of JavaScript events, what is the difference between the 'HTML Event Attributes' method and the addEventListener
method for handling events?
In the context of JavaScript events, what is the difference between the 'HTML Event Attributes' method and the addEventListener
method for handling events?
When a 'keydown' event is triggered in JavaScript, what is the primary information provided by the event object?
When a 'keydown' event is triggered in JavaScript, what is the primary information provided by the event object?
In JavaScript, how do you access all radio buttons or checkboxes that share the same name
attribute to determine which one is selected?
In JavaScript, how do you access all radio buttons or checkboxes that share the same name
attribute to determine which one is selected?
The DOM organizes a webpage into a hierarchical tree, with components nested under the root object named document
.
The DOM organizes a webpage into a hierarchical tree, with components nested under the root object named document
.
Using Window.screen.width
and Window.screen.height
provides the total screen dimensions, including areas potentially obscured by system UI like taskbars.
Using Window.screen.width
and Window.screen.height
provides the total screen dimensions, including areas potentially obscured by system UI like taskbars.
The property Window.screen.colorDepth
specifies the number of bits used to represent the black and white range of each pixel, which affects the displayable color range.
The property Window.screen.colorDepth
specifies the number of bits used to represent the black and white range of each pixel, which affects the displayable color range.
The window.confirm()
method presents a dialog box to the user with "OK" and "Cancel" options, returning true
if "OK" is selected and false
if "Cancel" is selected.
The window.confirm()
method presents a dialog box to the user with "OK" and "Cancel" options, returning true
if "OK" is selected and false
if "Cancel" is selected.
The Document Object Model's (DOM) document
object allows direct manipulation of the browser window itself, including its dimensions and location.
The Document Object Model's (DOM) document
object allows direct manipulation of the browser window itself, including its dimensions and location.
The document.images.length
property returns the quantity of image elements within the DOM, while document.images[0].src
is strictly used to set the image URL.
The document.images.length
property returns the quantity of image elements within the DOM, while document.images[0].src
is strictly used to set the image URL.
The modern methods of accessing elements using document.getElementById()
are more prone to errors and harder to maintain compared to accessing elements via index in component arrays.
The modern methods of accessing elements using document.getElementById()
are more prone to errors and harder to maintain compared to accessing elements via index in component arrays.
The innerText
property is capable of interpreting and rendering HTML tags, making it suitable for inserting formatted text with elements like bold or italics.
The innerText
property is capable of interpreting and rendering HTML tags, making it suitable for inserting formatted text with elements like bold or italics.
Using document.write()
to modify the document after the initial page load is a modern and recommended practice for creating dynamic web content.
Using document.write()
to modify the document after the initial page load is a modern and recommended practice for creating dynamic web content.
When handling events in JavaScript, the 'load' event fires exclusively after all content, including images and external resources, has completely finished loading.
When handling events in JavaScript, the 'load' event fires exclusively after all content, including images and external resources, has completely finished loading.
Flashcards
What is JavaScript?
What is JavaScript?
A scripting language embedded in web browsers to enhance web page interactivity and responsiveness.
Language Basics
Language Basics
Syntax, keywords, operators and built-in objects in JS.
Where to include Javascript?
Where to include Javascript?
The best practice is to place scripts at the end of the
section.
Keywords
Keywords
Signup and view all the flashcards
Operators
Operators
Signup and view all the flashcards
Whitespace relevance
Whitespace relevance
Signup and view all the flashcards
Variable declaration
Variable declaration
Signup and view all the flashcards
Data types in JavaScript
Data types in JavaScript
Signup and view all the flashcards
What is a function?
What is a function?
Signup and view all the flashcards
What is Scope?
What is Scope?
Signup and view all the flashcards
Client-side scripting
Client-side scripting
Signup and view all the flashcards
ECMAScript
ECMAScript
Signup and view all the flashcards
Writing to HTML
Writing to HTML
Signup and view all the flashcards
JavaScript Statements
JavaScript Statements
Signup and view all the flashcards
Statement Components
Statement Components
Signup and view all the flashcards
String Literals
String Literals
Signup and view all the flashcards
Variable Values
Variable Values
Signup and view all the flashcards
Expressions
Expressions
Signup and view all the flashcards
Comments in Javascript
Comments in Javascript
Signup and view all the flashcards
Loosely Typed Variable
Loosely Typed Variable
Signup and view all the flashcards
What is an Object?
What is an Object?
Signup and view all the flashcards
Object Attributes (Properties)
Object Attributes (Properties)
Signup and view all the flashcards
Object Behaviors (Methods)
Object Behaviors (Methods)
Signup and view all the flashcards
Accessing Object Properties
Accessing Object Properties
Signup and view all the flashcards
Calling Object Methods
Calling Object Methods
Signup and view all the flashcards
Extending Objects
Extending Objects
Signup and view all the flashcards
Constructor Method
Constructor Method
Signup and view all the flashcards
Built-in Objects
Built-in Objects
Signup and view all the flashcards
What is an Array?
What is an Array?
Signup and view all the flashcards
Date Object
Date Object
Signup and view all the flashcards
Objects in JavaScript
Objects in JavaScript
Signup and view all the flashcards
How to create a Date Object
How to create a Date Object
Signup and view all the flashcards
getFullYear() Method
getFullYear() Method
Signup and view all the flashcards
getMonth() Method
getMonth() Method
Signup and view all the flashcards
getDate()
getDate()
Signup and view all the flashcards
getDay() Method
getDay() Method
Signup and view all the flashcards
getHours()
getHours()
Signup and view all the flashcards
Array Indexes
Array Indexes
Signup and view all the flashcards
Control Structures
Control Structures
Signup and view all the flashcards
if
Statement
if
Statement
Signup and view all the flashcards
if-else
Statement
if-else
Statement
Signup and view all the flashcards
switch
Statement
switch
Statement
Signup and view all the flashcards
Loop
Loop
Signup and view all the flashcards
while
loop
while
loop
Signup and view all the flashcards
do-while
loop
do-while
loop
Signup and view all the flashcards
break
Keyword
break
Keyword
Signup and view all the flashcards
continue
Keyword
continue
Keyword
Signup and view all the flashcards
if
Statement Definition
if
Statement Definition
Signup and view all the flashcards
if...else
Extension
if...else
Extension
Signup and view all the flashcards
switch
Statement Function
switch
Statement Function
Signup and view all the flashcards
Loop Definition
Loop Definition
Signup and view all the flashcards
break
Statement Use
break
Statement Use
Signup and view all the flashcards
continue
Statement Action
continue
Statement Action
Signup and view all the flashcards
while
Loop Definition
while
Loop Definition
Signup and view all the flashcards
do...while
Loop
do...while
Loop
Signup and view all the flashcards
Document Object Model (DOM)
Document Object Model (DOM)
Signup and view all the flashcards
window.screen.width
window.screen.width
Signup and view all the flashcards
window.screen.height
window.screen.height
Signup and view all the flashcards
window.screen.colorDepth
window.screen.colorDepth
Signup and view all the flashcards
window.alert(message)
window.alert(message)
Signup and view all the flashcards
window.confirm(message)
window.confirm(message)
Signup and view all the flashcards
window.prompt(message, defaultValue)
window.prompt(message, defaultValue)
Signup and view all the flashcards
document.title
document.title
Signup and view all the flashcards
getElementsByClassName()
getElementsByClassName()
Signup and view all the flashcards
appendChild()
appendChild()
Signup and view all the flashcards
DOM Importance
DOM Importance
Signup and view all the flashcards
Screen Child Object
Screen Child Object
Signup and view all the flashcards
document.domain
document.domain
Signup and view all the flashcards
document.forms.length
document.forms.length
Signup and view all the flashcards
getElementById()
getElementById()
Signup and view all the flashcards
innerText
innerText
Signup and view all the flashcards
createElement()
createElement()
Signup and view all the flashcards
setAttribute()
setAttribute()
Signup and view all the flashcards
Event Handlers
Event Handlers
Signup and view all the flashcards
Multiple property
Multiple property
Signup and view all the flashcards
Study Notes
### Meet DOM
- A browser organizes web page components into a hierarchical tree.
- The tree is called the Document Object Model (DOM).
- Each component is nested under the top-level window object.
- JavaScript can access and change HTML document elements using the HTML DOM.
- The DOM enables dynamic webpage changes without reloading.
- It aids JavaScript interaction with HTML and CSS.
- Its essential for creating interactive web applications.
- JavaScript can change HTML elements/attributes/CSS styles in the page.
- Enables JavaScript to remove/add HTML elements/attributes.
- JavaScript can react to all existing HTML events in the page.
- Also lets JavaScript create new HTML events in the page
### Inspect Properties
- The top-level DOM window object has a screen child object.
- The screen child object provides properties that describe user's monitor resolution in pixels.
### Screen Dimensions and Usable Space
- The overall screen size can be accessed through Window.screen.width (total width in pixels) and Window.screen.height (total height in pixels).
- Usable screen space, excluding system UI elements, is found using Window.screen.availWidth (available width) and Window.screen.availHeight (available height).
- These properties help optimize layout design to ensure content fits in the user's available screen area.
### Color Depth Information
- Window.screen.colorDepth represents the bit value for color range.
- Common values include 8-bit (256 colors), 16-bit (65,536 colors), 24-bit (millions of colors), and 32-bit (billions of colors).
- 8-bit = low color
- 16-bit = high color
- 24-bit = true color
- 32-bit = deep color
### Show Dialogs
- The top-level DOM window object provides three methods for JavaScript to display dialog messages.
- A simple warning message can be displayed via window.alert(message).
- A confirmation box with "OK" and "Cancel" buttons is displayed via window.confirm(message), returning true for "OK" and false for "Cancel".
- The function window.prompt(message, defaultValue) displays a prompt box with a text input field, an "OK" button (returns the input value), and a "Cancel" button (returns null).
### Extract Info
- The document object is a child of the window object.
- Provides access to and allows manipulation of the HTML document.
- Dynamically updating web pages is useful.
- Commonly utilized in JavaScript for dynamic page interactions.
- The document object provides access to the page title (document.title), current URL (document.URL), domain name (document.domain), last modified date (document.lastModified), and referrer URL (document.referrer).
- document.title retrieves or sets the page title.
- document.URL provides the full URL of the current document.
- document.domain retrieves the domain name of the document, similar to location.host.
### Address Arrays
- The document object has child objects like forms, images, links, styleSheets, and scripts.
- Each object is represented as an array corresponding to elements in the HTML document.
- The number of each object in the document can be returned via:
- document.forms.length (number of forms)
- document.images.length (number of images)
- document.links.length (number of links)
- document.scripts.length (number of scripts)
### Address Array
- To access the first image (HTML tag) in the document body: document.images[0].
- The URL can be referenced using document.images[0].src.
- Dynamically replacing the old URL with a new URL in script is possible.
### Address Elements
- Error-prone: Accessing elements via index in component arrays can lead to errors because index positions change when modifying HTML.
- Solution is tedious and hard to maintain due to complexity in dynamic pages.
- Modern methods:
- Accessing element by ID.
- Accessing element by tag name.
- Accessing element by class name.
### Accessing element by ID
- Retrieves a single element by its id.
- Best for unique elements like headers, buttons, or sections.
### Accessing element by tag name
- Retrieves all elements with the given tag name.
- Returns an HTMLCollection (array-like structure).
### Accessing element by class name
- Retrieves all elements with a specific class.
- Returns an HTMLCollection.
- Applies styles or modifications to multiple elements at once.
### Write Content
- Methods to Write Content in the DOM:
- Insert or Change HTML Content (innerHTML).
- Insert or Change Text (No HTML).
- Writing Content Directly to the Page (document.write).
### Insert or Change HTML Content
- Allows setting or retrieving an element's HTML content.
- Supports text and HTML elements.
### Insert or Change Text
- Returns only the content between an elements tags.
- Used to sets or retrieves only text content.
- Ignores HTML tags, treating them as plain text
### Writing Content Directly to the Page
- Writes directly into the document body.
- Replaces all existing HTML content if executed after page load.
- Not recommended for modern web development.
- Used mainly for testing or when loading a new page dynamically.
- Creates an HTML element dynamically by using createElement(tagName)
- Takes the tag name as an argument
- Content can be added using .innerHTML or .innerText
- Appends the new element inside an existing parent.
- Places the new element at the end of the parent's content
- Adds attributes new attributes dynamically to DOM elements
- Takes two arguments" Attribute and Value
### Events
- The DOM allows JavaScript to react to "events" that occur on a web page. Functions are executed when a particular event happens.
- These functions are known as "event-handlers", and can react to events such as:
- load: Page loaded.
- click: User clicks a mouse button.
- keydown: User presses a keyboard key.
- change: User modifies an input field.
- submit: User submits an HTML form.
- 2 Main components to consider:
- The Event Itself
- The Reaction
### The Event Itself
- What happens that you want to respond to like:
- load
- click
- mousemove
- keydown, keyup
- submit
### The Reaction
- This is the function you define to react to the event
- For example, you might:
- Change the style (e.g., make a button red)
- Add or remove text/content from the page
- Send data to the server
- Show/hide an element
- two main ways to use or handle events:
- HTML Event Attributes (Inline Method)
- JavaScript (addEventListener Method)
### HTML Event Attributes (Inline Method)
- You directly add the event in the HTML element using an attribute like onclick, onmouseover, etc.
### JavaScript (addEventListener Method)
- It allows you to register a function to be executed when a specific event occurs on a specific element.
- This is the recommended way, especially in modern development.
### Load Events
- Fired when the entire page has completely loaded.
- The browser waits for loading everything before triggering the event.
- When responding to the load event in JavaScript, there are two main ways to define the event handler:
- Using the window.onload Property.
- Using add an event listener.
### Mouse Events
- Triggered when the user interacts with the mouse (or similar input device) over an element on the page
- click: Fired when the user clicks on an element (press + release)
- dblclick: Fired when the user double-clicks.
- mouseover: When the pointer enters an element.
- mouseout: When the pointer leaves an element.
### Events Values
- An event object contains information about the event that occurred, such as:
- What type of event it was (e.g. click, keydown)
- Where it happened (mouse coordinates)
- Which key was pressed
- Which element triggered the event
### Check Boxes
- Checkboxes and Radio Buttons
### Checkboxes
- Allow users to select multiple options.
- Each checkbox is independent.
### Radio Buttons
- Allow users to select only one option from a group.
- All buttons in a group share the same name attribute.
- Grouping Behavior and JavaScript Access
- Grouping by name
- Checkboxes and radio buttons are grouped by the same name.
- Javascript access
- You can access the group using JavaScript
- The result is an array-like object, and you can:
- Loop through it.
- Access each element using an index.
- Read or assign the .checked property.
- The .checked Property is a Boolean property on both checkboxes and radio buttons.
- True = selected, false= not selected
### Select options
- The <select> element is used in HTML to create a drop-down list of options that a user can choose from.
- You can interact with a <select> element in JavaScript using several useful properties:
- selectedIndex = Returns or sets the index of the selected option (starts at 0).
- value = Returns or sets the value of the selected option.
- options = Returns a collection (array-like) of all <option> elements inside <select>.
- length = Returns the number of options in the drop-down.
- multiple = A Boolean that indicates whether multiple options can be selected.
### Summary of Script Flow Management
- The `if` keyword performs the basic conditional test to test a condition for a boolean true or false value.
- An alternative branch to the basic conditional test uses the `else` keyword to extend an `if` statement.
- `switch` statement tests a large number of conditions for a boolean true or false value.
- In a `switch` block, each `case` statement must end with the `break` keyword to exit upon finding a match.
- A `switch` block may contain a final `default` statement to execute when no match has been found.
- A `for` loop specifies an initializer, a condition to be tested for a boolean true or false value, and a modifier, and the modifier must enable the tested condition to become false at some point to exit the loop.
- The `while` and `for` loops evaluate a test condition before any statements are executed.
- The `do while` loop evaluates a test condition after its statements have been executed.
- The `break` keyword exits from a loop when a specified condition is encountered.
- The `continue` keyword skips a single iteration of a loop when a specified condition is encountered.
- The document object has title, URL, domain, lastModified, and referrer properties that describe that document.
- The document object has forms, images, links, styleSheets, and scripts child objects that are arrays of document components.
- The document object has getElementById( ), getElementsByTagName(), and getElementsByClassName( ) methods that can be used to reference HTML elements.
- The innerHTML and innerText properties of the document object can be used to write content into existing elements.
- The document object has createElement( ), appendChild( ), and setAttribute( ) methods that can add content to a document.
- The DOM allows JavaScript to react to events such as load, click, keydown, change, and submit in response to user actions.
- Event-handler functions can be assigned to an object property or specified by the addEventListener( ) method.
- An event object can be passed to an event-handler function and the event can be identified by its event.type property.
- Radio and checkbox button objects have a checked boolean property, which is only true when the button is selected.
- A selection list object has a selectedIndex property, which contains the index number of the currently selected options[ ] array element.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.