Interview Question - JavaScript Arrow Functions

This is an interview question on JavaScript Arrow Function (=>)


console.log(f(10, 30))
let f = (a, b) => a + b;

Full Transcript

In this question, we have a console.log as the first statement and calling f with 10 and 30 as arguments. The next line declares an arrow function (=>), which adds the parameters together. Let's work together to find the right answer.

In the first line we're calling function f even before it was declared, it is in the next line that we have declared a variable called f, which refers to an arrow function. This arrow function has two parameters, a and b, and return addition of a and b. Since we are declaring an arrow function and not the function declaration syntax, there will be no hoisting performed when the code executes and therefore the ReferenceError will be the error as the output, which is the correct answer. Let's run the code in the IDE to prove that. Over here, as we hit run, we can see that the "ReferenceError f is not defined" is the correct answer.