1. Javascript intro
  2. Javascript basics
  3. Javascript variables
  4. Javascript functions
  5. Javascript popup boxes
  6. Javascript conditions
  7. Javascript loops
  8. Javascript arrays
  9. Javascript objects
  10. Javascript strings
  11. Javascript events
  12. Javascript errors
  13. Javascript DOM
  14. Javascript elements
  15. Javascript new windows
  16. Javascript date & time
  17. Javascript cookies
  18. Javascript print
  19. Javascript redirect
  20. Javascript void
  21. Javascript summary

Javascript String object

A String is a grouping of characters sorrounded by double quotes such as "this is a string". The String object is used to work with such entitites.

This tutorial focuses on:

Instantiating a String object

A String object is instantiated using the new keyword.

var aString = new String("Here is some text");

Alternatively, a String object can be instantiated by sorrounding text in double quotes and assigning it to a variable.

var anotherString = "Here is some more text";

Properties of the String object

var aString = "World wide web" //print the number of characters in aString document.write(aString.length);

Methods of the String object

var message = "Hello, It Is A Sunny DAY."; //print the message string in lowercase letters document.write(aString.toLowerCase());
hello, it is a sunny day.
var description1 = "Javascript is a scripting language developed by Netscape "; var description2 = "used to provide dynamic and interactive content on webpages."; //join the description1 and description2 strings //and print them document.write(description1.concat(description2));
Javascript is a scripting language developed by Netscape used to provide dynamic and interactive content on webpages.
var message = "Javascript is cool!"; //print the first four characters of the message string document.write(message.substr(0, 4));

The substr() method takes two parameters. The first parameter is a numeric value indicating where in the string to begin extracting characters from. The second parameter is also a numeric value indicating how many characters in the string to extract.

var text = "This text will be displayed in italics" //print the "text" string in italics document.write(text.italics());
This text will be displayed in italics
© Copyright 2013-2014
Terms of use | Privacy policy | Copyright information