JavaScript Strings

A String object in JavaScript represents a sequence of characters. Let us see what is a string and string object with its properties and methods, with live running examples.

Create a String and String Object

Let us see how to create a string. We have used double quotes here:

We can also use single quotes:

To create a string object using the new keyword:

JavaScript String Properties

The following are some of the String object properties:

  • Constructor
  • Length

Let us understand the properties one by one with examples.

string.constructor

This property returns a reference to the String function that created the object. Let us see an example:

Output

JavaScript string.constructor property

string.length

The length property returns the length of the string. Let us see an example:

Output

JavaScript string.length property

JavaScript String Methods

The following are some of the String object methods:

  • charAt()
  • concat()
  • indexOf()
  • lastIndexOf()
  • slice()
  • toUpperCase()
  • toLowerCase()

Let us understand the methods one by one with examples.

JavaScript charAt() method

The charAt() method is used to return the character at a specified index. Let us see an example to understand the charAt() method in JavaScript. We are finding the character at the 7th index:

Output

JavaScript charAt() method

JavaScript concat() method

The concat() method is used to concatenate two strings. Let us see an example to understand the concat() method in JavaScript:

Output

JavaScript concat() method

JavaScript indexOf() method

The indexOf() method is used to return the index of the first occurrence of the given value. Let us see an example to understand the indexOf() method in JavaScript. We will find the index of the first occurrence of the character i:

Output

JavaScript indexOf() method

JavaScript lastIndexOf() method

The lastIndexOf() method is used to return the index of the last occurrence of the given value. Let us see an example to understand the lastIndexOf() method in JavaScript. We will find the index of the last occurrence of the character i:

Output

JavaScript lastIndexOf() method

JavaScript slice() method

The slice() method is used to slice the string i.e., extract a section and return it as a new string. Let us see an example to understand the slice() method in JavaScript. We will extract the part of a string from specific start and end indexes:

Output

JavaScript slice() method

JavaScript toUpperCase() method

The toUpperCase() method is used to convert the string into upper case. Let us see an example to understand the toUpperCase() method in JavaScript:

Output

JavaScript toUpperCase() method

JavaScript toLowerCase() method

The toLowerCase() method is used to convert the string into lower case. Let us see an example to understand the toLowercase() method in JavaScript:

Output

JavaScript toLowerCase() method

JavaScript Numbers
JavaScript Booleans
Studyopedia Editorial Staff
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading