Search within TutorialsPark

Javascript String Object


Learn to use various properties and methods of String Object.


Javascript String Object: Introduction

The JavaScript String Object is a core JavaScript Object which is used to treat strings as an object, also called as a wrapper object since it wraps itself to a primitive string, enabling you to apply various properties and methods to it.

A String object is created using the keyword new and the String() object constructor method, and values is assigned to a JavaScript variable using a quoted String ("") of text.

Syntax: Javascript String Object.

Example: Javascript : String Object.

Give it a TRY! » Note: $1 represents the first captured subpattern and $2 represents the second subpattern.


Javascript String Object : Properties

To describe the attributes of the string object a number of properties are available.

length : To find the length of the characters in the string.

constructor : To get the function which created the String Object.

prototype : To extend the string function by attaching more properties and methods to it.

Example: JavaScript String Object : Properties

Give it a TRY! » Note: Always use Math object instead of regular JavaScript Code.


Javascript String Object: Prototypes

There is always a need to generate a random number to display some random image, message, ads etc in an unbaised way, for that very purpose we use Math.random() method.

The method Math.random() generates a random fraction number between 0 to 1, hence to have integer you should multiply it with integers and then truncate numbers after decimal point using Math.floor() method.

Example: JavaScript Math Object : Generate Random Numbers

Give it a TRY! » Note: Multiplying the number of 20 produces numbers greater than 0 and less than 20, method floor() round the number downwards.


Javascript String Object: Methods

The String object provides methods that can be used to create effects which are usually achieved using HTML elements,like fontsize, fontcolor, fontweight.

Example: JavaScript Math Object : Generate Random Numbers

Give it a TRY! » Note:Its always better to use CSS or JavaScript style property.


Javascript String Object: Search and Replace Methods

The String object provides methods to search for strings and then replace them with another.

Example: JavaScript String Object : Methods

Give it a TRY! » Note:The Substring method gets the substring found at position 10,3


Javascript String Object: String Case Methods

The String object provides methods to perform case coversion.

The methods used for that purpose are toLowerCase() , toLocaleLowerCase() , toUpperCase() and toLocaleUpperCase()

Locale methods are used to convert based on particular locale, which are identical to generics in many except a few languages like Turkish

Example: JavaScript String Object : String Case Methods

Give it a TRY! » Note:The locale methods implement particular locale specific methods.


Javascript String Object: Pattern Matching

The String object provides methods to carry out pattern matches within a string, the most common one being match().

Example: JavaScript Math Object : Generate Random Numbers

Give it a TRY! » Note: Generally, regular expressions are used to as patterns.s


Javascript String Object: String Manipulation

The String object provides an number of methods for string manipulation.

Example: JavaScript String Object : String Manipulation

Give it a TRY! » Note:Refer to the reference to learn all string manipulation methods in detail.


Javascript String Object: localeCompare() Methods

The String object methods of localeCompare() is used to compare one string to another and return values based on that.

A negative number is returned if the string should come alphabetically before the string.

A zero is returned if the string is equal to the string argument.

A positive number is returned when string should come alphabetically after the string argument.

Example: JavaScript String Object : localeCompare() Methods

Give it a TRY! » Note:The localeCompare() implements comparison based on local standards and is case sensitive.