parm530
6/25/2018 - 1:20 PM

JavaScript Basics 2

More in depth overview of JS

String Functions


//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']

Functions Review

  • BASIC SYNTAX: (function declaration)
function nameOfFunction() {
  //code
  return something;
}
  • lowerCamelCase is used for the naming convention for JS functions (no snake_cased!!)
  • A function in JS will always return undefined, unless a return value is given.

Arrow functions

[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
})
  • To call a function and execute it: functionName();
    • Needs the ();
    • calling the function without the ();, will just return the entire function declaration
  • Functions can be written as above or as a fucntion expression:
var functionName = function() {
  return something;
}
  • ** Function declarations are hoisted to the top of the scope**, function expressions are not!
    • This means you can call a function before declaring it, if it was written as a function declaration, if written as a function expression, the call to the function will error out!
  • You can give a function default params: function parm(name="Parm") {}

Control Fow

  • if/else statement
if (condition) {
  
} else if (condition2) {
  
} else {
  
}
  • switch statements
switch (expression) {
  case n:
    //code
    break;
  case m:
    //code
    break;
  default: 
    //code
}

Arrays

  • use .push(element) to add an element to an array
  • Removing an element in a JS array
var 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
  • Iteration using 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
})

Looping

  • loops include 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])

Hashes

  • Initialize the object:
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
}