String Functions

The JavaScript String functions are used to manipulate strings. They can be used to retrieve a subset of string from a specific string. They can also be used to modify a copy of string data. It should be noted when retrieving subset data, the functions use zero as a starting index.

Watch this video on String Functions

charAt()

It returns the char value present at the specified index.

charCodeAt()

It returns the Unicode value of a character present at the specified index.

concat()

It returns a combination of two or more strings.

indexOf()

It returns the position of a char value present in the given string. It will return -1 if the position couldn't be determined, i.e. if the character is not present in the string.

lastIndexOf()

It is similar to indexOf function, however it returns the position of a char value present in the given string by searching a character from the last position.

Other examples of String functions

  • search() - It searches a specified regular expression in a given string and returns its position if a match occurs.
  • match() - It searches a specified regular expression in a given string and returns that regular expression if a match occurs.
  • replace() - It replaces a given string with the specified replacement.
  • substr() - It is used to fetch the part of the given string on the basis of the specified starting position and length.
  • substring() - It is used to fetch the part of the given string on the basis of the specified index.
  • slice() - It is used to fetch a part of the given string.
  • toLowerCase() - It converts the given string into lowercase letters.
  • toLocaleLowerCase() - It converts the given string into lowercase letter on the basis of host's current locale.
  • toUpperCase() - It converts the given string into uppercase letters.
  • toLocaleUpperCase() - It converts the given string into uppercase letter on the basis of host's current locale.
  • toString() - It returns a string representing the particular object.
  • valueOf() - It returns the primitive value of a string object.
  • split() - It splits a string into an array of substrings based on a specified separator. It takes the separator as an argument and returns an array containing the substrings.
  • trim() - It trims off whitespace from the left and right side of the string.

Activity

Try It! Pick 5 of these methods and try them out in your console.