JavaScript String Reference

JavaScript Strings

A JavaScript string stores a series of characters like "John Doe".
A string can be any text inside double or single quotes:

String indexes are zero-based:
The first character is in position 0, the second in 1, and so on.

String Properties and Methods

Normally, strings like "John Doe", cannot have methods or properties because they are not objects.
But with JavaScript, methods and properties are also available to strings, because JavaScript treats strings as objects when executing methods and properties.


Note;

All string methods return a new value.
They do not change the original variable.

MethodDescription
charAt()Returns the character at a specified index (position)
charCodeAt()Returns the Unicode of the character at a specified index
concat()Returns two or more joined strings
endsWith()Returns if a string ends with a specified value
fromCharCode()Returns Unicode values as characters
includes()Returns if a string contains a specified value
indexOf()Returns the index (position) of the first occurrence of a value in a string
lastIndexOf()Returns the index (position) of the last occurrence of a value in a string
localeCompare()Compares two strings in the current locale
match()Searches a string for a value, or a regular expression, and returns the matches
repeat()Returns a new string with a number of copies of a string
replace()Searches a string for a value, or a regular expression, and returns a string where the values are replaced
search()Searches a string for a value, or regular expression, and returns the index (position) of the match
slice()Extracts a part of a string and returns a new string
split()Splits a string into an array of substrings
startsWith()Checks whether a string begins with specified characters
substr()Extracts a number of characters from a string, from a start index (position)
substring()Extracts characters from a string, between two specified indices (positions)
toLocaleLowerCase()Returns a string converted to lowercase letters, using the host's locale
toLocaleUpperCase()Returns a string converted to uppercase letters, using the host's locale
toLowerCase()Returns a string converted to lowercase letters
toString()Returns a string or a string object as a string
toUpperCase()Returns a string converted to uppercase letters
trim()Returns a string with removed whitespaces
valueOf()Returns the primitive value of a string or a string object

JavaScript String Properties

PropertyDescription
constructorReturns the string's constructor function
lengthReturns the length of a string
prototypeAllows you to add properties and methods to an object
â €
â €

DevCrib is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using DevCrib, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 2022 by Michael Okwuosah. All Rights Reserved.

DevCrib