JS: String.prototype

By Xah Lee. Date: . Last updated: .

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

〔►see JS: String Object

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

Type of String.prototype is object.

〔►see JS: Data Types

// type of String.prototype
console.log ( typeof String.prototype === "object" ); // true
console.log ( Object.prototype.toString.call( String.prototype ) === "[object String]" ) // true

Parent of String.prototype is Object.prototype.

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

〔►see JS: Prototype and Inheritance

String.prototype is the parent of all string objects.

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

The purpose of String.prototype object is to provides methods and properties useful for all string objects.

JavaScript Strings Are 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”.)

JavaScript String is 16 Bits Unit Sequence

〔►see JS: String is 16-Bit Unit Sequence

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

  1. String.prototype.constructor
  2. String.prototype.length

join, trim

  1. String.prototype.concat ( …args )
  2. String.prototype.repeat ( count ) ES2015
  3. String.prototype.trim ( )

substring

  1. String.prototype.slice ( start, end )
  2. String.prototype.substring ( start, end )
  3. String.prototype.subr ( index, length )

Search string

  1. String.prototype.indexOf ( searchString , position )
  2. String.prototype.lastIndexOf ( searchString , position )
  3. String.prototype.includes ( searchString , position ) ES2015
  4. String.prototype.startsWith ( searchString, position ) ES2015
  5. String.prototype.endsWith ( searchString, endPosition ) ES2015

Search or replace with regex

  1. String.prototype.replace (searchValue, replaceValue )
  2. String.prototype.search ( regexp )
  3. String.prototype.match ( regexp )

Convert to array

  1. String.prototype.split ( separator, limit )

Convert upper/lower case

  1. String.prototype.toLowerCase ( )
  2. String.prototype.toUpperCase ( )
  3. String.prototype.toLocaleLowerCase ( … )
  4. String.prototype.toLocaleUpperCase ( … )

Get character and Unicode

  1. String.prototype.charAt ( pos )
  2. String.prototype.charCodeAt ( pos )
  3. String.prototype.codePointAt ( pos ) ES2015

Convert to string

  1. String.prototype.toString ( )
  2. String.prototype.valueOf ( )

misc

  1. String.prototype.localeCompare ( that , … ) ES2015
  2. String.prototype.normalize ( form ) ES2015
  3. String.prototype [ Symbol.iterator ]( ) ES2015

constructor

String.prototype.constructor

Value is the String function object.

〔►see JS: String Object

〔►see JS: Property Key "constructor"

console.log ( "abc".constructor === String ); // true

console.log ( String.prototype.constructor === String ); // true

ECMAScript 2015 §Text Processing#sec-string.prototype.constructor

length

〔►see JS: String.prototype.length

concat

〔►see JS: String.prototype.concat

repeat

〔►see JS: String.prototype.repeat

trim

〔►see JS: String.prototype.trim


slice

〔►see JS: String.prototype.slice

substring

〔►see JS: String.prototype.substring

substr

〔►see JS: String.prototype.substr


Search String

indexOf

〔►see JS: String.prototype.indexOf

lastIndexOf

〔►see JS: String.prototype.lastIndexOf

includes

〔►see JS: String.prototype.includes

startsWith

〔►see JS: String.prototype.startsWith

endsWith

〔►see JS: String.prototype.endsWith


replace, search, match

There are 3 string methods for searching string with RegExp patterns. They are:

There is also RegExp object that has its own methods.

〔►see JS: RegExp Object

〔►see JS: RegExp.prototype


split

〔►see JS: String.prototype.split


toUpperCase, toLowerCase, toLocaleUpperCase, toLocaleLowerCase

s.toUpperCase() → Convert s to upper case.

s.toLowerCase() → Convert s to lower case.

console.log ( "abc".toUpperCase() === "ABC" ); // true

// works on Greek or any unicode that has upper/lower case defined
console.log ( "αβχ".toUpperCase() === "ΑΒΧ" ); // true

console.log ( "ABC".toLowerCase() === "abc" ); // true
console.log ( "ΑΒΧ".toLowerCase() === "αβχ" ); // true

ECMAScript 2015 §Text Processing#sec-string.prototype.touppercase

ECMAScript 2015 §Text Processing#sec-string.prototype.tolowercase

s.toLocaleUpperCase() → same as s.toUpperCase(), except that it may be different in some languages (For example, Turkish). JavaScript implementation may not chose to have this behavior. In that case, its identical to s.toUpperCase().

Here's quote from spec:

An ECMAScript implementation that includes the ECMA-402 Internationalization API must implement the toLocaleUpperCase method as specified in the ECMA-402 specification. If an ECMAScript implementation does not include the ECMA-402 API the following specification of the toLocaleUpperCase method is used.

This function interprets a String value as a sequence of UTF-16 encoded code points, as described in 6.1.4.

This function works exactly the same as toUpperCase except that its result is intended to yield the correct result for the host environment’s current locale, rather than a locale-independent result. There will only be a difference in the few cases (such as Turkish) where the rules for that language conflict with the regular Unicode case mappings.

ECMAScript 2015 §Text Processing#sec-string.prototype.tolocaleuppercase

s.toLocaleLowerCase()

ECMAScript 2015 §Text Processing#sec-string.prototype.tolocalelowercase


charAt, charCodeAt, codePointAt

〔►see JS: String.prototype.charAt

〔►see JS: String.prototype.charCodeAt

〔►see JS: String.prototype.codePointAt


toString, toValueOf

s.toString()

the primitive string

ECMAScript 2015 §Text Processing#sec-string.prototype.tostring

s.toValueOf()

ECMAScript 2015 §Text Processing#sec-string.prototype.valueof

localeCompare

s.localeCompare()

negative, 0, or positive integer.

ECMAScript 2015 §Text Processing#sec-string.prototype.localecompare


localeCompare ( that , … )

String.prototype.localeCompare ( that , … )

normalize ( form )

String.prototype.normalize ( form )

ECMAScript 2015 §Text Processing#sec-string.prototype.normalize

[ Symbol.iterator ]( )

String.prototype [ Symbol.iterator ]( )

ECMAScript 2015 §Text Processing#sec-string.prototype-@@iterator

Reference

ECMAScript 2015 §Text Processing#sec-string.prototype

String Topic

  1. JS: Default Charset/Encoding
  2. JS: String is 16-Bit Unit Sequence
  3. JS: Unicode Character Escape Sequence
  4. JS: Allowed Characters in Identifier
  5. HTML: Allowed Characters in id Attribute
  6. HTML: Character Sets and Encoding
  7. HTML/XML Entity List

  1. JS: Template String
  2. JS: Convert String to Number
  3. JavaScript Encode URL, Escape String
  4. JS: Format Number
  5. JS: JSON Object

  1. JS: String Object
  2. JS: String.prototype
  3. JS: String Constructor

RegExp Topic

  1. JS: RegExp Tutorial
  2. JS: String Methods for RegExp
  3. JS: RegExp Object
  4. JS: RegExp Constructor
  5. JS: RegExp.prototype
  6. JS: RegExp Syntax

JS Object Reference

  1. Global
  2. Object
  3. Array
  4. Function
  5. String
  6. RegExp
  7. Date
  8. Set
  9. Map
  10. JSON
  11. Math
  12. Reflect
  13. Number
  14. Boolean
  15. Symbol
Like what you read? Buy JavaScript in Depth