JS: String.prototype

By Xah Lee. Date: . Last updated: .

What is String.prototype

String.prototype is the value of the property key "prototype" of the function String. [see String Object]

String.hasOwnProperty("prototype")

Type

Type of String.prototype is Object .

typeof String.prototype === "object"

String.prototype is a string object.

console.log(
  Reflect.apply(Object.prototype.toString, String.prototype, []) ===
    "[object String]",
);

console.log(String.prototype.length === 0);
console.log("a" + String.prototype + "b" === "ab");

Parent

Parent of String.prototype is Object.prototype.

Reflect.getPrototypeOf(String.prototype) === Object.prototype

Purpose

Purpose of String.prototype is to provide methods and properties useful for all string objects.

String.prototype is the parent of all string objects.

console.log(
  Reflect.getPrototypeOf(Object("abc")) === String.prototype,
);

// Object("abc") turns primitive to object type

Properties

join, trim, pad

substring

Search string

Search / replace with regex

Convert to array

Convert letter case

Get character and Unicode

misc

str.toString
Return the string itself.
str.valueOf
Return the string itself.
str.localeCompare(str)
Return negative, 0, or positive integer, by comparing str with str, in a locale aware manner. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare
str.normalize(form)
normalize a string according to Unicode Standard Annex #15 Unicode Normalization Forms. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize
String.prototype[Symbol.iterator]
return the Iterator.

JavaScript, String

BUY ΣJS JavaScript in Depth