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: Value Types]

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

String.prototype = {a:1};

String.prototype.concat ( "x", "y" );

console.log ( String.prototype.length === 0 ); // true

Parent

Parent of String.prototype is Object.prototype.

console.log (
 Reflect.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 (
 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 JS: 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

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

join, trim, pad

  1. String.prototype.concat
  2. String.prototype.repeat
  3. String.prototype.trim
  4. String.prototype.trimStart
  5. String.prototype.trimEnd
  6. String.prototype.padStart
  7. String.prototype.padEnd

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

str.toString()

the primitive string

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

str.toValueOf()

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

localeCompare

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

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

JS String

  1. String Overview
  2. Template String
  3. Char, Code Unit, Codepoint
  4. String Escape Sequence
  5. Unicode Escape Sequence

JS RegExp

  1. RegExp Tutorial
  2. RegExp Syntax
  3. Regex Find/Replace

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 it? Help me by telling your friends. Or, Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials

  1. HTML
  2. CSS
  3. JavaScript
  4. JS Obj Ref
  5. DOM
  6. SVG
  7. Blog