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>
source to share
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 (:
source to share
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.
source to share