Functions are "First-class" citizens of JavaScript
They can be:
codepen demo
JavaScript syntax allows for several different function syntaxes
It is important to recognize functions as functions, regardless of the syntax used
codepen demo
codepen demo
Drive North on 1-15
Take Exit to Riverdale
Turn East
Turn on 1000 S
Turn on 4400 W
Drive for 300 feet
Navigate to
123 W 4400 S, Riverdale 84405
How do I get to your house?
Load the user table into memory
loop through rows
check if name == "jim"
if so return that row
Select * from Users
Where name == "jim"
How do I get "jim" from the user database
Codepen
const nums = [1,2,3]; nums.indexOf(2); [1,2,3].indexOf(2) //2
[ ].indexOf( )
[ ].sort( )
[ ].push( )
[ ].pop( )
[ ].join( )
. . .
Maps a set of values in an array to another set of values using a function
Maps a set of values in an array to another set of values using a function
[1,2,3,4].map(e => e*2); //[2,4,6,8]
(same number of elements)
http://codepen.io/mdjasper/pen/KaVeVK?editors=0010
Reduces a set of values to a single value using a function which accepts Current, Previous, and Initial values
Sum Function
[1,2,3,4].reduce((curr,prev)=>curr+prev, 0); //10
http://codepen.io/mdjasper/pen/PWZabX?editors=0010
Array filter accepts a function which returns a boolean. This function is a test. If the value passes the test, then it is returned into the new array
Sum Function
[1,2,3,4].filter(e => e>2); //[3,4]
http://codepen.io/mdjasper/pen/PWZabX?editors=0010
These array functions can be combined to create very powerful declarative statements
http://codepen.io/mdjasper/pen/EZPRGo?editors=0010
http://codepen.io/mdjasper/pen/RKrJmO?editors=0010