Get Programming with JavaScript - Listing 5.01
Listing 5.01 - Relying on a variable outside of the function
var message;
var showMessage;
message = "It's full of stars!";
showMessage = function () {
console.log(message);
};
showMessage();
Further Adventures
Listing 5.01 - Relying on a variable outside of the function - Task 1
- Change the message.
var message;
var showMessage;
message = "We choose to go."; // Change the message
showMessage = function () {
console.log(message);
};
showMessage();
Listing 5.01 - Relying on a variable outside of the function - Task 2
- Change the variable name in the parentheses for console.log.
var message;
var showMessage;
message = "We choose to go.";
showMessage = function () {
console.log(msg); // Change the variable name
};
showMessage();
The msg variable has not been declared, so running the program will cause an error to be displayed.
The showMessage function in the program displays the value of a variable. If that variable does not exist, calling the function will cause an error.