JavaScript Introduction | Standard Objects | Global Objects and Wrapper Objects

Global Object

The global object is a predefined JavaScript object that acts as a container for global properties and global functions. The global object itself can be accessed through the this operator in the global scope.

In JavaScript, every object becomes a property of the global object. When a web browser loads a new page, JavaScript creates a new global object and initializes its properties.

Wrapper Object

var str = "string";    // Create a string
var len = str.length;  // Use the string property length

In the example above, the string literal str is not an object, but it can still use the length property. This is because when a program tries to reference a property of the string literal str, JavaScript automatically converts the string literal into an object, as if new String(str) had been called.

The temporary object created this way inherits the methods of the String object and is used to reference the property. After the property reference is complete, the temporary object is automatically deleted. A temporary object created when accessing a property of a primitive type such as a number, string, or Boolean is called a wrapper object.

var str = "string";            // Create a string literal
var strObj = new String(str);  // Create a string object
str.length;                    // The literal value internally creates a wrapper object, then references the length property.
str == strObj;                 // The equality operator treats the literal value and its wrapper object as the same.
str === strObj;                // The strict equality operator distinguishes the literal value from its wrapper object.
typeof str;                    // string type
typeof strObj;                 // object type

Standard Object

In JavaScript, standard objects are core objects that serve as the foundation for other objects.

Representative JavaScript standard objects that are frequently used include the following.

  1. Number object
  2. Math object
  3. Date object
  4. String object
  5. Array object