Functions as Parameters


#1

Im trying to go step by step, but I dont even understand the task of Functions as Parameters (https://www.codecademy.com/courses/introduction-to-javascript/lessons/higher-order-functions/exercises/functions-as-parameters?action=resume_content_item).

Can somebody explain, what is going on in this code:
const timeFuncRuntime = funcParameter => {
let t1 = Date.now();
funcParameter();
let t2 = Date.now();
return t2 - t1;
}
const addOneToOne = () => 1 + 1;
timeFuncRuntime(addOneToOne);

  1. Why it`s neccesary to use Date.now() method. Moreover it has a strange description:
    The Date.now() method returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC.

  2. What result we are going to get by return t2 - t1? It seems to me, that t2 and t1 have equal values…

Thanks :slight_smile:


#2

The purpose of that exercise I believe was to demonstrate not only how passing a function into another function works, but also so that you’d see the result of t2-t1 :slightly_smiling_face:

The function runs in between the two calls to Date.now(). That’s not just a date…

The Date.now() method returns the number of milliseconds elapsed since January 1, 1970

Do

console.log(timeFuncRuntime(addOneToOne));

What do you see?