1.

Join two or more strings in JavaScript

Answer»

The trim() method is used to remove whitespace from both sides of a string.

Let’s say we have the following string with leading and trailing whitespace:

var myStr = "     Cup of tea!    ";

To remove whitespaces, USE the trim() method: 

myStr.trim( )

The example DEMONSTRATES how to remove whitespaces from both the sides of a string in JavaScript: 

<!DOCTYPE HTML> <html> <body> <script>     var myStr = "     Cup of tea!    ";     document.writeln("String (after REMOVING leading and trailing whitespace): "+myStr.trim()); </script> </body> </html>

The output displays the same string after removing the leading and trailing whitespace:

String (after removing leading and trailing whitespace): Cup of tea!


Discussion

No Comment Found