Callback Functions

Hi, this is actually my first post on forum and I haven’t had any problems so far but now I feel stuck a bit.
I do not fully understand Callback Functions and I went to cheat sheet to read about it and I understand in theory how it works but looking at examples still I do not get it.

const isEven = (n) => {
  return n % 2 == 0;

let printMsg = (evenFunc, num) => {
  const isNumEven = evenFunc(num);
  console.log(`The number ${num} is an even number: ${isNumEven}.`)

// Pass in isEven as the callback function
printMsg(isEven, 4); 
// Prints: The number 4 is an even number: True.

could you kindly explain step by step how you do callback function within another function ?

by the way I am doing the lesson The .forEach() Method at the moment and before I move forward I want to be clear.

The key is just to play around with trying different simple variations.
It’s interesting to note here how calling the function without the () reacts. In one instance it prints out the function text, in another it doesn’t.

let greeting = function() {
  return 'hi';

let speak = (words) => {

//function() {
//return 'hi';

// hi

//Output: [Function: greeting]

//Output: hi

1 Like

now looks much more clear to me, I think I finally get it.
well as far as I understand the second function has assigned argument (words) and then is logged to the console so if we put together
we pass the arguments to the other function.
I will keep practicing

thank you

Yes, it’s just an example I improvised. But feel free to change it around and see if it breaks or you can get it to do other stuff.

It’s these moments of sandbox work that help cement fundamentals.

var pickNumber = Math.random() * 10;
let randomNumber = function () {
    return Math.floor(pickNumber) * 10;

let chosenNumber = (num) => {
    console.log(`this is the number that I picked ${num}`);


I created simple code just to see if I got it right, what do you think ?

Perfect. It’s a pretty important concept in all of programming so it’s good to know.

yes Indeed, now I am going through The .forEach() Method and The .map() Method
they look a bit different I will try to do them and let’s see how it goes.

1 Like