JavaScript String trim()


String trim() method की String के दोनों side से whitespace remove करता है। लेकिन यह original String को change नहीं करता है।

JavaScript String trim Syntax

string.trim();
Parameters
  1. string | required : यह String variable है जिसमे से whitespace remove करना है।

  2. Return Value : यह original String को whitespace remove करके return करता है।

JavaScript String trim Example

File : js_string_trim.html

CopyFullscreenClose FullscreenRun
<!DOCTYPE html>
<html>
  <body>
    <script>
      let str = "           Hello World !.       ";
      document.write("Original Length : " + str.length);
      /*now remove whitespace*/
      let new_str = str.trim();
      document.write("<br> After removing whitespace : " + new_str.length);
    </script>
  </body>
</html>
Output
Original Length : 32
After removing whitespace : 14

Hey ! I'm Rahul founder of learnhindituts.com. Working in IT industry more than 4.5 years. I love to talk about programming as well as writing technical tutorials and blogs that can help to others .... keep learning :)

Get connected with me - LinkedIn Twitter Instagram Facebook