JavaScript : String Object

In JavaScript, a string is a sequence of  characters surrounded by single or double quotes. For example :
  var mystr = "This is a Test  String.";
The above statement define a string value and stored it into the variable mystr. In JavaScript there are no distinction between single string and strings of characters. For example :
  var mystr1 = "Hello world, This is Test";
  var mychr1 = "hello";

String Object (By using new keyword) :

We can create string objects by using new keyword.
  var mystr = new String("Hello world");
The above statement creates a string object.

Lets See an Example :
<html>
<body>
  <p><b>Javascript Strings :</b></p>
  <script>
    var mystr = "This is test String.";
    var mychr = "Hello";
    var strobj = new String("Hello world") ;
    var brk = "<br/>"; // for breaking line
    document.write(mystr);
    document.write(brk);
    document.write("typeof : ", typeof mystr);
    document.write(brk);
    document.write(brk);
    document.write(mychr);
    document.write(brk);
    document.write("typeof : ", typeof mychr);
    document.write(brk);
    document.write(brk);
    document.write(strobj);
    document.write(brk);
    document.write("typeof : ", typeof strobj);
  </script> 
</body>
</html>
Output :

Javascript Strings :


Special Characters :

In JavaScript there are some reserved single character escape sequences for use in strings. At here the backslash (\) escape character turns special characters into string characters. Some of These special characters are as follows :


Code Description
\b Backspace
\t Tab (Horizontal)
\n Linefeed (newline)
\v Tab (vertical)
\f Form feed
\r Carriage return
\" Double quote
\' Single quote
\\ backslash


JavaScript String Methods:

Some of the JavaScript string methods are as follows :


Next Topic :