JavaScript String Functions, Operators and Properties


ATTENTION: THIS PAGE IS Valid HTML 5 AND IS BEST VIEWED WITH HTML 5 - Please upgrade your browser or download one of the HTML 5 compatible browsers such as Mozilla Firefox, Chrome, Opera or IE 9 (March 14, 2011 or later). For more information see HTML 5 browsers.

If you find this helpful, please click the Google +1 Button to the left, if it is white, to make it turn blue or red. Thank you! (It also helps find this page again more easily.)

PDF mobile

JavaScript String Functions, Operators and Properties

String used in examples below
var str = "HTML tags start with &#x201c;<&#x201d; &amp; end with &#x201c;>&#x201d;.";

The results in the examples below are from actual working examples of JavaScript, resulting from running actual JavaScript code in your browser, rather than simply the expected results put in HTML code. For example, a locale-sensitive comparison as performed by localeCompare will be based on the locale in your browser.

String properties
ResultString propertyUse / Result
get onlyNumberstring.lengthget length of String
String operators
ResultString operatorUse / Result
String= valueString assignment operator (JavaScript equals)
Example:var newstr = "value"a String with a value of "value"
Stringstring1 + string2concatenate two Strings
Example:var newstr = "This is" + " a test.";
String+= string2append a String to another String
Example:str += " Right on!";
Static methods of String global object
Static MethodUse / Result
Stringnew String(value)String constructor
Examples:var newstr = new String("value")a String with a value of "value"
var newstr = new String()an empty String
StringString(object)cast object to String
StringString.fromCharCode(char1[, ... charN])create a String from character codes
Examples:var newstr = String.fromCharCode(9733, 10003)
var newstr = String.fromCharCode(0x2605, 0x2713)
String functions
ResultString functionUse / Result
Stringstring.charAt(position)get character at position in string
Numberstring.charCodeAt(position)get character code at position in string
Stringstring.concat(string1[, ... stringN])append one or more Strings
Example:str.concat(" ", "Right on!")
Numberstring.indexOf(searchString[, position])find position of searchString within string; you can also search for a regular expression using
str.indexOf("with", 16)
Numberstring.lastIndexOf(searchString[, position])find last position of searchString within string
str.lastindexOf("with", 31)
Numberstring.localeCompare(string1, string2)locale-sensitive comparison of one string with another string
Note that:
  • comparison is dependent on the client system's locale, not the server-side system's locale, and
  • although a case-sensitive comparision is used, which letter in the alphabet is considered a higher priority for determining order than its case - in English, for example, lower case comes before upper case, so the order of alphabetic characters is "a", "A", "b", "B", etc. (see examples below)
Arraystring.match(/regexp/[flags])retrieve substrings of a String that match a given regular expression
Stringstring.replace(searchString, replacement)
string.replace(/regexp/flags, replacement)
replace part of one string with another string
Example:str.replace("start", "begin")[^a-z\s]/i)
Stringstring.slice(start, end)get a substring of a string
Example:str.slice(16, 31)
Arraystring.split(separator[, limit])
string.split(/regexp/[, limit])
split String based on separator
Examples:str.split(" ")[2] // third word
str.split(/\s/)[3] // fourth word
"first " + str.split(" ", 5).length + " of " + str.split(" ").length
Stringstring.toLocaleLowerCase()convert string to lower case based on client locale
Stringstring.toLocaleUpperCase()convert string to upper case based on client locale
Stringstring.toLowerCase()convert string to lower case
Stringstring.toString()the String itself, same as String.valueOf()
Stringstring.toUpperCase()convert string to upper case
Stringstring.trim()remove leading and trailing spaces
Example:" TEST ".trim()
Stringstring.valueOf()the String itself, same as String.toString()
Global functions with String parameters

For the difference between encodeURI, encodeURIComponent and escape and information on the specific characters used for encoding, see JavaScript percent encoding functions.

Function / OperatorUse / Result
StringdecodeURI(encodedURI)decode an encoded URI
StringdecodeURIComponent(encodedComponent)decode a query parameter or other encoded URI component
Examples:decodeURIComponent("decimal%E2%80%94To%E2%80%94%25") + "=" + decodeURIComponent("pct%20%3D%20dec%23%20*%20100")
StringencodeURI(unencodedURI)encode a URI
Examples:document.createElement("a").href = encodeURI(" Firm/");
StringencodeURIComponent(unencodedComponent)encode a query string parameter or other URI component
Examples:encodeURIComponent("decimal&#x2014;To&#x2014;%") + "=" + encodeURIComponent("pct = dec# * 100")
Stringescape(string)deprecated escape all special characters in a string
Examples:escape("decimal&#x2014;To&#x2014;%") + "=" + escape("pct = dec# * 100")
(any)eval(string1[, ... stringN])evaluate string as a JavaScript expression, returning the value of the last expression
Examples:eval(".67 * 100") + "%"
NumberparseFloat(string)convert String to a floating point Number
Examples:var intval = parseFloat("123.45");
NumberparseInt(string)convert String to an integer Number
Examples:var intval = parseInt("123");
JSON functions
ResultJSON functionResult
ObjectJSON.parse(string)convert JSON string to JavaScript object
Example:var obj = JSON.parse( '{"foo":"bar"}' )
StringJSON.stringify(object)convert JavaScript object to JSON string
Example:var newstr = JSON.stringify( { foo: "bar" } );

Valid HTML 5