Javascript Code Snippets
keywords before "→" are what triggers autocomplete
Loops
Keywords |
Outcome |
floop |
for (let i = 0; i < arr.length; i++){} |
fin |
for (let key in obj) {console.log(key)} |
fof |
for (let element of arr) {console.log(element)} |
Placeholder Data
dummyobj → const user1 = {
firstName: "John",
lastName: "Doe",
age: 39,
onlineStatus: false,
address: {
state: "California",
street: "39th street"
}
}
dummyarr → const arr = ["john", "doe", -1, 0, 1, true, 3, 4];
Promise
newprom → const promiseName = new Promise((resolve, reject) => {
resolve();
reject();
})
returnprom → return new Promise((resolve, reject) => {})
JSONPlaceholder URL with different endpoints
jsonurl → const url = "https://jsonplaceholder.typicode.com/(users/posts/todos)"
Fetching Data
asfun → async function functionName(){
const data = await fetch(url)
const response = await data.json();
console.log(response);
}
functionName()
thenfun → function functionName(){
fetch(url)
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.log(error))
}
functionName()
Console
clog → console.log()
ctime → console.time() console.timeEnd()