Wednesday, December 18, 2013

Javascript Puzzle - 2

12:33 AM Posted by Unknown , , , 1 comment
Courtesy: A Javascript conference video. I forgot the name of the conference. Will update.

var name = 'Mr. Bond';

    (function(){
      if(typeof name === 'undefined'){
          var name = "Mr. Bond";
          sayHello(name);
      } else {
           alert(name);
       }
})();

function sayHello(name){
    alert('hello, ' + typeof name !== 'undefined'? name: 'Mr. Robert!');
}

What this alerts? And, give me the reason for that.

1 comment:

  1. Do you want me to shout the answer and kill the puzzle ?? ;)

    ReplyDelete