Cheat Sheet: Javascript
Cheat Sheet: Javascript
id
Cheat Sheet Returns or assigns an id to an element
element.class
OUTPUT Returns or assigns a class to an element
# This is a comment
/* This is a multi-line comment */ elementName.innerText = “Hello World”
Assigns text to an HTML element
window.alert(“Hello World”)
Displays “Hello World” in an alert box
EVENTS
Events occur only in certain circumstances; they’re
console.log(“Hello World”) generally associated with functions.
document.getElementsByClassName(“class element.onkeyup
”) Occurs when any key is pressed in the element
Returns a list of elements where class=”class”
VARIABLES
document.createElement(“button”) Variables must be declared before they are used.
Creates a button element var number = 1;
var workshop = “Advanced Web Dev”;
element.childNodes var even_numbers = [2, 4, 6, 8];
STRING FUNCTIONS
element.appendChild
Adds a DOM element to ‘element’ var string = “javascript”;
string.length
CONDITIONAL STATEMENTS
LOOPS
Relational Operators
== Equal to != Not equal to Counted Loops
> Greater than < Less than for (i = 0; i < 5; i++) {
>= Greater than or equal to alert(i);
<= Less than or equal to }
This outputs the values 1-9.
Boolean Operators - evaluate to True or False
// Loop through an array
&& and
var even_numbers = [2, 4, 6, 8];
(1 > 0) && (4 > 0) for (i = 0; i < even_numbers.length; i++)
{
Evaluates to: True
alert(even_numbers[i]);
|| or }