mardi 28 mai 2019

Using !! in an if statement ( JavaScript Study; closures)

This was posted by another student and I would love to know what the double exclamations are doing in the if statement? I just can't get my head wrapped around this? Maybe I'm overthinking this too much, but can someone please explain what is happening here. Is this just badly written code? The code runs fine, but they have a job argument that's never called and when I use if (!name) The code starts undefined and then keeps on executing the 'Peasant' string.. my head is going to explode.

function interviewQuestion(job,question) {
    return function(name) {
        if (!!name) {
                console.log(name + question);
            } else {
                console.log('Peasant' + question);
            }
        }
    };


var interviewQuestionTyrant = interviewQuestion('Tyrant',', are you born of blood and fire?');
var interviewQuestionHand = interviewQuestion('Hand', ', can you outscheme the Lannisters?');
var interviewQuestionNightswatch = interviewQuestion('NightsWatch',', who will guard the Wall?');

interviewQuestionHand();
interviewQuestionNightswatch('Edd');
interviewQuestionTyrant('Sansa');
interviewQuestionHand('Tyrion');

Aucun commentaire:

Enregistrer un commentaire