constabc="abcdefghijklmnopqrstuvwxyz";abc.indexOf("l");// find substring, -1 if doesn't contain abc.lastIndexOf("l");// last occuranceabc.replace("abc","123");// find and replace, takes regular expressionsabc.toUpperCase();// convert to upper caseabc.toLowerCase();// convert to lower caseabc.concat(" ",str2);// abc + " " + str2abc.charAt(2);// character at index: "c"abc[2];// unsafe, abc[2] = "C" doesn't workabc.charCodeAt(2);// character code at index: "c" -> 99abc.split(",");// splitting a string on commas gives an arrayabc.split("");// splitting on characters128.toString(16);// number to hex(16), octal (8) or binary (2)
Array
constarr=[1];arr.concat([4,5,6]);// Merge 2 arrays: [1,4,5,6]arr.push(7);// Add element to LAST: [1,4,5,6,7]arr.unshift(0);// Add element to FIRST: [0,1,4,5,6,7]arr.splice(2,0,2);// Add element BEFORE index 2: [0,1,2,4,5,6,7]arr.splice(2+1,0,3);// Add element AFTER index 2: [0,1,2,3,4,5,6,7]arr.shift();// Remove FIRST Element [1,2,3,4,5,6,7]arr.pop();// Remove LAST Element [1,2,3,4,5,6]arr.reverse();// Reverse [6,5,4,3,2,1]arr.sort();// Sort [1,2,3,4,5,6] arr.splice(arr.indexOf(REF),0,NEW)// Add element BEFORE a specific index [_,_,REF,NEW,_,_]arr.splice(arr.indexOf(REF)+1,0,NEW)// Add element AFTER a specific index [_,_,REF,NEW,_,_]arr.splice(2,1,'_');// Replace element at index 2: [1,_,3,4,5,6]// get the Sub Setconstarr=[0,1,2,3,4,5]arr.slice(0);// [0,1,2,3,4,5]arr.slice(0,1);// [0 ]arr.slice(1);// [ 1,2,3,4,5]arr.slice(1,2);// [ 1 ] arr.slice(2,4);// [ 2,3 ]arr.slice(4);// [ 4,5]arr.slice(5);// [ 5]constnewArr=arr.slice();// clone the array
Set
constset=newSet([2,2,1]);// Array -> Setconstarr=[...set];// Set -> arrayset.add(4).add(3)// Add element to LAST [2,1,4,3]set.delete(3)// Delete element [2,1,4]constentriesArr=set.entries();// Create an iterator for loop [value, value]entriesArr.next().value;// [2,2]set.has(1);// Check if set has 1 => TRUEset.values();// return iterator object from the insert orderset.keys();// return iterator object from the insert order (same as values())set.clear()// Clear all elementsconstset=newSet([2,1,4])constset2=newSet([4,2,1,5])constunionSet=set.union(set2)// Union 2 sets: [2,1,4,5]constintersectSet=set.intersection(set2)// Intersect 2 sets: [2,1,4]constdiffSet=set.difference(set2)// Difference of 2 sets: [5]
HashMap
constmap=newMap([[1,2],[2,3],[4,5]]);map.set(6,7);// Add new key-value => { 1 => 2, 2 => 3, 4 => 5, 6 => 7 }map.has(3);// Check if key 3 exist => FALSEmap.get(1);// get value of key 1 => 2map.delete(1);// delete key-value of 1 => { 2 => 3, 4 => 5, 6 => 7 }map.size();// get the size of a map => 3[...map.keys()];// get the list of keys => [2,4,6]map.forEach((val,key)=>console.log({key, val}));// {key: 2, val: 3}// {key: 4, val: 5}// {key: 6, val: 7}
Object
constobj={"firstName": "Calvin","age": 23,"numList": [1,2,3]};// access the valueconst{ firstName, age, numList }=obj;// Get the key-value: firstName = "Calvin", age = 23, numList = [1,2,3]obj.newNumList=[...numList,4];// Add new key - value: [1,2,3,4]deleteobj.numList// Remove the key-value: { "firstName": "Calvin", "age": 23, "newNumList": [1,2,3,4] };
varpi=3.141;pi.toFixed(0);// returns 3pi.toFixed(2);// returns 3.14 - for working with moneypi.toPrecision(2)// returns 3.1pi.valueOf();// returns numberNumber(true);// converts to numberNumber(newDate())// number of milliseconds since 1970parseInt("3 months");// returns the first number: 3parseFloat("3.5 days");// returns 3.5Number.MAX_VALUE// largest possible JS numberNumber.MIN_VALUE// smallest possible JS numberNumber.NEGATIVE_INFINITY// -InfinityNumber.POSITIVE_INFINITY// Infinity