Other String Methods

String Methods

Other String Methods

🙋 Need help? Ask an expert now!

Similar to arrays, there are other string methods that you might want to know and use in the future. These methods are valueOf(), split(), trim(), trimLeft() and trimRight(). The syntax and the function of each methods is shown below.


  • returns the primitive value of the object string


  • splits the string by separator and returns an array of strings, containing the separated parts
  • separator can be a regular expression


  • removes whitespace from the beginning and end of the string

str.trimLeft(), str.trimRight()

  • remove whitespace from the left/right side of the string

Let's take a look at the methods one by one. The valueOf() method returns the primitive value of the string, which is essentially the same thing saying console.log(str1). The split() method separates each word in str2 by detecting the space bar and places each word into an element in the array. The trim(), trimLeft(), and trimRight() methods simply remove the white spaces in the string and make it look cleaner when printed.

Edit Me on GitHub!

Application Question

Consider the following code segment:

var str = " Hell o World ! ";

What will the following code segment print to the console?