Javascript Challenges

Hoisting and Conditionals Statements

I'm Ernie and my friend is Bert and we wrote this code to tell other people which type of birds we like.

var bird = 'Pidgeons';
( function () {
    if ( typeof bird === 'undefined' ){
        var bird = 'Rubber Duck';
        console.log('Ernie loves his ' + bird );
    } else {
        console.log('Bert loves his ' + bird );
    }
}() );

There should be a problem with our code because for some reason I only see 'Ernie loves his Rubber Duck' when I expected to see 'Bert loves his Pidgeons', could you help me?

Exercise

Correct!
False!

Why this is happening?

Exercise

Correct!
False!

Please help me and fix this code to get 'Bert loves his Pidgeons'.

var bird = 'Pidgeons'; ( function () { if ( typeof bird === 'undefined' ){ var bird = 'Rubber Duck'; console.log('Ernie loves his ' + bird ); } else { console.log('Bert loves his ' + bird ); } }() );