Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
JavaScript

JavaScript String trim(): Removing Whiltespace Characters

  • 16 Sep, 2025
  • Com 2
JavaScript String trim() method

JavaScript String trim() function removes whitespace characters from both the beginning (leading) and end (trailing) of a string, returning a new string without modifying the original.

Trim whitespaces from the string in JavaScript

Whitespace includes standard spaces (‘ ‘), tabs (‘\t’), newlines (‘\n’), carriage returns (‘\r’), form feeds (‘\f’), vertical tabs (‘\v’).

const str = "   iPhone, 17   ";

console.log(str)

// Output:     iPhone, 17

const trimmed = str.trim();

console.log(trimmed);

// Output: iPhone, 17

In the above code, you can see that we removed leading and trailing whitespaces from both ends of the input string. One thing to note is that internal spaces are preserved.

Syntax

string.trim()

Parameters

This method takes no arguments.

Mixed Whitespace Types

It is not necessary to have common whitespaces in the string. There are other types of whitespace characters as well, and the trim() function can handle that gracefully.

Javascript trim() with Mixed Whitespace Types

const str = "\t\n IOS 26 \r\f\v";

console.log(str)

// Output:
//        IOS 26


const trimmed_str = str.trim();

console.log(trimmed_str);

// Output: IOS 26

In this code, we used \t, \n, \r, \f, and \v whitespace characters in a string, trimmed that string, and returned a clean string. These types of whitespace characters are common in text from files or user pastes.

If no whitespace is present at the beginning or end, it returns an unmodified string because there is nothing to trim.

Unicode Whitespace

The trim() function also strips unicode whitespace characters (those with the White_Space property in Unicode, such as non-breaking spaces \u00A0 and zero-width spaces \u200B).

const str = "\u200B\u00A0Hello\u200B\u00A0";

console.log(str)

// Output:   Hello

const trimmed_str = str.trim();

console.log(trimmed_str);

// Output: Hello

It removes zero-width spaces (\u200B) and non-breaking spaces (\u00A0).

This is helpful for international or web-scraped text.

Empty String

If the input string is empty, the output is also an empty string without any changes, and no error is thrown.

const str = "";

console.log(str)

// Output: 

const trimmed = str.trim();

console.log(trimmed);

// Output:

String with only whitespace

If all the content in a string is whitespace, it returns an empty string.

It helps validate non-empty inputs (e.g., if (input.trim() === ”) { /* handle empty */ }).

const str = "   \t\n ";

console.log(str)

// Output:
// 

const trimmed = str.trim();

console.log(trimmed);

// Output:

TypeError: Cannot read properties of null (reading ‘trim’)

If the input is null or undefined, it will throw the TypeError: Cannot read properties of null (reading ‘trim’) error.

let str = null;

console.log(str.trim()); 

// TypeError: Cannot read properties of null (reading 'trim')

To resolve this error, use the optional chaining operator (?), which returns undefined.

let str = null;

console.log(str?.trim());

// Output: undefined

Remove internal whitespace from a string

Using the string.trim() method, you cannot remove whitespace between words within a string. It can only remove from the start and end.

To remove whitespace inside a string in JavaScript, use the string.replace() method with a regular expression. The replace() method removes whitespace from the start, middle, and end points, meaning it removes whitespace from anywhere.

Remove internal whitespace from a string in JavaScript

 

const input_string = "  This string is a string    with   lots of  whitespace.    ";

console.log(input_string)

// Output:   This string is a string    with   lots of  whitespace.

const string_without_whitespace = input_string.replace(/\s/g, '');

console.log(string_without_whitespace);

// Output: Thisstringisastringwithlotsofwhitespace.

The above output is without any whitespaces, just like a single word.

That’s all!

Post Views: 58
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

Numpy.where(): Conditional Selection of Array Elements
JavaScript String slice(): Extracting a Portion of a String

2 Comments

  1. js truncate string

    October 8, 2020 at 6:44 pm

    i anyone dealing with truncation words in strings, i recommend using the cuttr.js library https://github.com/d-e-v-s-k/cuttr-js

    Reply
  2. Sound of text

    October 21, 2023 at 7:09 pm

    Great explanation of the JavaScript string trim() method! It’s helpful to know that it can remove whitespace from both sides of a string. This is especially useful when working with user input or data that may have extra spaces or line breaks added by the user. Thanks for sharing this information!

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend