when i add .toastring() operator in array of javaScript the length of array increases, Why? [closed]

when i add.toString() operator in Array of javascript the length of array was increased why?

i wil trying to change Array into string.
before executing .toString() operator the length of my array is 11 but after to.string length was increased to 53

  • 1

    Can you share some code to show us what you’re doing exactly?

    – 

  • const marks= [1.4,44,33,41,’rizwan’,’zain’]; // console.log(typeof marks); console.log(marks.length); // console.log(marks[0]); // console.log(marks[1]); // console.log(marks[2]); // console.log(marks[3]); // console.log(marks[4]); // console.log(marks[5]); const name=[‘rizwan’,’zain’,’ali’,’saad’,’mehmood’]; const arr= marks.concat(name); console.log(arr) console.log(arr.length) arr.sort() console.log(arr) let a= arr.toString() // let b= arr.join(“-“); console.log(arr) console.log(a.length)

    – 

  • 2

    Can you edit your question please so it’s formatted? Its a bit hard to read as a comment

    – 




  • 3

    Are you calculating the length of the result of array.toString()? If so, then you’re calculating the length of the resulting string, you have a string now, not an array, and so you’re counting every character in the output. E.g. [1, 2, 3].toString() will give "1,2,3", which has a length of 5, even though the array’s length was 3.

    – 

  • 2

    Given that a string representation of an array also contains commas, and any given element could have a multiple character string representation (e.g. ‘rizwan’ is six), how could it not be longer?! What exactly did you expect here?

    – 




a.length is telling you the length of the string not the array.
The string also has commas meaning that it’ll always have an increased length than the array. Also if you are doing this to display it on a website try array.join(‘, ‘)

Leave a Comment