W3Newbie
  • HTML Tutorial
  • CSS Tutorial
  • Web Dev
    • VS Code Tutorial
    • Command Line Tutorial
    • Git Tutorial
    • Github Tutorial
    • Sass Tutorial
  • Courses
  • Resources
  • Tutorials
  • Click to open the search input field Click to open the search input field Search
  • Menu Menu

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.

Methods VS. Properties

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

*The “Wrapper Object” (Auto-Boxing)

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

The new Keyword: A Common Pitfall

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.

< Previous
Next Lesson >

  • JavaScript Tutorial
  • JavaScript Introduction
    • JavaScript’s History
    • Getting Started with JavaScript
    • JavaScript’s Syntax & Structure
    • JavaScript Variables
    • JavaScript Data Types
    • JavaScript Escape Sequences
    • JavaScript Operators
    • JavaScript Type Coercion
    • JavaScript Expressions & Statements
  • JavaScript Built-in Objects & Methods
    • JavaScript Primitives & Methods
    • JavaScript String Properties & Methods
    • JavaScript Number Methods & Global Functions
    • JavaScript Math Object
    • JavaScript Date Object
  • JavaScript Control Flow and Collections
    • JavaScript Conditional Logic
    • JavaScript Try/Catch Synchronous Error Handling
    • JavaScript Arrays
    • JavaScript Array Methods
    • JavaScript Loops
    • JavaScript Objects
    • JavaScript Objects vs. JSON
    • JavaScript Primitive vs. Reference Values

The Newbie Template Bundle

The 12 Website Bundle Pack

Subscribe on YouTube

Web Development Courses

The HTML Email Mastery Course - Build Responsive HTML Email Templates.

Latest Tutorial Posts

  • The HTML Email Mastery Course - Build Responsive HTML Email Templates.w3newbie.com
    HTML Email Mastery Course CouponOctober 12, 2020 - 8:10 pm
  • Create A 5 Page Website With PHP Includes, HTML5, CSS3 & Bootstrap 4w3newbie.com
    Create A 5 Page Website With PHP Includes, HTML5, CSS3 & Bootstrap 4April 22, 2019 - 1:48 pm
© w3newbie.com, 2026. Terms of Use. Privacy Policy.
Scroll to top Scroll to top Scroll to top