Last active
October 17, 2018 22:17
-
-
Save reinislejnieks/af63f1a075e059aada4760b9b571026a to your computer and use it in GitHub Desktop.
#js tips
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// to log better | |
const a = {}; | |
const b = {}; | |
const c = {}; | |
console.log('%c Something', 'color:orange; font-weight:bold;'); | |
console.log({a, b, c}); | |
// to log array of obj with the same props | |
console.table([a, b, c]); | |
// debug with time | |
conosle.time('looper'); | |
let i = 0; | |
while (i<100000) { i++} | |
console.timeEnd('looper'); | |
// to see stack trace in console | |
let foo = () => console.trace('Hei, Amigo!'); | |
// template literals | |
let baz = { | |
name: 'David', | |
age: 24, | |
} | |
function bar(str, age) { | |
const ageStr = age > 18 ? 'old' : 'young'; | |
return `${str[0]}${ageStr} at ${age} years`; | |
} | |
let me = bar`I am ${baz.age}`; | |
console.log(me); | |
// merge objects | |
let a = { | |
prop1: 'one', | |
prop2: 'two', | |
} | |
let b = { | |
prop3: 'three', | |
prop4: 'four', | |
} | |
const result = {...a, ...b}; | |
const result2 = {...result, prop5: 'five',}; | |
console.log(result2); | |
// array push equivalent | |
let arr = ['one', 'two', 'three']; | |
let arr2 = ['four', 'five', 'six']; | |
console.log([...arr, 'four', 'five', 'six',]); | |
console.log(['four', 'five', 'six', ...arr,]); | |
console.log(['four', ...arr, 'five', 'six',]); | |
// async, await - avoids many then(), then(), then() n*1000000.... | |
const random = () => { | |
Promise.resolve( | |
console.log( | |
Math.random() | |
) | |
); | |
} | |
const sumRandomAsyncNums = async() => { | |
const first = await random(); | |
const second = await random(); | |
const third = await random(); | |
} | |
sumRandomAsyncNums(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment