...

/

Built-in String and Array Functions

Built-in String and Array Functions

Learn to use popular built-in functions related to strings and arrays in JavaScript.

Built-in functions for strings

JavaScript provides various built-in functions for dealing with strings. These functions are collectively called string methods.

Note: All string methods return a new string without changing the original one.

There are numerous tasks related to strings that make it easy for programmers to perform routine tasks. This section will provide examples and results of some commonly used string methods.

The methods related to string segmentation

We can use the following methods for string segmentation.

  • The substring(start, end) takes the starting (start) and ending (end) indices and returns the extracted substring.

  • The slice(start, end) also takes the starting (start) and ending (end) indices and returns the extracted substring.

Note: The difference between substring and slice is that substring does not allow negative indexing while slice allows, as demonstrated in the following examples.

  • The split(separator) splits a string into an array of substrings based on the separator.
    • If the separator is not provided, it returns a single-element array containing the whole string.
    • If the separator is "", it returns an array of strings, each having a single character.

The methods related to case change

These methods change the letter case of the text stored in a string (say, from lowercase to uppercase). Let’s use an example program to explore case changes.

  • The toUpperCase() method converts all characters in a string to uppercase.

  • The toLowerCase() method converts all characters in the ...

Access this course and 1400+ top-rated courses and projects.