Axel
Axel

Reputation: 5111

Console.log outputs false result [Weird Behaviour]

I have a string where I use split('') to convert into an array. I just want to delete the last item of an array using splice().

However, I get this weird result when I output the array after deleting the last item. It outputs fine when I don't use console.log() in Chrome console. Why is this happening?

(Try this code in the console. It outputs fine)

let strings = 'AA11111';
let splits = strings.split("");
splits.splice(0, splits.length - 1);

(This gives me the false result. [Outputs the last item of the array.])

let strings = 'AA11111';
let splits = strings.split("");
splits.splice(0, splits.length - 1);
console.log(splits);

Upvotes: 1

Views: 113

Answers (4)

Pankaj Kumar
Pankaj Kumar

Reputation: 105

Here what happens is that all elements except the last one are removed. And that is the reason why you get modified array with just 1 element when you console.log. And in the first case it is returning the deleted items.

splits.splice(0, splits.length - 1);
 The modified array with just 1 element when you console.log. And in the first case it is returning the deleted items.

Upvotes: 0

Franklin Pious
Franklin Pious

Reputation: 3848

Here what happens is that all elements except the last one are removed. And that is the reason why you get modified array with just 1 element when you console.log. And in the first case it is returning the deleted items.

splits.splice(0, splits.length - 1);

The first paramenter is the start position of the element to be deleted and second parameter is the number of elements

Upvotes: 1

dwjohnston
dwjohnston

Reputation: 11771

This output makes sense to me.

Reading from the Array.prototype#splice documentation.

Syntax

array.splice(start[, deleteCount[, item1[, item2[, ...]]]])

Parameters

start

Index at which to start changing the array (with origin 0). If greater than the length of the array, actual starting index will be set to the length of the array. If negative, will begin that many elements from the end of the array (with origin -1) and will be set to 0 if absolute value is greater than the length of the array.

deleteCount Optional

An integer indicating the number of old array elements to remove.

You start at index 0, ('A'), you delete length-1 elements (six elements). You're left with the last element. ('1').

If you want to delete the last element I would do this:

let strings = 'AA11111';
let splits = strings.split("");
splits.splice(-1, 1); //starting at the last element, delete one element. 
console.log(splits);

Upvotes: 0

mostafa tourad
mostafa tourad

Reputation: 4388

Array.prototype.splice() used to remove elements from an array and change the content of the original array and it take in the first parameter the index at which you need to delete elements and next parameter the number of elements to delete , so if you need to delete the last element you need to start at arr.length-1 and delete count should be 1 look at the example below

let strings = 'AA11111';
let splits = strings.split("");
splits.splice(splits.length - 1,1);
console.log(splits);

Upvotes: 2

Related Questions