JS: String.prototype

By Xah Lee. Date: . Last updated: .

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

[see JS: String Object]

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

Type

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

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]

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 (
 Object.getPrototypeOf ( "abc" ) === String.prototype ); // true

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 Code Unit vs Code Point]

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
  2. String.prototype.repeat
  3. String.prototype.trim

substring

  1. String.prototype.slice
  2. String.prototype.substring
  3. String.prototype.substr

Search string

  1. String.prototype.indexOf
  2. String.prototype.lastIndexOf
  3. String.prototype.includes
  4. String.prototype.startsWith
  5. String.prototype.endsWith

Search / replace with regex

  1. String.prototype.search
  2. String.prototype.match
  3. String.prototype.replace

Convert to array

  1. String.prototype.split

Convert letter 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
  2. String.prototype.charCodeAt
  3. String.prototype.codePointAt

Convert to string

  1. String.prototype.toString
  2. String.prototype.valueOf

misc

  1. String.prototype.localeCompare
  2. String.prototype.normalize
  3. String.prototype [ Symbol.iterator ]

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(str)

Returns negative, 0, or positive integer, by comparing s with str, in a locale aware manner.

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

normalize

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: Source Code Encoding
  2. JS: String Code Unit vs Code Point
  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. JS: Encode URL, Escape String
  4. JS: Format Number
  5. JS: JSON

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

RegExp Topic

  1. JS: RegExp Tutorial
  2. JS: RegExp Syntax
  3. JS: Regex Find/Replace
  4. JS: RegExp Object
  5. JS: RegExp Constructor
  6. JS: RegExp.prototype

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
Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

Ask me question on patreon