JavaScript String Methods

TypeMethodMethodResult
String Lengthlengthvar txt = "ABCDEF";
var sln = txt.length;
It returns 6
Find
String in a String
indexOf()var str = "Please locate where 'locate' occurs!";
var pos = str.indexOf("locate");
It returns 7
Find
String in a String
lastindexOf()var str = "Please locate where 'locate' occurs!";
var pos = str.lastindexOf("locate");
It returns 21
  Both indexOf(), and lastIndexOf() return -1 if the text is not found.
Find
String in a String
lastindexOf()var str = "Please locate where 'locate' occurs!";
var pos = str.indexOf("John");
It returns -1
Find
String in a String
indexOf()var str = "Please locate where 'locate' occurs!";
var pos = str.indexOf("locate",15);
It returns 21
Search for a
String in a String
indexOf()var str = "Please locate where 'locate' occurs!";
var pos = str.search("locate");
It returns 7
The slice()
Method
slice()var str = "Apple, Banana, Kiwi";
var res = str.slice(7,13);
It returns 'Banana'
  JavaScript counts positions from zero. First position is 0.
  slice(start, end) substring(start, end) substr(start, end) are the same.
  slice(start, end) substring(start, end) substr(start, end) are the same.
The slice()
Method
slice()var str = "Apple, Banana, Kiwi";
var res = str.slice(7);
It returns 'Banana'
The slice()
Method
slice()var str = "Apple, Banana, Kiwi";
var res = str.slice(-12);
It returns 'Banana, Kiwi'
The substr()
Method
substr()var str = "Apple, Banana, Kiwi";
var res = str.substr(7,6);
It returns 'Banana'
  The second parameter specifies the length of the extracted part.
The substr()
Method
substr()var str = "Apple, Banana, Kiwi";
var res = str.substr(7);
It returns 'Banana, Kiwi'
  If you omit the second parameter, substr() will slice out the rest of the string.
Replacing
String Content
replace()str = "Please visit Microsoft!";
var n = str.replace("Microsoft", "W3Schools");
Please visit W3Schools!
  By default, the replace() function replaces only the first match.
  To replace case insensitive, use a regular expression with an /i flag (insensitive):
  Note that regular expressions are written without quotes.
Replacing
String Content
replace()str = "Please visit Microsoft!";
var n = str.replace("MICROSOFT/i", "W3Schools");
Please visit W3Schools!
  To replace all matches, use a regular expression with a /g flag (global match)
Replacing
String Content
replace()str = "Please visit Microsoft and Microsoft!";
var n = str.replace("/MICROSOFT/g", "W3Schools");
Please visit W3Schools and W3Schools!
Converting to
Upper Case
toUpperCase()var text1 = "Hello World!";
var text2 = text1.toUpperCae();
HELLO WORLD!
Converting to
Lower Case
toLowerCase()var text1 = "Hello World!";
var text2 = text1.toLowerCae();
hello world!
The concat()
Method
concat()var text1 = "Hello";
var text2 = "World";
var text3 = "text1.concat(" ", text2)
Hello World!
  The concat() method can be used instead of the plus operator. The two lines do the same.
The concat()
Method
concat()var text = "Hello" + " " + "World!";
var text = "Hello".concat(" ", "World!");
Hello World!
  All string methods return a new string. They don't modify the original string.
  Formally said: Strings are immutable: Strings cannot be changed, only replaced.
  The trim() method removes whitespace from both sides of a string.
String.trim()string.trim()var str = " Hello World!";
alert(str.trim())
HelloWorld!
The charAt()
Method
charAt()var str = "Hello World!";
str.charAt(0)
It returns H
The charCodeAt()
Method
charAt()var str = "HELLO WORLD!";
str.charAt(0)
It returns 72
  The charCodeAt() method returns the unicode of the character at a specified index in a string. The method returns a UTF-16 code (an integer between 0 and 65535).
  Converting a String to an Array
  var txt = "a,b,c,d,e";   // String
txt.split(",");   // Split on commas
txt.split(" ");   // Split on spaces
txt.split("|");   // Split on pipe
  For a complete reference, go to our Complete JavaScript String Reference.


String HTML Wrapper Methods
The HTML wrapper methods return the string wrapped inside the appropriate HTML tag.
MethodDescription
anchor()Creates an anchor
big()Displays a string using a big font
blink()Displays a blinking string
bold()Displays a string in bold
fixed()Displays a string using a fixed-pitch font
fontcolor()Displays a string using a specified color
fontsize()Displays a string using a specified size
italics()Displays a string in italic
link()Displays a string as a hyperlink
small()Displays a string using a small font
strike()Displays a string with a strikethrough
sup()Displays a string as superscript text