Skip to content

JavaScript Primitive Methods

Introduction to JavaScript Primitives and Objects

In the realm of JavaScript, a fundamental understanding of primitives and their interaction with objects is crucial. Primitives are the basic data types including numbers, strings, booleans, undefined, null, symbol, and bigint. Despite their simplicity, JavaScript allows these primitives to be used in ways that are typically associated with objects.

What Makes Primitives Unique

Primitives are unique for several reasons:

1. Immutability: Once a primitive value is created, it cannot be altered. For instance, when you create a string, you cannot change its individual characters. Any operation that seems to change a primitive actually creates a new primitive. Example:


Output appears here after Run.

2. Memory Efficiency: Primitives are stored directly in memory where the variable is located. This direct storage typically makes access to primitive values faster and more memory-efficient than objects. Example:


javascript
let num = 123; // Stored directly in memory where 'num' is located

3. Simple and Fast: Primitives are straightforward in their representation, making them simpler and faster to process compared to objects. They don't have the overhead of object properties and methods. Example:


javascript
let flag = true; // A simple boolean with no additional properties or methods

Objects in JavaScript: A Contrast

Objects are more complex structures in JavaScript. Unlike primitives:

  • Mutable: They are mutable and can store collections of data.
  • Reference Type: Objects are reference types, stored as references.
  • Versatile: They can store functions, arrays, and even other objects.

Interplay of Primitives and Objects

JavaScript treats primitives as objects when executing methods or properties. This is done through "object wrappers" – temporary objects that enable the primitive to behave like an object.

Object Wrappers: A Closer Look

Each primitive has a corresponding constructor object:

  • String for string primitives.
  • Number for numeric values.
  • Boolean for boolean values.
  • Symbol for symbol primitives.
  • BigInt for bigint numbers.

Working with String Methods

Strings, though primitives, can utilize various object methods. For instance:


Output appears here after Run.

Here, toUpperCase() is a method of the String object that is temporarily wrapped around the string primitive.

Number Methods in Action

Numbers also have methods that can be used similarly. Consider the toFixed() method:


Output appears here after Run.

This method rounds the number to the specified number of decimal places. Note that toFixed() returns a string, not a number.

Working with Boolean Methods

Booleans can be wrapped with the Boolean object to utilize additional methods. For example:


Output appears here after Run.

Here, toString() converts the boolean to its string representation.

Symbol and its Methods

Symbols, a unique primitive type in JavaScript, can also use methods from the Symbol object:


Output appears here after Run.

This demonstrates the use of toString() to get the string representation of the symbol.

BigInt and its Functionality

BigInt, designed for handling large integers, can use methods from the BigInt object:


Output appears here after Run.

toString() here is used to convert the BigInt value to a string.

These examples enhance the understanding of how booleans, symbols, and bigints interact with JavaScript object wrappers, providing additional methods and functionalities.

When Not to Use Object Wrappers

While JavaScript allows primitives to be wrapped in objects, it's important to use this feature judiciously. Avoid using new with primitives (e.g., new Number(1) or new Boolean(false)). Wrapper objects are always truthy, even when wrapping false, which can lead to unexpected results and confusion.

Converting Primitives to Objects and Vice Versa

JavaScript provides ways to explicitly convert between primitives and their object counterparts:

  • To Primitive: Using valueOf() method on objects.
  • To Object: Using new Object(primitive).

Example of conversion:


Output appears here after Run.

Summary

Understanding the nuances of primitives and their relationship with objects in JavaScript is essential for any aspiring developer. This knowledge not only clarifies the language's fundamentals but also empowers developers to write more efficient and effective code.

Practice

Which of the following methods are applicable to primitive data types in JavaScript?

Dual-run preview — compare with live Symfony routes.