Javascript Flashcards
undefined (JS)
var declared without an operator
document.write();
output to browser
turns special characters into strings
'Hello \'you\' there'
backslash (\) escape
Boolean
"real" values
true
variable = (condition) ? value1: value2
assigns a value to a variable, based on some condition
Ternary Operator
if (condition) { statements }
The if Statement
if (expression) { // executed if condition is true } else { // executed if condition is false }
The else Statement
specify a new condition if the first condition is false
else if statement
switch (expression) { case n1: statements break; case n2: statements break; default: statements }
switch statement
breaks out of the switch block
The break Keyword
specifies the code to run if there is no case match
The default Keyword
while (condition) { code block }
var i=0; while (i<=10) { document.write(i + "
"); i++; }
"); i++; }
The While Loop
var i=20; do { document.write(i + "
"); i++; } while (i<=25);
"); i++; } while (i<=25);
The Do...While Loop
breaks only one iteration in the loop, and continues with the next iteration
The continue statement
function myFunction() { alert("Calling a Function!"); } myFunction(); //Alerts "Calling a Function!"
Calling a Function
functionName(param1, param2, param3) { // some code }
Function Parameters
The prompt() method takes two parameters.
var user = prompt("Please enter your name"); alert(user);
Prompt Box
function person(name, age, color) { this.name = name; this.age = age; this.favColor = color; }
var p1 = new person("John", 42, "green");
object constructor function
refers to the current object
"this" keyword
calls a function or evaluates an expression at specified intervals (in milliseconds)
setInterval(calc, 2000);
setInterval() method
the owner (or root) of all objects in your webpage.
document.body.innerHTML = "Some text";
The document Object
document.getElementById(id)
find element by id
document.getElementsByClassName(name)
returns as array
find elements by class name
document.getElementsByTagName(name)
returns as array
find elements by tag name
returns an array of an element's child nodes
element.childNodes
returns the first child node of an element
element.firstChild
returns the last child node of an element
element.lastChild
returns true if an element has any child nodes, otherwise false
element.hasChildNodes
returns the next node at the same tree level
element.nextSibling
returns the previous node at the same tree level
element.previousSibling
returns the parent node of an element
element.parentNode
clones an element and returns the resulting node
element.cloneNode()
creates a new element node
document.createElement(element)
creates a new text node
document.createTextNode(text)
adds a new child node to an element as the last child node
element.appendChild(newNode)
inserts node1 as a child before node2
element.insertBefore(node1, node2)
element.replaceChild(newNode, oldNode)
Replacing Elements
allows you to create an animation relative to a container
position: relative #CSS
call the function in window.onload to make sure the HTML is loaded or JS may not execute
window.onload
used to stop a setInterval timer
clearInterval
attaches an event handler to an element without overwriting existing event handlers
addEventListener() method