Problem Description:
When you run the code containing the return statement directly in the script tag or in the browser console, an error is reported:
SyntaxError: Illegal return statement
if (!NaN) {
console.log('went into the conditional statement')
return "Ollie gives!"
}
reason:
In JavaScript, the return statement can only be placed in the function, otherwise the following error will pop up.
SyntaxError: Illegal return statement
Solution:
package the modified code block in function.
(function () {
if (!NaN) {
console.log('went into the conditional statement')
return "Ollie gives!"
}
})()
// Run results
It goes into the conditional statement
"Ollie gives!"