Javascript String

tutorial and example
tutorial and example

String object works with series of characters. It is used to store and manipulate text.

There are two ways to create string in JavaScript:

  1. String literal.
  2. Using new keyword.

String literal

By using double quotes string literal is created.

Syntax

Example

Try Now

Output

Using new keyword

Syntax

Example

Try Now

Output

String properties

There are list of properties of string object and there description.

  1. constructor.
  2. length.
  3. prototype.

Constructor– It returns a reference to the string function that created the object.

Syntax

Length-It returns the number of characters in a string.

Syntax

Prototype– It allowed to add properties and method to any object (Number, Boolean, string, and data etc.).

This is global property which is available most of the objects.

Syntax

There are methods also available in JavaScript string:

Methods Description
charAt(index) It returns the character at the given index.
concat(str) It merges the text of two string and returns new string.
indexOf(str) It returns the index position of the given string.
iastindexOf(str) It returns the last index position of the given string.
match() It is used to match regular expression against a string.
tolowercase() It is used to return given string value converted to lower case.
touppercase() It is used to return the given string value converted to uppercase.
valueOf() Returns the primitive value of the specified object.

charAt()

It is a method that returns the character from specified index.

Try Now

concat(str)

This method add two or more string and return a new string.

Try Now

Here another example of conct(str)

Try Now

indexOf(str)

String indexOf(str) method returns the index position of the given string.

Try Now

lastindexOf(str)

String lastIndexOf(str) method returns the last index position of the given string.

Try Now

match()

Try Now

toLowerCase()

It returns the given string in lowercase letters.

Try Now

toUpperCase()

It returns the given string in uppercase letters.

Try Now

valueOf()

It returns the primitive value of a string object.

Try Now