In JavaScript, you've two options to declare a function:
javascript// 1. Function declaration function sum(a, b){ return a + b } // 2. Function expression const sum = (a, b) => { return a + b }
JavaScript hoists function declarations (raised to the top), which means, that you can use them before the code executes.
For example:
javascript// ✅ res = 7 let res = sum(5, 2) function sum(a, b){ return a + b }
But if you try the following:
javascript// ❌ Uncaught ReferenceError: sum is not defined let res = sum(5, 2) const sum = (a, b) => { return a + b }
In this case, you need to declare the function before calling it.
Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.