JavaScript Flashcards
1
Q
Benefits of JavaScript
A
- user input (mouse, keyboard, etc.)
- modify HTML elements and CSS dynamically (change styles, etc.)
- analyze data like user input (validating form input, etc.)
- change content based on specific conditions (diff message for student vs teachers, etc.)
2
Q
JavaScript
A
- use variable names
- several types of variables: string, integer, float/double, array
3
Q
Arrays
A
- indexes such that each item has a position, starting at 0
- access individual elements using square brackets and an index
- ie Boolean and arrays, if colors = [“red”,”blue”]
4
Q
JS and CSS
A
- directly change element styles
- change classes or IDs
- advanced animations/transitions
- content can also be changed
5
Q
document.getElementBy__()
A
- ById, TagName, or ClassName
- ID element getter is the best one to use since it returns one specific object
- getting elements by tag or class may be helpful in specific cases in which an array of elements need to be accessed at once
6
Q
dot notation
A
- accessing levels of properties, so styles can be modified
- style is a property in HTML elements
- CSS styles are properties within style
- element.style.styleproperty
- eg. md.style.width
- after specifying a style, use ‘=’ and set the new value in quotation marks
7
Q
camel case
A
- equivalent to style properties that contain a hyphen in CSS
- backgroundColor = background-color
8
Q
HTML to JSS class change options
HTML: div id=”tb” class=”box”
JS: var tb = document.getElementbyId(“tb”);
tb.className = “newbox”;
tb.className = “box newbox”;
A
- single class is usually a replacement
- multiple classes are usually additions
9
Q
Event handling - event listeners
A
- style/content changes will typically be done as a result of an event
- user input events: mouse-based, keyboard-based
- load event
- timer events
10
Q
2 main ways to add event listeners
A
- inline: attach event listener as an attribute in the HTML element tag
- dot notation: use the addEventListener() function as a property on the element using dot notation
11
Q
Inline event listener
A
- starts with “on” followed by an event name
- eg. onclick, onmouseover, onmouseout, onfocus (focus on form field), onblur (leave a form field)
12
Q
2 types of timer events
A
- timeout: trigger a function once after a specified time has elapsed
- interval: trigger a function repeatedly in intervals of the specified time
- setTimeout(function, ms) or setInterval(function, ms)
13
Q
Conditionals
A
- portions of code will only execute if specific conditions are met
- if-else statements are used
- combined them with && (and) operator or use ||(or) operator
14
Q
Functions
A
- process that can be executed at any time, and any number of times
- call function –> myFunction();
- have input parameters which are placed within parentheses
- parameters make the function reusable and flexible to work in different scenarios
15
Q
what are the parameters to function average (x,y) {
var z = (x+y)/2;
document.write(“result: “ + z);
}
A
x and y