JavaScript Cheat Sheet: Essential Syntax, DOM Methods, and Programming Concepts, Exams of Javascript programming

CONDITIONAL STATEMENTS. Relational Operators. == Equal to != Not equal to. > Greater than. < Less than. >= Greater than or equal to.

Typology: Exams

2021/2022

Uploaded on 07/05/2022

tanya_go
tanya_go 🇦🇺

4.7

(73)

1K documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
JavaScript – Cheat Sheet
Page 1 of 2
JavaScript
Cheat Sheet
OUTPUT
# This is a comment
/* 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
DOM METHODS
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
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
Variables must be declared before they are used.
var number = 1;
var workshop = “Advanced Web Dev”;
var even_numbers = [2, 4, 6, 8];
ARITHMETIC OPERATORS
+ Add - Subtract * Multiply / Divide
++ Increase by one -- Decrease by one
STRING FUNCTIONS
var string = “javascript”;
string.length
Returns 10the length of the string
pf2

Partial preview of the text

Download JavaScript Cheat Sheet: Essential Syntax, DOM Methods, and Programming Concepts and more Exams Javascript programming in PDF only on Docsity!

JavaScript – Cheat Sheet Page 1 of 2

JavaScript

Cheat Sheet

OUTPUT

This is a comment

/* 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

DOM METHODS

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

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

Variables must be declared before they are used. var number = 1; var workshop = “Advanced Web Dev”; var even_numbers = [2, 4, 6, 8];

ARITHMETIC OPERATORS

  • Add - Subtract * Multiply / Divide ++ Increase by one -- Decrease by one

STRING FUNCTIONS

var string = “javascript”; string.length Returns 10 – the length of the string

JavaScript – Cheat Sheet Page 2 of 2

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.