I have this simple example below which uses trim. As per the title of the question, is there any difference between them?. As you can see below they have the same output. If the answer is "No", which one is much better to use?
Currently I use .trim()
because it's my first time seeing $.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>
String.prototype.trim
is not available below IE9, otherwise they are the same.
The recommended String.prototype.trim
polyfill on MDN is exactly the same as that used in jQuery source.
In most cases, the native implementation would be faster to use. This JSPerf by Sumit Gulati suggests that too.
Realistically, the difference in performance would be quite negligible and I wouldn't base my decision on performance. However, it would be better to use the native implementation so that there's no dependency on jQuery. The large websites have dropped support for IE8 and I think it's safe to follow suit (:
jQuery's trim()
function was available long before JavaScript's native trim()
function was added, which is available in browsers above IE9 and the like.
Use the native trim function for future projects.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With