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 lesson focuses on:
- Instantiating a String object
- Properties of the String object
- Methods of the String object
Instantiating a String object
A String object is instantiated using the new keyword.
Example:
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
-
length
Returns the numbers of characters in a string.
Example:
var aString = "World wide web" //print the number of characters in //the aString string document.write(aString.length);Output:
14
Methods of the String object
-
toLowerCase()
Displays a String in lowercase letters.
Example:
var message = "Hello, It Is A Sunny DAY."; //print the message string //in lowercase letters document.write(aString.toLowerCase());Output:
hello, it is a sunny day. -
concat()
Joins two or more strings.
Example:
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));Output:
Javascript is a scripting language developed by Netscape used to provide dynamic and interactive content on webpages. -
substr(startPoint, numToExtract)
Extracts a substring from a string.
Example:
var message = "Javascript is cool!"; //print the first four characters //of the message string document.write(message.substr(0, 4));Output:
JavaThe 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.
-
italics()
Displays a string in italics.
Example:
var text = "This text will be displayed in italics" //print the "text" string in italics document.write(text.italics());Output:
This text will be displayed in italics




