Chapter 5. The Head/Global Object

Conceptual Overview of the Head Object

JavaScript code, itself, must be contained within an object. As an example, when crafting JavaScript code for a web browser environment, JavaScript is contained and executed within the window object. This window object is considered to be the “head object,” or sometimes confusingly referred to as “the global object.” All implementations of JavaScript require the use of a single head object.

The head object is set up by JavaScript behind the scenes to encapsulate user-defined code and to house the native code with which JavaScript comes prepackaged. User-defined code is placed by JavaScript inside the head object for execution. Let’s verify this as it pertains to a web browser.

Below, I am creating some JavaScript values and verifying the values are placed in the head window object.

Live Code

<!DOCTYPE html><html lang="en"><body><script>

var myStringVar = 'myString';
var myFunctionVar = function() {};
myString = 'myString';
myFunction = function() {};

console.log('myStringVar' in window); // returns true
console.log('myFunctionVar' in window); // return true
console.log('myString' in window); // returns true
console.log('myFunction' in window); // return true

</script></body></html>

You should always be aware that when you write JavaScript, it will be written in the context of the head object. The remaining material in this chapter assumes you are aware that the term “head object” is synonymous with ...

Get JavaScript Enlightenment now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.