JavaScript: String.prototype

By Xah Lee. Date: . Last updated: .

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

〔►see JavaScript: String Object

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

Type of String.prototype is object.

〔►see JavaScript: 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 JavaScript: 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 JavaScript: 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 JavaScript: String Object

〔►see JavaScript: 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 JavaScript: String.prototype.length

concat

〔►see JavaScript: String.prototype.concat

repeat

〔►see JavaScript: String.prototype.repeat

trim

〔►see JavaScript: String.prototype.trim


slice

〔►see JavaScript: String.prototype.slice

substring

〔►see JavaScript: String.prototype.substring

substr

〔►see JavaScript: String.prototype.substr


Search String

indexOf

〔►see JavaScript: String.prototype.indexOf

lastIndexOf

〔►see JavaScript: String.prototype.lastIndexOf

includes

〔►see JavaScript: String.prototype.includes

startsWith

〔►see JavaScript: String.prototype.startsWith

endsWith

〔►see JavaScript: 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 JavaScript: RegExp Object

〔►see JavaScript: RegExp.prototype


split

〔►see JavaScript: 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 JavaScript: String.prototype.charAt

〔►see JavaScript: String.prototype.charCodeAt

〔►see JavaScript: 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. JavaScript: Default Charset/Encoding
  2. JavaScript: String is 16-Bit Unit Sequence
  3. JavaScript: Unicode Character Escape Sequence
  4. JavaScript: Allowed Characters in Identifier
  5. HTML: Allowed Characters in id Attribute
  6. HTML: Character Sets and Encoding
  7. HTML XML Entities

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

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

RegExp Topic

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

Object Reference

  1. JavaScript: Object Object
  2. JavaScript: Array Object
  3. JavaScript: Function Object
  4. JavaScript: String Object
  5. JavaScript: RegExp.prototype
  6. JavaScript: Date Object
  7. JavaScript: Set Object
  8. JavaScript: Map Object
  9. JavaScript: JSON Object
  10. JavaScript: Math Object
  11. JavaScript: Reflect Object
Like what you read? Buy JavaScript in Depth
or, buy a new keyboard, see Keyboard Reviews.