

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
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.