You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Converting string received from Local Storage to array
letvalue=JSON.parse(item);
Setting items to Local Storage
localStorage.setItem("key",item);
Converting array to string for local storage
letitem=JSON.stringify(value);
Removing whole key-value pair from Local Storage
localStorage.removeItem("key");
Strings in Javascript
String length
letstr="Some text here";letlen=str.length;// reutrns length of str
Uppercase
str.toUpperCase();
Lowercase
str.toLowerCase();
Indexing
str.indexOf('t');// returns 1st occurance of 't' else -1str.lastIndexOf('t');// returns last occurance of 't' else -1str.charAt(n);// returns char at nth index of 0-based index string
Substring
str.substring(m,n);// returns str from m to n-1 indexstr.substring(n);// returns str from nth to last indexstr.slice(m,n);// same as substringstr.slice(n);// if n is negative then returns last n char else same as substring
Other
str.endsWith(text);// checks if str ends with textstr.includes(text);// checks if str includes textstr.split(delimiter);// returns array for string splitted over delimiter
Arrays in Javascript
Push and Pop
constarr=[23,43,64,12,93,20];arr.push(num);// push element at end of arrayarr.pop();// pop element from end of array
Unshift and Shift
arr.unshift(num);// push element at front of arrayarr.shift();// pop element from front of array
Splice
arr.splice(m,n);// removes n elements from index m (inclusive)
letobj={
name ="Atul Rustagi",
age =23,
isActive =true}for(letkeyinobj){console.log(`The ${key} of object is ${obj[key]}`);}
Events
Calling a function on event
letelement=document.getElementById('some-id');element.addEventListener("event",function(e){// function body})element.addEventListener("event",(e)=>{// function body})element.addEventListener("event",CallbackFunc);functionCallbackFunc(e){// function body}