This is how function declaration works in JavaScript:
// function declaration
function getSum(a, b) {
return a + b;
}
// function call
const result = getSum(60, 9);
console.log(result);
// 69In Python, a function must be defined before any code that executes it. But that’s not so in JavaScript! As long as a function is defined somewhere in the file, it can be called even before the definition in source
console.log(getLabel(3));
// prints 'awful'
function getLabel(numStars) {
if (numStars > 7) {
return "great";
} else if (numStars > 3) {
return "okay";
} else {
return "awful";
}
}This works because JavaScript “hoists” the function declaration to the top of the file before the code is executed.