15 C
Jaipur
Friday, November 20, 2020

Javascript Trim String – Linux Hint

Must read

Telefonica: Competition easing in Spain’s cut-throat telco market, Telefonica says – Latest News

The heavy discounts seen in Spain's telecoms market have become less generous in the fourth quarter, Telefonica's chief operating officer said, in a sign...

Ravi Shankar Prasad: Will finalise data protection law very soon: Ravi Shankar Prasad – Latest News

Union IT and Communications Minister Ravi Shankar Prasad on Thursday said the government was very keen to promote India as a very big centre...

roblox: Kids gaming platform Roblox faces hurdles ahead of public listing – Gaming News

Profanities and other offensive content that basic word-filtering tools are designed to catch can be found in some game titles and user profiles on...

AI, virtual reality can help fast track COVID-19 vaccine, experts say – Latest News

From helping in optimising the yield of therapeutics to training staff for setting up large-scale manufacturing sites, cutting-edge technologies such as artificial intelligence (AI)...

Javascript is a scripting or programming language, which is used both on the client-side and back-end of the web. Just like any other language, strings are an important type of the variables, and we often need to manipulate or alter strings as per our needs. While getting data from the user in the form fields, a programmer has to take care of a lot of things. In this article, we will have a look at javascript’s trim() function. We will learn how this function helps in beautifying the strings in javascript and how can we get rid of extra spaces. So, let’s take a look at what is a string and how we can trim the strings.

The string is a simple text or characters which can include the alphabets, numbers, or symbols.

Javascript’s trim() method trims the extra white space from both sides of the strings. Extra white space can be space or tab, etc.

Syntax

Syntax for the trim() method is as follows:

In javascript’s trim string method, we simply call the function over a string, and it trims the string into a clean, space free string. This function doesn’t take any arguments.

Let’s try some examples and understand it.

Examples

First, we suppose a string and add some extra white space around the string.

let str = ”   Linuxhint!   “

Now, to get rid of the extra white spaces from both sides, we try to apply the trim() method over that string and see how it works.


We can see in the output that the string is trimmed and there is no extra whitespace left around the string as we desired it to happen.

Now, a question arises: what if we want to trim the string from only the left side or the start of the string and vice versa. There is a built-in function for that as well. There are two different trimStart() and trimLeft() functions, but these both do the same task. So, if we want to trim the string from the left side only and want to keep the whitespaces on the right side. We can use the trimStart() or trimtrimLeft() function.


As you can see that both functions do the same task and trim the strings from the left side only.

Similarly, if we want to trim the string from the last or the right side only. We can use any of the trimEnd() or trimRight() functions.


It is observed that the string is trimmed only from the right side, as we expected.

So, this is how the javascript’s built-in functions trim(), trimStart(), trimLeft(), trimEnd() and trimRight() works and helps us in getting rid of the extra whitespace around the string.

Conclusion

In this article, we have learned about javascript’s built-in string trim() function and see it’s various implementations. We have also learned about the trimStart() and trimEnd() functions. This article includes profound and explained in-depth knowledge, the need, and the usage of the javascript’s string trim function. So, keep on learning javascript with linuxhint.com.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article

Telefonica: Competition easing in Spain’s cut-throat telco market, Telefonica says – Latest News

The heavy discounts seen in Spain's telecoms market have become less generous in the fourth quarter, Telefonica's chief operating officer said, in a sign...

Ravi Shankar Prasad: Will finalise data protection law very soon: Ravi Shankar Prasad – Latest News

Union IT and Communications Minister Ravi Shankar Prasad on Thursday said the government was very keen to promote India as a very big centre...

roblox: Kids gaming platform Roblox faces hurdles ahead of public listing – Gaming News

Profanities and other offensive content that basic word-filtering tools are designed to catch can be found in some game titles and user profiles on...

AI, virtual reality can help fast track COVID-19 vaccine, experts say – Latest News

From helping in optimising the yield of therapeutics to training staff for setting up large-scale manufacturing sites, cutting-edge technologies such as artificial intelligence (AI)...

Microsoft: Over 500 million new apps likely in 5 years – Apps News

Hyderabad: With COVID-19 pandemic leading to digital transformation at a speed never seen before, over 500 million new applications are likely to be built...