Javascript Basic Reference
February 07, 2021
Declaring Variables
const myVar = 1
can’t reassign, block scoped
let myVar = 2
can reassign, block scoped
var myVar = 3
can reassign, global unless in function
Math Operators
+, -
Add and Substract*, /
multiply and divide**
exponent++
increment (add one)--
decrement (subtract one)+=
add and reassign-=
subtract and reassign*=
multiply and reassign/=
divide and reassign%
modulo/modulusMath.random()
generate random number (0-1)Math.ceil()
round a number upMath.floor()
round a number downMath.round()
round a numberMath.sqrt()
square root of numberMath.PI
The numerical value of PI
Boolean Operators
< >
greater than, less than<= >=
less equal, greater equal== ===
equality, strict equality!= !==
inequality, strict inequality&&
and||
or
Conditionals
- IF statement
if (expression){
//code if true
} else if (expression){
//code if second expression is true
} else {
//code if all expressions are false
}
- Ternary Operator
expression ? resultIfTrue : resultIfFalse
- Switch Statements
switch(value){
case possibleMatch:
//code if match
break
default:
//code if no case matches
break
}
Loops
- while loops
while(expression){
//code to repeat as long as expression is true
}
- for loops
for(initiateCounter; expression; adjustCounter){
//code will that will as long as expression is true
}
- for of loops
for (item of iterable){
//code that will run for each item in iterable (array, set, map) and on each loop the current item will be stored in "item"
}
- for in loops (for objects)
for(key in object){
//loops over object keys, access value with object[key]
}
- forEach Array Method
Array.forEach((item) => {
// code to run for each item in the array, each item will take turns being stored in "item"
})
Functions
function myFunc(){
//code to run when function invoked
}
hoisted, can use this keyword
const myFunc = function(){
//code to run when function is invoked
}
not hoisted, can use this keyword
const myFunc = () => {
//code to run when the function is invoked
}
not hoisted, can’t use this keyword
Arrays
const arr = [1,2,3]
declare an arrayarr[0]
access an array valueconst [one, two, three] = arr
array destructuringconst arrCopy = [...originalArr]
copy arrayconst dupeFree = [...new Set(arr)]
remove duplicates
Objects
const obj = {one: 1, two: 2}
declare an objectobj.one
accessing property with dot notationobj["two"]
accessing property with square bracketsconst {one, two} = obj
object destructuringconst objCopy = {...originalObj}
copy an object
DOM Manipulation
document.querySelector('h1')
return the first matching nodedocument.querySelectorAll('h1')
return array of all matching nodesnode.innerHTML
property containing string to parsed as html in the nodenode.innerText
property containing string to be parsed as text in the nodenode.style
object containing all CSS properties of ndoeparentNode.appendChild(childNode)
append a node as a child of a nodenode.addEventListener("click", someFunction)
add event listener, function will run whenever specified event occursdocument.createElement("h1")
create a node