splice function cant be written inside a console.log ?? I tried both here's what I tried -
const arr1 = ["a","b","c","d","e","f"];
console.log(arr1.splice(1,4,"HI"));
output for this code= [ 'a', 'HI', 'f' ]
const arr1 = ["a","b","c","d","e","f"];
arr1.splice(1,4,"HI");`enter code here`
console.log(arr1);
output for this code = [ 'b', 'c', 'd', 'e' ]`
What's the difference?
The log is showing what the splice
function returned, which is an array of the deleted items. You seem to want to see the new contents of the original array, which isn't what splice
returns, so you'll have to show it separately.
arr1.splice(1,4,"HI")
console.log(arr1);
Live Example:
const arr1 = ["a","b","c","d","e","f"];
arr1.splice(1,4,"HI");
console.log(arr1);
or if you really want a one-liner, you could abuse the comma operator:
console.log((arr1.splice(1,4,"HI"), arr1)); // But please don't do this ;-)
Live Example:
const arr1 = ["a","b","c","d","e","f"];
console.log((arr1.splice(1,4,"HI"), arr1)); // But please don't do this ;-)