Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptHow to trim a string at beginning or ending in JavaScript ?

How to trim a string at beginning or ending in JavaScript ?

This article demonstrates how to trim a string at the beginning, end, and also from both sides. For various sorts of string trimming, JavaScript provides three functions.

  • TrimLeft() function is used to remove characters from the beginning of a string.
  • TrimRight() function is used to remove characters from the end of a string.
  • Trim() function is used to remove characters from both ends.

JavaScript’s native functions, like those of many other languages, solely remove whitespace characters. We will discuss all these functions in detail, & understand them through examples.

Trimming a String at the Beginning

In this case, we trim the string at the beginning using the trimLeft() function. 

JavaScript trimLeft() Function: This method is used to eliminate white space at the beginning of a string. The string’s value is not changed in any way, if any white space is present after the string, it’s not modified.

Syntax:

string.trimLeft();

Example 1: In this example, a variable var is declared with the string ” neveropen”. Notice the given string that has whitespace at the left end. The trimLeft() function will remove the whitespace at the beginning.

Javascript




const word = " neveropen";
console.log("Initial String:" + "'" + word + "'");
 
// Trimming the string at the Beginning
let new_word = word.trimLeft();
console.log("Modified String:" + "'" + new_word + "'");


Output

Initial String:' neveropen'
Modified String:'neveropen'

Example 2: In this example, a variable var is declared with the string ” neveropen “. Notice the given string that has whitespace at both ends. trimLeft() will only remove the whitespace at the beginning and leaves the whitespace at the end unchanged.

Javascript




const word = "  neveropen  ";
console.log("Initial String:" + "'" + word + "'");
 
// Trimming the string at the start
let new_word = word.trimLeft();
console.log("Modified String:" + "'" + new_word + "'");


Output

Initial String:'  neveropen  '
Modified String:'neveropen  '

Trimming the String at the End

In this case, we trim the string at the end using the trimRight() function.

JavaScript trimRight() Function: This method is used to eliminate white-space from the end of a string. The string’s value is not changed in any way, if any white space is present before the string, it’s not modified.

Syntax:

string.trimRight();

Example 1: In this example, a variable var is declared and string “neveropen ” is given to it. Notice the given string which has whitespace at the right end, so trimRight() removes the whitespace at the end.

Javascript




const word = "neveropen ";
console.log("Initial String: " + "'" + word + "'");
 
// Trimming the string at the right end
let new_word = word.trimRight();
console.log("Modified String: " + "'" + new_word + "'");


Output

Initial String: 'neveropen '
Modified String: 'neveropen'

Example 2: In this example, a variable var is declared and string ” neveropen ” is given to it. Notice the given string that has whitespace at both ends. The trimRight() function removes the whitespace at the end and not at the beginning.

Javascript




const word = " neveropen ";
console.log("Initial String: " + "'" + word + "'");
 
// Trimming the string at the right end
let new_word = word.trimRight();
console.log("Modified String: " + "'" + new_word + "'");


Output

Initial String: ' neveropen '
Modified String: ' neveropen'

Trimming the string from both the ends

In this case, we trim the string at both ends using the trim() function.

JavaScript trim() Function: Trim() eliminates whitespace from both ends of a string and produces a new string with no changes to the original. All whitespace characters and all line terminator characters are considered whitespace in this context.

Syntax:

string.trim();

Example: In this example, a variable var is declared and string ” neveropen ” is given to it. Notice the given string that has whitespace at both ends. The trim() removes the whitespace at both ends. 

Javascript




const word = " neveropen ";
console.log("Initial String: " + "'" + word + "'");
 
// Trimming the string at both ends
let new_word = word.trim();
console.log("Modified String: " + "'" + new_word + "'");


Output

Initial String: ' neveropen '
Modified String: 'neveropen'

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments