JavaScript Strings

What is a String?

A string is a sequence of characters used to represent text. In JavaScript, strings are enclosed in single quotes ('), double quotes ("), or backticks (`).

JavaScript

String Properties

Strings have properties that provide information about the string.

Tutorials dojo strip

Length

The length property returns the number of characters in a string.

JavaScript

String Methods

JavaScript provides various methods to manipulate strings.

1. charAt()

The charAt() method returns the character at a specified index.

JavaScript

2. concat()

The concat() method concatenates two or more strings.

JavaScript

3. includes()

The includes() method checks if a string contains a specified substring.

JavaScript

4. indexOf()

The indexOf() method returns the index of the first occurrence of a specified substring.

JavaScript

5. replace()

The replace() method replaces a specified substring with another substring.

JavaScript

6. slice()

The slice() method extracts a part of a string and returns it as a new string.

JavaScript

7. split()

The split() method splits a string into an array of substrings.

JavaScript

8. substring()

The substring() method extracts characters from a string between two specified indices.

JavaScript

9. toLowerCase() and toUpperCase()

The toLowerCase() method converts a string to lowercase, and the toUpperCase() method converts a string to uppercase.

JavaScript

10. trim()

The trim() method removes whitespace from both ends of a string.

JavaScript

JavaScript Strings Example Code

Explanation of Code:

  • String Properties: Demonstrates the use of the length property.
  • String Methods: Demonstrates the use of various string methods such as charAt, concat, includes, indexOf, replace, slice, split, substring, toLowerCase, toUpperCase, and trim.
HTML

JavaScript Labs

Tutorials dojo strip