

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
CONDITIONAL STATEMENTS. Relational Operators. == Equal to != Not equal to. > Greater than. < Less than. >= Greater than or equal to.
Typology: Exams
1 / 2
This page cannot be seen from the preview
Don't miss anything!


/* This is a multi-line comment */ window.alert(“Hello World”) Displays “Hello World” in an alert box console.log(“Hello World”) Writes “Hello World” into the browser console
These methods affect the overall HTML page. document.getElementById(“id”) Returns the element where id = “id” document.getElementsByClassName(“class ”) Returns a list of elements where class=”class” document.createElement(“button”) Creates a button element element.childNodes Returns all children elements within ‘element’ element.parentNode Returns the predecessor of ‘element’ element.appendChild Adds a DOM element to ‘element’ element.removeChild(child_element) Removes ‘child_element’ from ‘element’ element.id Returns or assigns an id to an element element.class Returns or assigns a class to an element elementName.innerText = “Hello World” Assigns text to an HTML element
Events occur only in certain circumstances; they’re generally associated with functions. document.onload Occurs when the web page is initially loaded element.onclick Occurs when the element is clicked element.onkeyup Occurs when any key is pressed in the element
Variables must be declared before they are used. var number = 1; var workshop = “Advanced Web Dev”; var even_numbers = [2, 4, 6, 8];
var string = “javascript”; string.length Returns 10 – the length of the string
string.slice(0, 4) Slices string from index 0 to index 4. Returns “java” string.replace(‘a’, ‘A’) Replaces first instance of ‘a’. Returns “jAvascript” CONDITIONAL STATEMENTS Relational Operators == Equal to != Not equal to > Greater than < Less than >= Greater than or equal to <= Less than or equal to Boolean Operators - evaluate to True or False && and (1 > 0) && (4 > 0) Evaluates to: True || or (1 > 3) || (4 > 3) Evaluates to: True ! not !(1 == 1) Evaluates to: False One Way Selection if (name == “Sudo”) { alert(“Hello Sudo”); } Two Way Selection if (mark >= 50) { alert(“Pass”); } else { alert(“Fail”); } Multiple Selection if (number > 0) { alert(“Positive”); } else if (number < 0) { alert(“Negative”); } else { alert(“Zero”); } LOOPS Counted Loops for (i = 0; i < 5; i++) { alert(i); } This outputs the values 1- 9. // Loop through an array var even_numbers = [2, 4, 6, 8]; for (i = 0; i < even_numbers.length; i++) { alert(even_numbers[i]); } This all values stored in the array. Conditional Loops var i = 1; while (i < 10) { alert(i); } This outputs the values 1- 9 FUNCTIONS // Function that adds two numbers function add(number1, number2) { return number1 + number2; } alert( add( 3, 4 ) ) Displays 7 // Anonymous Functions Example window.onload = function() { alert(“Hello”); } This outputs “Hello” once the window is loaded, and cannot be used any other time.