Is there a difference between $ .trim and trim?

I have this simple example below which crop is used. As per the title of the question, is there any difference between the two ?. As you can see below, they have the same result. If the answer is no, which one should you use?

I am currently using .trim()

because this is the first time I see $.trim()

.

var SampleTrim = '     TRIM         ';

console.log(SampleTrim.trim());
console.log($.trim(SampleTrim));
      

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      

Run codeHide result


+3


source to share


3 answers


String.prototype.trim

not available below IE9, otherwise they will be the same.

The recommended String.prototype.trim

polyfill on MDN is exactly the same as the jQuery source .



Faster implementation in most cases ... This JSPerf from Sumit Gulati also offers.

Realistically, the difference in performance would be very minor and I would not base my decision on performance. However, it would be better to use the inline implementation so that there is no dependency on jQuery. Major websites have dropped support for IE8 and I find it safe to follow suit (:

+7


source


The jQuery trim()

function was available long before native JavaScript. Added a functiontrim()

that is available in browsers above IE9 and the like.



Use the built-in trim feature for future projects.

+2


source


As I know SimpleTrim.trim () is a native way to trim a string, keeping up with modern browser support.

And $ .trim (SimpleTrim) is another way that jQuery helps the user to trim a string when the browser doesn't support inline wrapper.

+2


source







All Articles