Created
April 5, 2017 20:36
-
-
Save BenGitsCode/3d75b1fe8297ef5bc376c811d8d5710b to your computer and use it in GitHub Desktop.
Javascript Array iteration, callbacks and function syntax
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
//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