Methods of Primitives in JavaScript

Methods of Primitives in JavaScript

Methods of Primitives in JavaScript

In JavaScript, primitive values like string, number, and boolean act like objects because JavaScript provides them with methods. But how does that work? Let’s dive in!

1️⃣ What Are Primitives?

Primitives are the basic data types in JavaScript:
String"Hello"
Number42
Booleantrue / false
SymbolSymbol("id")
BigInt123n
Null & Undefined (⚠️ No methods on these)

📌 Important: Primitives are NOT objects, but JavaScript temporarily wraps them in objects when you call methods.

2️⃣ How Can Primitives Have Methods?

When you use a method on a primitive (like "Hello".toUpperCase()), JavaScript does the following behind the scenes:

1️⃣ Wraps the primitive in a temporary object (like new String("Hello"))
2️⃣ Calls the method on this temporary object
3️⃣ Deletes the temporary object immediately

Example:

console.log("hello".toUpperCase()); // "HELLO" // Behind the scenes: let temp = new String("hello"); console.log(temp.toUpperCase()); // "HELLO" temp = null; // Temporary object is removed

📌 Objects stay in memory, but temporary wrapper objects disappear instantly.

3️⃣ Methods of Different Primitives

🔹 String Methods

let text = "JavaScript"; console.log(text.length); // 10 console.log(text.toUpperCase()); // "JAVASCRIPT" console.log(text.toLowerCase()); // "javascript" console.log(text.indexOf("S")); // 4 console.log(text.slice(0, 4)); // "Java" console.log(text.replace("Script", "Fun")); // "JavaFun" console.log(text.includes("Java")); // true console.log(text.repeat(3)); // "JavaScriptJavaScriptJavaScript"

🔹 Number Methods

let num = 42.678; console.log(num.toFixed(2)); // "42.68" console.log(num.toPrecision(3)); // "42.7" console.log(num.toString()); // "42.678" console.log(Number.isInteger(42)); // true console.log(Number.isNaN("hello" / 2)); // true

🔹 Boolean Methods

Since true and false are primitives, they don't have many built-in methods, but they can be converted to strings or numbers.

let isActive = true; console.log(isActive.toString()); // "true" console.log(Number(isActive)); // 1 console.log(Number(false)); // 0

🔹 Symbol Methods

Symbols are unique and used as property keys.

let sym = Symbol("id"); console.log(sym.toString()); // "Symbol(id)" console.log(sym.description); // "id"

🔹 BigInt Methods

BigInt allows working with large numbers beyond Number.MAX_SAFE_INTEGER.

let big = 12345678901234567890n; console.log(big.toString()); // "12345678901234567890" console.log(big + 10n); // 12345678901234567900n console.log(big > 100000n); // true

4️⃣ Special Cases: null and undefined

🚫 null and undefined don’t have methods because they aren’t objects.

console.log(null.toString()); // ❌ TypeError console.log(undefined.toUpperCase()); // ❌ TypeError

📌 Always check for null or undefined before calling methods.

5️⃣ Summary

Primitives behave like objects temporarily
JavaScript wraps them in temporary objects to allow method calls
Strings, Numbers, Booleans, Symbols, and BigInts have useful methods
null and undefined have no methods

🚀 Now you understand how primitives work with methods! Let me know if you need more examples. 😊

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close