Javascript Function Argument Passing Function as Parameter

Introduction

Declare and Define the functions that will make the function calls below work properly.

Output the first element



function first( names, cb ){
  cb( names[0] );// w  w w. j  a va2s.c  o m
}


var names = ['Tyler', 'Cahlan', 'Ryan', 'Colt', 'Tyler', 'Blaine', 'Cahlan'];
first(names, function(firstName){
  console.log('The first name in names is ', firstName)
});

Display the last element from array


function last( names, cb){
  cb( names[names.length-1] )/*from  w w w . j a  v  a2 s .  com*/
}

var names = ['Tyler', 'Cahlan', 'Ryan', 'Colt', 'Tyler', 'Blaine', 'Cahlan'];
last(names, function(lastName){
  console.log('The last name in names is ', lastName);
});



PreviousNext

Related