If you find that the splice return behavior is annoying, as many do, and you have to use it often enough in the same program, you might consider adding one of the following two custom prototype functions to use instead.
There is a popular spliced option that behaves as you expected, splice will behave in your question.
Array.prototype.spliced = function() { Array.prototype.splice.apply(this, arguments); return this; } value = "c, a, b"; value = value.split(',').spliced(1, 1).join(','); console.log(JSON.stringify(value));
Another option is one that can be used in various situations. It will perform any array function specified using parameters after it, and will always return an array.
Array.prototype.arrEval = function() { var args = Array.prototype.slice.call(arguments); Array.prototype[args.shift()].apply(this, args); return this; }
You can also do the same in one line without a user-defined function using the built-in variable assignment, for really bold.
value = "c, a, b" value = ((arr = value.split(',')).splice(1, 1), arr.join(',')) alert(value);
source share