Variable Scope - Demo 2

This page alerts a function and a variable directly on the window object, to demonstrate that the global context is the window object.

Here is the code:

var sport = "baseball";   
var player = null;   
  
function getPlayer() {   
    if (sport === "baseball") {   
        player = "Evan Longoria";   
    } else {   
        player = "Eva Longoria";   
    }   
    var player2 = "Derek Jeter";   
    return player;   
}   
  
alert(window.getPlayer());   
alert(window.player);  

< Back to Article