Skip to content

Instantly share code, notes, and snippets.

@obymanyando
Created August 23, 2021 12:43
Show Gist options
  • Save obymanyando/629b71f750ed8268d8efc4f190f8edc9 to your computer and use it in GitHub Desktop.
Save obymanyando/629b71f750ed8268d8efc4f190f8edc9 to your computer and use it in GitHub Desktop.
Little JS Snippets
//Summing numbers
const sum = (a, b) => {
return a + b
}
console.log(sum(2, 5))
// Summing Arbitatry Numbers
const sumArbitraryNums = () {
let sum = 0
for (const num of arguments) {
sum += num
}
return sum
}
console.log(sumArbitraryNums(2, 3, 100, 5, 20, 7))
// Reduce
const reduceArbitraryNums = (...args) => args.reduce((a, b) => a + b)
console.log(reduceArbitraryNums(2, 3, 100, 5, 20, 7))
//Object
const person = {
firstName: 'Oby',
lastName: 'Manyando',
getPersonInfo1: function () {
console.log(this)
},
getPersonInfo2: () => {
//Notice behaviour of ES6 with this keyword
console.log(this)
},
}
person.getPersonInfo1()
person.getPersonInfo2()
const getPersonInfo = () => {
return `He is ${person.firstName} ${person.lastName}`
}
console.log(getPersonInfo())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment