More specifically, I'm referring to this method from Lodash and this JavaScript method.
When would you choose to use one over the other?
More specifically, I'm referring to this method from Lodash and this JavaScript method.
When would you choose to use one over the other?
In my limited test below, Lodash handles null & undefined by returning an empty string. It also contradicts the ECMAScript standard by returning -0 for -0 where the native method returns 0. (see: 6.1.6.1.20)
You can check out the source for lodash here for implementation details: https://github.com/lodash/lodash/blob/master/toString.js
The native toString behavior is defined by tc39's ECMAScript 2021 Language Specification 7.1.17
The tests are not exhaustive so feel free to add your own.
console.log("native: " + [1,2,3].toString());
console.log("lodash: " + _.toString([1,2,3]));
console.log("native: " + [[1,2,3],[1,2,3],[1,2,3]].toString());
console.log("lodash: " + _.toString([[1,2,3],[1,2,3],[1,2,3]]));
console.log("native: " + (-0).toString());
console.log("lodash: " + _.toString(-0));
console.log("native: " + (-0.0).toString());
console.log("lodash: " + _.toString(-0.0));
console.log("native: " + (Infinity).toString());
console.log("lodash: " + _.toString(Infinity));
console.log("native: " + (-Infinity).toString());
console.log("lodash: " + _.toString(-Infinity));
console.log("native: " + (NaN).toString());
console.log("lodash: " + _.toString(NaN));
console.log("native: " + ({foo:'bar'}).toString());
console.log("lodash: " + _.toString({foo: 'bar'}));
console.log("native: " + [{foo:'bar'},{foo:'bar'},{foo:'bar'}].toString());
console.log("lodash: " + _.toString([{foo:'bar'},{foo:'bar'},{foo:'bar'}]));
console.log("native: " + Object.prototype.toString.call(null));
console.log("lodash: " + _.toString(null));
console.log("native: " + Object.prototype.toString.call(undefined));
console.log("lodash: " + _.toString(undefined));
console.log("native: " + document.toString());
console.log("lodash: " + _.toString(document));
console.log("native: " + (new Set()).toString());
console.log("lodash: " + _.toString(new Set()));
console.log("native: " + Symbol('foo').toString());
console.log("lodash: " + _.toString(Symbol('foo')));
console.log("native: " + 'foo'.toString());
console.log("lodash: " + _.toString('foo'));
console.log("native: " + (true).toString());
console.log("lodash: " + _.toString(true));
console.log("native: " + (BigInt(100000000000000000000)).toString());
console.log("lodash: " + _.toString(BigInt(100000000000000000000)));
console.log("native: " + (new Date()).toString());
console.log("lodash: " + _.toString(new Date()));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>