slice is a prototype function on Array and String. It doesn't mutate and returns a new Array/String that equals to an extract of the Array/String. splice is a prototype function on Array and is used to insert and delete elements at a given index of the array. It mutates the actual array and returns the deleted elements.
You can explicitly call obj.toString() or you can use JavaScript's coercion: (obj + ""), so the obj.toString() method is implicitly called to convert it to a string. By default, the output is not too useful; it returns [object Object], but you can override the toString() method. Or, if you want to serialize the object into a string representation, you may want to use JSON.stringify(obj)
"Hello World".split('').reduce((a, b) => b + a)
slice is a prototype function on Array and String. It doesn't mutate and returns a new Array/String that equals to an extract of the Array/String. splice is a prototype function on Array and is used to insert and delete elements at a given index of the array. It mutates the actual array and returns the deleted elements.
You can explicitly call
obj.toString()
or you can use JavaScript's coercion:(obj + "")
, so the obj.toString() method is implicitly called to convert it to a string. By default, the output is not too useful; it returns[object Object]
, but you can override the toString() method. Or, if you want to serialize the object into a string representation, you may want to useJSON.stringify(obj)
Wow i was expecting this answer for reversing a string you can even do