JavaScript String Reference (original) (raw)

Last Updated : 18 Apr, 2025

JavaScript strings are used for storing and manipulating text content. They can contain zero or more characters within single or double quotes, like “Geeksforgeeks” or ‘**Geeksforgeeks**‘.

**Syntax

String(object)

**Example: In this example, we will return the length of a string.

JavaScript `

function stringLength() { const str = "GeeksforGeeks";

console.log("String Length: " + str.length);

}

stringLength();

`

Example: Date Object’s String Value

This example demonstrates how to return the Date() object’s string value.

JavaScript `

// Date Object having date and time const currentDate = new Date()

// date object's string value const currentDate_String = currentDate.toString()

console.log("Date Object's String Value: " + currentDate_String)

`

Output

Date Object's String Value: Wed Jul 10 2024 06:48:56 GMT+0000 (Coordinated Universal Time)

The complete list of JavaScript Strings has listed below:

**JavaScript String Constructor

In JavaScript, a constructor gets called when an object is created using the new keyword.

Constructor Description
String() Convert the value of an object to a string.

**JavaScript String Properties

A JavaScript property is a member of an object that associates a key with a value. There is no static property in the string only instance property.

Instance Properties Description
length Find out the size of that object
constructor Return the string constructor function for the object.

**JavaScript String Methods

JavaScript methods are actions that can be performed on objects. There are two types of String methods in JavaScript.

Static Methods Description
fromCharCode() Create a string from the given sequence of UTF-16 code units.
fromCodePoint() Return a string or an element for the given sequence of code point values (ASCII value).
Instance Methods Description
at() Find the character at the specified index.
anchor() Creates an anchor element that is used as a hypertext target.
charAt() Returns that character at the given index of the string.
charCodeAt() Returns a Unicode character set code unit of the character present at the index in the string.
codePointAt() Return a non-negative integer value i.e, the code point value of the specified element.
concat() Join two or more strings together in JavaScript.
endsWith() Whether the given string ends with the characters of the specified string or not.
includes() Returns true if the string contains the characters, otherwise, it returns false.
indexOf() Finds the index of the first occurrence of the argument string in the given string.
lastIndexOf() Finds the index of the last occurrence of the argument string in the given string.
localeCompare() Compare any two elements and returns a positive number
match() Search a string for a match against any regular expression.
matchAll() Return all the iterators matching the reference string against a regular expression.
normalize() Return a Unicode normalization form of a given input string.
padEnd() Pad a string with another string until it reaches the given length from rightend.
padStart() Pad a string with another string until it reaches the given length from leftend.
repeat() Build a new string containing a specified number of copies of the string.
replace() Replace a part of the given string with some another string or a regular expression
replaceAll() Returns a new string after replacing all the matches of a string with a specified string/regex.
search() Search for a match in between regular expressions and a given string object.
slice() Return a part or slice of the given input string.
split() Separate given string into substrings using a specified separator provided in the argument.
startsWith() Check whether the given string starts with the characters of the specified string or not.
substr() Returns the specified number of characters from the specified index from the given string.
substring() Return the part of the given string from the start index to the end index.
toLowerCase() Converts the entire string to lowercase.
toLocaleLowerCase() Returns the calling string value converted to a lowercase letter.
toLocaleUpperCase() Returns the calling string value converted to a uppercase letter.
toUpperCase() Converts the entire string to uppercase.
toString() Return the given string itself.
trim() Remove the white spaces from both ends of the given string.
trimEnd() Remove white space from the end of a string.
trimStart() Remove white space from the start of a string.
valueOf() Return the value of the given string.

Common Use Cases of JavaScript Strings

JavaScript strings are widely used for various operations, including:

Comparison with Other Data Types

JavaScript strings can be compared with other data types to understand their unique properties: