JavaScript Tutorial JavaScript Examples JQuery Tutorial CSS Tutorial HTML Tutorial About Us

Lesson 11: String Object

The String object in JavaScript is for manipulating texts and characters.  It can be used to process names, addresses, phone numbers, ID, company names, serial numbers and so on. As a matter of facts,  JavaScript statements comprise strings and characters. A string is made up of letters, numbers and alphanumeric characters such as #,@,$,!,&,*,\,+,- and so on. The string object operates using various methods, as follows:

11.1 String Methods toFixed(n)

This method converts a number into a string by retaining n decimal places.


var number = 2.2689;
var Num = number.toFixed(2);
Methods Description
big( ) Add the HTML tags <big></big> to the string displays bigger text.
bold( ) Add the HTML tags <bold></bold> to the string displays bold text.
fixed( ) Add the HTML tags <tt></tt> to the string makes the text appears in typewriter style.
ilatics( ) Add the HTML tags <i></i> to the string displays text in italics.
small( ) Add the HTML tags <small></small> to the string displays smaller text.
sub( ) Add the HTML tags <sub></sub> to the string and displays the text as subscript text.
sup( ) Add the HTML tags <sup></sup> to the string and displays the text as superscript text.
strike( ) Add the HTML tags <strike></strike> to the string and displays the text as strike-through text.
fontcolor(color) Add the HTML tags <fontcolor(color)></font> to the string and displays the text in predefined color.
fontcolor(size) Add the HTML tags <fontcolor(size)></font> to the string and displays the text in predefined size.
toLowerCase( ) Convert string to all lowercase
toUpperCase Convert string to all uppercase
substring(A, B) Extracts the substring starting from position A to position B.
chartAt(index) Returns the character located at position index within the string (index with 0)
concat(text) Add text to the end of the string
toString( ) Returns the greatest integer less than or equal to x
fromCharCode() Convert a Unicode number into a character

The syntax of using the String method is  String.Method, where string can be of any acceptable variable name in JavaScript. For example,  You can define the following string:

var StudentName="John Peterson"

and manipulate the string with various methods below:

The following example demonstrates the usage of String methods.

Example 11.1

<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>String Methods</title>
myString="JavaScript Tutorial";

concatString=myString.concat(" for you")
document.write("<br>Text in Italics : "+IString);
document.write("<br>Smaller Text : "+SString);
document.write("<br>Text in Bold : "+BString);
document.write("<br>Text with bigger characters : "+BigString);
document.write("<br>Text with typewriter style : "+FixString);
document.write("<br>Subscript text: "+subString);
document.write("<br>Superscript text: "+supString);
document.write("<br>Strike-thru text: "+strikeString);
document.write("<br>Red color text: "+colorString);
document.write("<br>Fontsize +3: "+StringSize);
document.write("<br>Convert to lowercase: "+LCstring);
document.write("<br>Convert to Uppercase: "+UCstring);
document.write("<br>Extracts SubString: "+PString);
document.write("<br>Character at 9th position is : "+CharString);
document.write("<br>Concatenates String: "+concatString);
document.write("<br>Same String: "+SameString);


The output

Example 11.2: The fromCharCode() Method

This example demonstrates the usage of the fromCharCode() Method. This method converts a Unicode into a corresponding character.

<!DOCTYPE html>
<body onload="myFunction()">

<h3>The character of unicode <b id="chr"></b> is <b id="chr1"></b></h3>

function myFunction() {
var ucode = prompt("Please enter a Unicode and click OK to view the corresponding character");
var myChr = String.fromCharCode(ucode);
document.getElementById("chr1").innerHTML = myChr;
document.getElementById("chr").innerHTML = ucode;


The character of unicode is

Example 11.2: The charCodeAt(0) Method

This method returns the Unicode of the first character in a string. Index 0 indicates the first position of the string. This example returns the Unicode for the character input by the user. As the user only input one character, index 0 indicates the character is entered.

<!DOCTYPE html>
<body onload="myFunction()">

<h3>The unicode of the character <b id="chr"></b> is <b id="chr1"></b></h3>

function myFunction2() {
 var mychr = prompt("Please enter a character and click OK to view the corresponding unicode");
 document.getElementById("chr").innerHTML = mychr;
 document.getElementById("chr1").innerHTML = mychr.charCodeAt(0);


The Unicode of the character is

❮ Previous Lesson Next Lesson ❯

Copyright©2008 Dr.Liew Voon Kiong. All rights reserved |Contact|Privacy Policy