More in depth overview of JS
//Casing
"shout".toUpperCase(); //=> "SHOUT"
"SHOUT".toLowerCase(); //=> "shout"
//Replace
"i love to code!".replace("code", "eat") //=> "i love to eat!"
//Will only replace the first occurence of the word or phrase to
//subsitute!
//Convert string to number
Number('78') //=> 78
parseInt('78') //=> 78
//Slice
//returns a new string that is cut from a starting point argument to the
//ending point argument
"hello, my name is parm".slice(18, 22) //=> "parm"
//you can also use negative indices to start at the END of a string
//Split
//converts an string into an array by the argument provided
"hello, apple, banana".split(',') //=> ['hello', 'apple', 'banana']
function nameOfFunction() {
//code
return something;
}
undefined
, unless a return value is given.[1,2,3,4].filter( i => i % 2 === 0) // [2,4], no need for explicit return statement
[1,2,3,4].filter( i => {return i % 2 === 0} ) // [2,4], if need to specify a return value
//can be written as:
[1,2,3,4].filter( i => {
return i % 2 === 0
})
functionName();
();
();
, will just return the entire function declarationvar functionName = function() {
return something;
}
function parm(name="Parm") {}
if (condition) {
} else if (condition2) {
} else {
}
switch (expression) {
case n:
//code
break;
case m:
//code
break;
default:
//code
}
.push(element)
to add an element to an arrayvar fishArray = ['angel', 'clown', 'drum', 'surgeon'];
fishArray.splice(indexToStart, numberOfElementsToRemove, elementToAdd (optional));
fishArray.splice(1, 2, 'mandarin') //removes 'clown' and 'drum' and adds 'mandarin' to the end
forEach
var array = [ ... ];
array.forEach(function(element, index) {
//forEach will yield each element in the array to your function
//a second argument to this function is the index of the element
})
for
, while
, do-while
//For loop
for([initialization]; [condition]; [iteration]) {
//loop body
}
for(var i = 1; i < 100; i++) {
//stuffs
}
//While loop: continue loop execution until condition is false
while([condition]) {
//stuffs
}
//Do-while ensures that a loop is executed atleast once
do {
} while ([condition])
var obj = {};
//or
var obj = new Object();
obj["name"] = "Parm";
obj["age"] = 25;
obj["occupation"] = "Developer";
// To access a value, use the key:
obj["name"] //=> "Parm"
//Deleting a value
delete obj["age"]
//You can the value by just over-writing the value stored in the key
obj["age"] = 100;
//To check it this object is empty, there is no .empty? method, just check the keys:
Object.keys(obj) //returns an array for all the keys in the object
//Iterating over an object
for (variableName in objectName) {
//code
}