https://bigfrontend.dev/quiz/if
What does the code snippet below output by console.log
?
if (true) {
function foo() {
console.log('BFE');
}
}
if (false) {
function bar() {
console.log('dev');
}
}
foo();
bar();
if (true) {
function foo() {
console.log('BFE');
}
}
if (false) {
function bar() {
console.log('dev');
}
}
// In Chrome and Firefox:
foo(); // 'BFE'
bar(); // Error
// In Safari:
foo(); // 'BFE'
bar(); // 'dev'
When we define a function within an if
statement, the result varies on browsers. In Chrome and Firefox, only the name of the function is hoisted, whereas in Safari, the function is hoisted.