JavaScript String Reference
Complete reference guide for JavaScript string properties and methods
📚 String Reference Guide
This reference guide contains all JavaScript string properties and methods with examples. Use it as a quick lookup for string operations.
// String object example
let str = new String("Hello World");
console.log(str.length); // Property
console.log(str.toUpperCase()); // Method
Output:
11
HELLO WORLD
String Properties
| Property | Description | Example |
|---|---|---|
length
|
Returns the length of the string |
"Hello".length // 5
|
constructor
|
Returns the string's constructor function |
"Hello".constructor // String()
|
prototype
|
Allows adding properties to String objects |
String.prototype.myMethod
|
🔹 Case Conversion Methods
| Method | Description | Example |
|---|---|---|
toLowerCase()
|
Converts string to lowercase |
"HELLO".toLowerCase() // "hello"
|
toUpperCase()
|
Converts string to uppercase |
"hello".toUpperCase() // "HELLO"
|
toLocaleLowerCase()
|
Converts to lowercase using locale |
"HELLO".toLocaleLowerCase()
|
toLocaleUpperCase()
|
Converts to uppercase using locale |
"hello".toLocaleUpperCase()
|
🔹 Search Methods
| Method | Description | Example |
|---|---|---|
indexOf()
|
Returns first index of specified value |
"hello".indexOf("l") // 2
|
lastIndexOf()
|
Returns last index of specified value |
"hello".lastIndexOf("l") // 3
|
includes()
|
Checks if string contains specified value |
"hello".includes("ell") // true
|
startsWith()
|
Checks if string starts with specified value |
"hello".startsWith("he") // true
|
endsWith()
|
Checks if string ends with specified value |
"hello".endsWith("lo") // true
|
search()
|
Searches for match using regex |
"hello".search(/l/) // 2
|
match()
|
Matches against regular expression |
"hello".match(/l/g) // ["l", "l"]
|
🔹 Extraction Methods
| Method | Description | Example |
|---|---|---|
slice(start, end)
|
Extracts part of string |
"hello".slice(1, 4) // "ell"
|
substring(start, end)
|
Extracts characters between two indices |
"hello".substring(1, 4) // "ell"
|
substr(start, length)
|
Extracts specified number of characters |
"hello".substr(1, 3) // "ell"
|
charAt(index)
|
Returns character at specified index |
"hello".charAt(1) // "e"
|
charCodeAt(index)
|
Returns Unicode of character at index |
"hello".charCodeAt(0) // 104
|
🔹 Modification Methods
| Method | Description | Example |
|---|---|---|
replace(search, replace)
|
Replaces first occurrence |
"hello".replace("l", "x") // "hexlo"
|
replaceAll(search, replace)
|
Replaces all occurrences |
"hello".replaceAll("l", "x") // "hexxo"
|
concat(string2, ...)
|
Joins two or more strings |
"hello".concat(" ", "world") // "hello world"
|
repeat(count)
|
Returns string repeated specified times |
"ha".repeat(3) // "hahaha"
|
padStart(length, pad)
|
Pads string from start |
"5".padStart(3, "0") // "005"
|
padEnd(length, pad)
|
Pads string from end |
"5".padEnd(3, "0") // "500"
|
🔹 Whitespace Methods
| Method | Description | Example |
|---|---|---|
trim()
|
Removes whitespace from both ends |
" hello ".trim() // "hello"
|
trimStart()
|
Removes whitespace from start |
" hello ".trimStart() // "hello "
|
trimEnd()
|
Removes whitespace from end |
" hello ".trimEnd() // " hello"
|
trimLeft()
|
Alias for trimStart() |
" hello ".trimLeft() // "hello "
|
trimRight()
|
Alias for trimEnd() |
" hello ".trimRight() // " hello"
|
🔹 Conversion Methods
| Method | Description | Example |
|---|---|---|
toString()
|
Returns string representation |
(123).toString() // "123"
|
valueOf()
|
Returns primitive value of string |
new String("hello").valueOf() // "hello"
|
split(separator, limit)
|
Splits string into array |
"a,b,c".split(",") // ["a", "b", "c"]
|
🔹 Static Methods
| Method | Description | Example |
|---|---|---|
String.fromCharCode()
|
Creates string from Unicode values |
String.fromCharCode(72, 101, 108, 108, 111) // "Hello"
|
String.fromCodePoint()
|
Creates string from code points |
String.fromCodePoint(0x1F600) // "😀"
|
String.raw()
|
Returns raw string form of template |
String.raw`Hi\n${2+3}!` // "Hi\\n5!"
|
🔹 Quick Reference Example
Here's a comprehensive example using various string methods:
let text = " JavaScript Programming Tutorial ";
// Properties
console.log("Length:", text.length); // 35
// Case conversion
console.log("Upper:", text.toUpperCase());
console.log("Lower:", text.toLowerCase());
// Trimming
console.log("Trimmed:", text.trim());
// Searching
console.log("Index of 'Script':", text.indexOf("Script"));
console.log("Includes 'Program':", text.includes("Program"));
console.log("Starts with ' Java':", text.startsWith(" Java"));
// Extraction
console.log("Slice (2, 12):", text.slice(2, 12));
console.log("Character at 5:", text.charAt(5));
// Modification
console.log("Replace 'Java' with 'Type':", text.replace("Java", "Type"));
console.log("Split by space:", text.trim().split(" "));
// Conversion
console.log("String value:", text.valueOf());
Output:
Length: 35
Upper: JAVASCRIPT PROGRAMMING TUTORIAL
Lower: javascript programming tutorial
Trimmed: JavaScript Programming Tutorial
Index of 'Script': 6
Includes 'Program': true
Starts with ' Java': true
Slice (2, 12): JavaScript
Character at 5: S
Replace 'Java' with 'Type': TypeScript Programming Tutorial
Split by space: ["JavaScript", "Programming", "Tutorial"]
String value: JavaScript Programming Tutorial