JavaScript Primitives & Methods

Home » JavaScript Tutorial » JavaScript Built-in Objects & Methods » JavaScript Primitives & Methods

In this lesson we’ll bridge the gap between the fundamental JavaScript data types we’ve already covered and how they interact with built-in functionality. We previously established that JavaScript has seven primitive types: String, Number, Boolean, BigInt, null, undefined, and Symbol. While these primitives are simple values and not complex objects, JavaScript is designed to temporarily treat them as objects when you try to access methods (like using toUpperCase() on a string) or properties (like using .length to get the character length of a string), making essential manipulation techniques immediately available.

1. Method (Action/Function): A method is a function that is associated with an object or a data type. It is an action or an operation that you can perform.

  • Syntax: Methods are always followed by parentheses, (), because they are functions that you are calling or executing.
  • Example: .toUpperCase() (makes string characters uppercase).
  • Purpose: It performs a task, such as transforming the string into uppercase, or searching for a substring.

2. Property (Data/Value): A property is a characteristic/detail or a data value associated with an object or a data type. It’s simply a piece of stored information.

  • Syntax: Properties are accessed directly without parentheses.
  • Example: .length (gets character length/count of string).
  • Purpose: It provides immediate information about the primitive, such as how many characters are in a string or how many elements are in an array.
FeatureAction or Data?SyntaxExample
MethodAction (a function)Requires ()myString.toUpperCase()
PropertyData (a stored value)No parenthesesmyString.length

This brings us to a fundamental contradiction in JavaScript. We learned that primitives are simple data types, not objects. If they aren’t objects, how can they have methods like .toUpperCase() attached to them?

The answer lies in a feature called Auto-Boxing.

When you attempt to access a property or method on a primitive, JavaScript performs a rapid “magic trick” in the background:

1. Wrap: JavaScript temporarily wraps the primitive value in a special Wrapper Object (e.g., new String()). This temporary object does have methods.

2. Execute: The engine runs the method you requested on that object and returns the result.

3. Discard: As soon as the operation is complete, JavaScript destroys the wrapper object, and the variable remains a simple primitive.

This process happens so quickly (in milliseconds) that it is invisible to you as a developer. It allows you to enjoy the performance benefits of lightweight primitives while still accessing powerful object-like utility methods.

const nameObj = "Rodney";
console.log(nameObj.toUpperCase()); // Output: "RODNEY"
// The JavaScript "engine" temporarily converted the string to an object to perform an action or function on it into uppercase characters.
JavaScript

Because these wrapper objects exist (specifically String, Number, and Boolean), beginners are often tempted to create variables using them explicitly with the new keyword. You should generally avoid this.

While explicitly creating a wrapper object might look formal, it fundamentally changes the Data Type of your variable from a primitive to an object. This leads to confusing bugs, particularly when comparing values.

const nameObj2 = new String("Rodney");
console.log(typeof nameObj2); // Output: object

// The Equality Trap:
// "Rodney" (String) is NOT strictly equal to new String("Rodney") (Object)
console.log(name === nameObj2); // Output: false
JavaScript

Summary Rule: Always declare strings, numbers, and booleans using their literal forms (e.g., let x = 10 or let s = "text"). Never use new Number(10) or new String("text") unless you have a very specific, advanced reason to do so.