Skip to content

Instantly share code, notes, and snippets.

@BenGitsCode
Created April 5, 2017 20:36
Show Gist options
  • Save BenGitsCode/3d75b1fe8297ef5bc376c811d8d5710b to your computer and use it in GitHub Desktop.
Save BenGitsCode/3d75b1fe8297ef5bc376c811d8d5710b to your computer and use it in GitHub Desktop.
Javascript Array iteration, callbacks and function syntax
//ARRAY ITERATION METHODS USING AN ANONYMOUS CALLBACK
// const addOne = function(number){
// return (number + 1)
// }
const arr = [1,2,3,4,5]
console.log(arr.forEach(function addOne(actualItem, indexOfItem, arrayWereGoingThrough){
console.log('actual item: ', actualItem, 'index of item ', indexOfItem)
}))
=======================================================
//FILTER USING AN ANONYMOUS FUNCTION
// const isItThree = function(number){
// return number % 2 === 0
// }
const arr = [1,2,3,4,5,3]
console.log(arr.filter(randomFn = function(number){
return number % 2 === 0
}))
console.log('Calling randomFn outside filter ', randomFn(3))
==========================================================
//FILTER USING AN ANONYMOUS FUNCTION
// const isItThree = function(number){
// return number % 2 === 0
// }
const arr = [1,2,3,4,5,3]
console.log(arr.filter(randomFn = function(number){
return number % 2 === 0
}))
console.log('Calling randomFn outside filter ', randomFn(3))
==========================================================
//REGULAR OLD BORING FUNCTION
const addOne = function(number){
return number + 1
}
//ARROW FUNCTION W/O 'FUNCTION' WORD
const addOneArrow = (number, secondNumber) => {
return secondNumber + 1
}
//ARROW FUNCTION W/0 PARENS
//*Only one parameter
const addOneArrowNoParens = number => {
console.log('Hi')
return number + 1
}
//ARROW FUNCTION W/O CURLIES
//Implicit return, only one statement
const addOneArrowNoCurlies = number => number + 1
console.log(addOneArrowNoCurlies(4))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment