Code has been added to clipboard!

String Functions

Reading time 3 min
Published Aug 9, 2017
Updated Oct 10, 2019

JavaScript String Functions: Main Tips

  • Strings in JavaScript are a data type for storing character sequences.
  • Strings can be modified with a variety of JavaScript string functions: replace(), search(), substr() and many more.
  • JavaScript strings are a primitive data type but can still access various methods and properties.

List of Properties

The JavaScript string object contains several properties. By using them, you can get information on the string or provide some additional features.

For instance, the length will indicate how long the string is, and the constructor reveals the constructor of the string.

The table below explains all of the possible properties:

Property Description
constructor Return string's constructor function.
length Return length of string.
prototype Add properties and methods to object.

Common String Methods

The string manipulation is completed with JavaScript string functions for modifying, deleting, organizing, or replacing entire strings or their portions.

The variety of methods allows you to arrange strings into arrays or to extract a specific portion of the string as well.

In the table below, we provide a brief introduction to the JavaScript string functions.

Some of these functions have their own separate tutorials prepared as well. You can read more about string.includes(), replace(), slice(), splice(), and others.

Method Description
charAt() Return character at specified index (position).
charCodeAt() Return Unicode of the character at specified index.
concat() Join multiple strings, and return new joined ones.
endsWith() Check whether a string ends with specified string/characters.
fromCharCode() Convert Unicode values to characters.
includes() Check whether string contains specified string/characters.
indexOf() Return position of the first found occurrence of specified value in string.
lastIndexOf() Return position of last found occurrence of specified value in string.
localeCompare() Compare two strings in current locale.
match() Search string for matches against regular expression, and return those matches.
repeat() Return new string with specified number of copies of existing string.
replace() Search string for specified value, or regular expression, and a return new string where specified values are replaced.
search() Search string for specified value, or regular expression, and return position of match.
slice() Extract part of string and return new string.
split() Split string into array of substrings.
startsWith() Check whether a string begins with specified characters.
substr() Extract characters from string, beginning at specified start position, and through specified number of characters.
substring() Extract characters from string, between two specified indices.
toLocaleLowerCase() Convert string to lowercase letters, according to host's locale.
toLocaleUpperCase() Convert string to uppercase letters, according to host's locale.
toLowerCase() Convert string to lowercase letters.
toString() Return value of string object.
toUpperCase() Convert string to uppercase letters.
trim() Remove whitespace from both ends of string.
valueOf() Return primitive value of String object.

Description of HTML Wrapper Methods

The HTML wrapper methods return the string enclosed within the appropriate HTML tag. In other words, you can manipulate the JavaScript string format.

After you create a new string, you link a string object to it as well.

Due to the string object, you can select from a variety functions to manage the JavaScript string format. The following table consists of methods that can be applied to change format of strings:

Method Description
anchor() Create anchor.
big() Display string in a bigger font
blink() Display a blinking string
bold() Display string in bold
fixed() Display string using fixed-pitch font
fontcolor() Display string using a defined color
fontsize() Display string using a defined size
italics() Display string in italic
link() Display string as hyperlink
small() Display string in a smaller font
strike() Display string with strikethrough
sub() Display string as subscript text
sup() Display string as superscript text
What Is JavaScript Used For?
Tutorial
Introduction
Output
Syntax
Comment
Commands
Operators
Comparison and Logical Operators
Data Types
Math.random()
Type Conversion
Function Definitions
Events
Objects
Object Properties
Prototype
Array
Sorting Arrays
Strings
Numbers
Number Format
Math Object
Onclick Event
Date
Date Formats
Scope
Regular Expressions
Reserved Words
Common Mistakes
Performance
Forms
Form Validation
Window: The Browser Object Model
Popup Boxes
Cookies
JSON
AJAX Introduction
AJAX Form
Automatic File Download
Functions
Array Methods
String Methods
Date Methods
Timing Events
Cheat Sheet
JavaScript in HTML
HTML DOM Methods
HTML DOM Changing HTML
HTML DOM Animation
HTML DOM EventListener
HTML DOM Navigation
HTML DOM NodeList
HTML DOM Element Nodes
Array Functions
Boolean
Calling a Function
Date Functions
Global Objects
Input Text
Operator
Statements
String Functions
Math
Math.random
Number
RegEx
alert
array.filter
array.length
array.map
array.reduce
array.push
array.sort
break and continue
className
confirm
decodeURIComponent
for
forEach
if
indexOf
innerHTML
location.reload
number.toString
onclick
onload
parseInt
prompt
replace
setAttribute
setInterval
setTimeout
slice
splice
string.includes
string.indexOf
string.split
style.display
submit
substr
substring
switch
test
throw, try and catch
toLowerCase
toUpperCase
use strict
while
window.history
window.location
window.navigator
window.screen