Did you find this useful?
Socialize it today.


JavaScript String Object

Friday, April 20, 2012

The JavaScript string data type is a primitive data type that represents one or more characters. JavaScript supports a string primitive and a global String object. The String object wraps the primitive string with methods and properties which are used to manipulate the string itself.


String Object Syntax

String objects can be created by either calling the new String() constructor or the String() global function. Here is an example of using both methods of creating the object:


var txt1 = new String("string");
var txt2 = "string";


String Object Properties

The following table lists the properties of the String object.


PropertyDescription
constructorReturns the function that created the String object.
lengthReturns the length of a string.
prototypeAllows you to add additional properties and methods to an object.


Here is an example of how to use the length property:


document.write("Hello!".length)


String Object Methods

The following table lists the methods of the String object.


MethodDescription
anchor()Creates an HTML anchor around text.
big()Displays a string using a the HTML big element.
blink()Displays a blinking string using the HTML blink element.
bold()Displays a string in bold using the HTML b element.
charAt()Returns the character at the specified index.
charCodeAt()Returns the Unicode of the character at the specified index.
concat()Returns a string of the joined strings.
fixed()Displays a string using a fixed-pitch font by use of the HTML TT element.
fontcolor()Displays a string using a specified color using the HTML font element and color attribute.
fontsize()Displays a string using a specified size using the HTML font elemnet and size attribute.
fromCharCode()Converts Unicode values to characters.
hasOwnProperty()Returns a Boolean value that indicates is the object has a property with the specified name.
indexOf()Returns the character position of the first found occurrence of a specified value in a string.
italics()Displays a string in italic using the HTML I element.
lastIndexOf()Returns the character position of the last found occurrence of a specified value in a string.
link()Displays a string as a hyperlink.
match()Searches for a match between a regular expression and a string, and returns the matches.
replace()Uses a regular expression to replace text in a string and returns the result.
search()Returns the position of the first substring match in a regular expression search.
slice()Returns a part of a string.
small()Displays a string using a small font using the HTML small element.
split()Splits a string into an array of substrings.
strike()Displays a string with a strikethrough using the HTML strike element.
sub()Displays a string as subscript text using the HTML sub element.
substr()Returns a substring beginning at a specified location and having a specified length.
substring()Returns the substring at a specified location within a String object.
sup()Displays a string as superscript text using the HTML sup element.
toLowerCase()Converts a string to lowercase letters.
toUpperCase()Converts a string to uppercase letters.
valueOf()Returns the primitive value of a String object


Here is an example of how to use the font color method:


document.write("Hello!".fontcolor("red"));


Always keep in mind that JavaScript is case sensitive.

Please help us spread the word by socializing it today!

email contact us

Did you find something wrong with the information on this page? Please take a moment to report it to us so that we can continue to improve the quality of the information on this site. Click here to report an issue with this page.



Recommended Books & Training Resources

Murachs JavaScript and DOM Scripting HTML CSS and JavaScript Editor JavaScript and jQuery: The Missing Manual