The ES2019 specification may have been a smaller addition to JavaScript, but it still brought some interesting features. This tutorial will show you eight ES2019 features that can make your life easier. These features include trimStart()trimEnd()flat()flatMap()Object.fromEntries() and more.

String.prototype.trimStart() and String.prototype.trimEnd()

If you’ve ever worked with strings there is chance you had to deal with unwanted white space. From now on, there will be two ES2020 features that will help you with this issue. These features are .trimStart() and trimEnd() string methods. These methods do what their names imply.

They both help you trim, or remove, white space from given string. The first one, the trimStart() will remove all white space from the start of the string. The second, the trimEnd() will remove all white space from the end of the string. If you need to remove white spaces on both sides?

This gives you two options. The first option is using both these ES2019 features together. The second option is to use another string method trim(). Both will give you the result you want.

// String.prototype.trimStart() examples:
// Try string without white space:
'JavaScript'.trimStart()
// Output:
//'JavaScript'

// Try string with white space at the beginning:
' JavaScript'.trimStart()
// Output:
//'JavaScript'

// Try string with white space on both sides
' JavaScript '.trimStart()
// Output:
//'JavaScript '

// Try string with white space at the emd
'JavaScript '.trimStart()
// Output:
//'JavaScript '

// String.prototype.trimEnd() examples:
// Try string without white space:
'JavaScript'.trimEnd()
// Output:
//'JavaScript'

// Try string with white space at the beginning:
' JavaScript'.trimEnd()
// Output:
//' JavaScript'

// Try string with white space on both sides
' JavaScript '.trimEnd()
// Output:
//' JavaScript'

// Try string with white space at the emd
'JavaScript '.trimEnd()
// Output:
//'JavaScript'

#javascript #language #programming #technology

8 Useful JavaScript ES2019 Features to Know About
1.40 GEEK