Can someone explain to me the Remove whitespace string?


I am on module 6 in the introduction to JavaScript.

question 2. says to use the JavaScript doc to find a string method that trims the white space. When I looked at the documentation I found String.prototype.trim(), so when I typed that in it gave me a long red error message. When I used just .trim everything worked. Why wouldn’t the first method work and why would I have to type in .trim instead?

String is a global object, so when you define a string, you create an instance of the global String object

a typical object (including String) inherits properties (including methods) from Object.prototype, or in your case from String.prototype

That is why its indicate as such in the documentation, but calling a method on a (String) object instance doesn’t require prototype.

Don’t worry if not all that makes sense yet, it will come

1 Like

Just curious if you know, but why does the code require only .trim instead of entering in string.prototype.trim?

I hope that makes sense?

Because its a method of String object, it gets its properties and methods from prototype (which is used for inheritance), but then to call a method, you can just use the method name.

But its explained within my answer? You can look into how String object exist within Javascript, how prototype works, and then put the pieces together.

I don’t know in that much detail, JS is not my main language, i have a general understanding.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.