Just migrated it from Codepen.io to markdown. Credit goes to David Conner.
Working with DOM | Working with JS | Working With Functions |
---|---|---|
Accessing Dom Elements | Add/Remove Array Item | Add Default Arguments to Function |
Grab Children/Parent Node(s) | Add/Remove Object Properties | Throttle/Debounce Functions |
Create DOM Elements | Conditionals | |
Add Elements to the DOM | Loops | |
Add/Remove/Toggle/Check Classes | Events | |
Timers | ||
Type Checking |
// Returns a reference to the element by its ID.
document.getElementById('someid');
// Returns an array-like object of all child elements which have all of the given class names.
document.getElementsByClassName('someclass');
// create an empty array
var myArray = [];
// create array with items. Can store any type
var myOtherArray = [myArray, true, 'a random string'];
// call specific value in an array
myOtherArray[0];
// will return myArray
// change value for this item
myOtherArray[0] = false;
// will now return false
// add to end of array
myOtherArray[myOtherArray.length] = 'new stuff';
// will return the new item 'new stuff'
// or you can use push()
myOtherArray.push('new stuff');
// will return new length of array
// you can remove this last item by using pop()
myOtherArray.pop();
// will return the last item of the array and will have removed it from myOtherArray
// shift and unshift will do the same for the begging of the Array
// myOtherArray.shift();
// will remove and return first item of array
// myOtherArray.unshift(1,2);
// this will add 1 and 2 to beginning of array and return new length
// you can use delete keyword but turn value to undefine and not shorten length. so we use splice()
myOtherArray.splice(2, 1);
// this will remove and return the third item only.
// first arg is where to start and second is how many things to splice. this example is 1.
// create an object
var newObject = {};
// add a property to object
newObject.newPropName = 'super slick';
// or other syntax
newObject['other new prop name'] = 'mildly slick';
// Now newObject.newPropName will return 'super slick'
newObject.newPropName;
// now to delete
delete newObject.newPropName;
// If Else statements
var a = 1;
var b = 2;
if (a < b) {
console.log('the if is true!');
} else {
console.log('the if is false!');
}
// Multi If Else statements
var a = 1;
var b = 2;
var c = 3;
if (a > b) {
console.log('a is bigger than b');
} else if (a > c) {
console.log('but a is bigger than c');
} else {
console.log('a is the smallest');
}
// Ternary operators. same as if else
var a = 1;
var b = 2;
a === b ? console.log('The statement is true') : console.log('The statement is false');
// switch statements
var a = 4;
switch (a) {
case 'Oranges':
console.log('Orange? really?');
break;
case 1:
console.log('a is equal to 1.');
break;
case 2:
console.log('a is equal to 2.');
break;
case 3:
console.log('a is equal to 3.');
break;
case 4:
console.log('a is equal to 4.');
break;
default:
console.log('I run if no one else is true.');
}
// while loop
var i = 0;
while (i < 10) {
console.log(i);
i += 1
}
// for loop
for (var i = 0; i < 10; i++) {
console.log(i);
}
// for in statments
var obj = {a:1, b:2, c:3};
for (var prop in obj) {
// check if property is inherited or not
if (obj.hasOwnProperty(prop)) {
console.log('obj.' + prop + ' = ' + obj[prop]);
}
}
Events (MDN Event reference)
var newElement = document.getElementsByTagName('h1');
newElement.onclick = function() {
console.log('clicked');
};
var logEventType = function(e) {
console.log('event type:', e.type);
};
newElement.addEventListener('focus', logEventType, false);
newElement.removeEventListener('focus', logEventType, false);
window.onload = function() {
console.log('Im loaded');
};
// set time out
window.setTimeout(simpleMessage, 5000);
// if you wanted to clear the timer.
var timer = window.setTimeout(simpleMessage, 5000);
window.clearTimeout(timer);
// set interval. will repeat every 5000ms
window.setInterval(simpleMessage, 5000);
// if you wanted to clear the intervals.
var intervalHandler = window.setInterval(simpleMessage, 5000);
window.clearInterval(intervalHandle);
var myNumber = 1;
var myString = 'some Text';
var bools = true;
var myArray = [];
var myObj = {};
var notNumber = NaN;
var nullified = null;
var undef;
typeof myNumber;
// returns 'number'
typeof myString;
// returns 'string'
typeof bools;
// returns 'boolean'
typeof myArray;
// returns 'object'.
// Not super helpful so must check if it has length property to see if it is an array.
typeof myArray === 'object' && myArray.hasOwnProperty('length');
// returns true
typeof myObj;
// returns 'object'. Must do the same test as above but expect false back from check.
typeof notNumber;
// returns 'number'. this is confusing but returns this as NaN is part of the global Number object.
// must check if isNaN()
typeof notNumber === 'number' && isNaN(notNumber);
// returns true if type of is 'number' and is still NaN
typeof undef;
// returns 'undefined'
undef === undefined && typeof undef === 'undefined';
// returns 'true'
notDeclared === undefined;
// -> Uncaught ReferenceError: notDeclared is not defined