JavaScript: String.prototype

By Xah Lee. Date: . Last updated: .

String.prototype is the value of the property key "prototype" of the function String.

[see String Object]

console.log(
 String.hasOwnProperty ( "prototype" ) ); // true

Type

Type of String.prototype is Object .

console.log(
 typeof String.prototype === "object"
); // true

Object.prototype is a string object.

console.log(
 Object.prototype.toString.call( String.prototype ) === "[object String]"
); // true
console.log( String.prototype.length === 0 ); // true
console.log( "a" + String.prototype + "b" === "ab"); // true

Parent

Parent of String.prototype is Object.prototype .

console.log(
 Reflect.getPrototypeOf ( String.prototype ) === Object.prototype
); // true

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
); // true

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

String is Immutable

All JavaScript string methods return a new string. That is, if a variable named “x” is a string, as in x = "abc";, and you call a string method such as x.slice(), “x” is not changed. (we say that JavaScript string is “immutable”.)

String is 16 Bits Unit Sequence

[see Character, Code Unit, Codepoint]

Index of String

Many string methods take index (integer) as argument, or return a index.

Some string methods also allow negative index. Negative index usually means count from right to left, starting at the end of string, but some method treat negative index as 0.

The most intuitive way to understand index is to think of index as between the chars. Index 0 is before first character. Index 1 is after first character. Index -1 is the place between last character and second last character.

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. ECMAScript 2015 §Text Processing#sec-string.prototype.localecompare
str.normalize( form )
ECMAScript 2015 §Text Processing#sec-string.prototype.normalize
String.prototype[Symbol.iterator]
ECMAScript 2015 §Text Processing#sec-string.prototype-@@iterator

JavaScript String

JS in Depth
XAH  BUY NOW

JS in Depth

JS Obj Ref

DOM


JS Obj Ref

String

prototype