JavaScript From Beginner To Advanced

0% completed

Previous
Next
JavaScript - String Methods

JavaScript provides a couple of methods for working with strings, enabling developers to perform manipulations, searches, transformations, and much more. Below is a comprehensive table of string methods available in JavaScript:

JavaScript String Properties

Here's a table for the commonly used string property:

Sr.NoPropertyDescription
1lengthReturns the length of the string.

JavaScript String Methods

Here's an expanded table of string methods including the additional ones as per the tutorial:

Sr.NoMethod NameDescription
1charAt()Returns the character at the specified index (position).
2charCodeAt()Returns the Unicode of the character at the specified index.
3concat()Joins two or more strings, and returns a new joined strings.
4fromCharCode()Converts Unicode values to characters.
5includes()Determines whether a string contains a specified string/characters.
6endsWith()Determines whether a string ends with specified string/characters.
7indexOf()Returns the position of the first found occurrence of a specified value in a string.
8lastIndexOf()Returns the position of the last found occurrence of a specified value in a string.
9localeCompare()Compares two strings in the current locale.
10match()Searches a string for a match against a regular expression, and returns the matches.
11repeat()Returns a new string with a specified number of copies of an existing string.
12replace()Searches a string for a specified value, or a regular expression, and returns a string where the specified values are replaced.
13search()Searches a string for a specified value, or regular expression, and returns the position of the match.
14slice()Extracts a part of a string and returns a new string.
15split()Splits a string into an array of substrings.
16startsWith()Determines whether a string begins with specified characters.
17substr()Extracts the characters from a string, beginning at a specified start position, and through the specified number of character.
18substring()Extracts the characters from a string, between two specified indices.
19toLocaleLowerCase()Converts a string to lowercase letters, according to the host's current locale.
20toLocaleUpperCase()Converts a string to uppercase letters, according to the host's current locale.
21toLowerCase()Converts a string to lowercase letters.
22toUpperCase()Converts a string to uppercase letters.
23toString()Returns the value of a String object.
24trim()Removes whitespace from both ends of a string.
25valueOf()Returns the primitive value of a String object.
26trimStart()Removes whitespace from the beginning of a string.
27trimEnd()Removes whitespace from the end of a string.

Examples

Example 1: Using the length Property

The length property returns the number of characters in a string, including spaces and special characters.

Javascript
Javascript

. . . .
  • message.length evaluates to 13 because the string "Hello, World!" consists of 13 characters, including punctuation and spaces.

Example 2: Using the slice() Method

The slice() method extracts a section of a string and returns it as a new string, without modifying the original string. It takes two arguments: the starting index and the ending index (the end index is not included in the result).

Javascript
Javascript

. . . .
  • text.slice(0, 5) extracts characters from index 0 to index 5 (not including the character at index 5), resulting in the substring "Hello".
  • The original string, text, remains unchanged after the operation.

Example 3: Using the toUpperCase() Method

The toUpperCase() method converts the entire string to uppercase letters. It does not take any arguments and does not modify the original string.

Javascript
Javascript

. . . .
  • greeting.toUpperCase() converts "Hello, world!" to uppercase, resulting in "HELLO, WORLD!".
  • This method is often used for normalization or when comparing strings in a case-insensitive manner.

Each of these examples demonstrates practical uses of JavaScript string methods and properties, showcasing how they can be applied to manipulate and analyze text data effectively.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next