Strings and Arrays are Viewed as Objects in JavaScript

In JavaScript, strings are stored as objects. Strings can be declared in two ways. The first one is by assigning the value within double quotes and the second one is by using the new keyword.

mystring=“Welcome to JavaScript”;

mystring=new String(“Welcome to JavaScript”);

Both the above declarations are valid and there is absolutely no difference between the two.

Calculating the length of the string

The length of a string can be calculated by using the length () method.

var mystring=” JavaScript”


The above statement produces a result of nine to be printed. The index of the last character of the string is equal to the length of the string minus one

  charAt() method

This method is used to return a character by giving its index value as an argument. The characters of a string are indexed from zero to the length minus one.

var mystring=”JavaScript”


The above statement writes the character ”S” to the current document as it is the fifth character in the string. This method can also be called with a literal :


The above version also produces the same result as before.

indexOf() method

This method returns the index Of the first occurrence of the given substring in the calling string object :

var mystring=”JavaScript”


The above script produces an output of 5 to be printed. The first occurrence of the substring occurs at the sixth character of the substring. Therefore its index value 5 is printed. Another variation of the indexOf  method is to give the occurrence number of the substring.

var mystring = “baddba”

document.write(mystring.indexOf (“ba”,2))

The above script writes the second occurrence of the string “ba” to the current document. The general syntax for the indexOf() method is :

stringName.indexOf (searchvalue,[occurrence Number])


This method similar to the indexOf() method except that the last occurrence of the specified string is returned.


The substring method can be used to return a portion of the main string. It takes in two parameters. The systax for substring() method is :

stringName.substring(startIndex, endIndex +1)

The startIndex indicates the index of the first character in the substring and endIndex indicates the last index of the substring.

var mystring=”JavaScript”

var mystring=mystring.substring(3,7)

The above statements assign ascr to the variable newstring.


The split method is used to separate a string based upon a specified delimiter. The following example illustrates the use of split :

var mystring=”JavaScript learn from the leader”

var mysplit = mystring.split(“ ”)

The above script results in mystring being split based on the delimiter blankspace. The result is stored in an array mysplit.

toLower Case() and toUpper Case()

As the names indicate the toUpperCase() and toLowerCase() methods are used to convert cases in alphabetic characters.

var s = “JavaScript”



The above script writes JAVASCRIPT and javascript to the current document.

The following is an example to demonstrate the various methods associated with the String object :



The second example illustrates the use of the substring() , toUpperCase(), toLowerCase() and the split function.

Code –



This method returns a string of the array elements. The array elements in the string are separated by commas.


Join method is similar to the toString method except that a separator string can be given. If no separator is mentioned, commas separate the array elements. The syntax for join method is :



As the name indicates the reverse method is used to reverse the elements in an array.


The sort function sorts the elements of an array in the dictionary order. An optional comparison function can be given based upon which the sorting is done. In case the comparison function is given it should take in two parameters. The function should return a negative number  if the first parameter is less than the second, zero if both parameters are equal and a positive number if the first parameter is greater than the second. The following example demonstrates the use of the various methods associated with array object.