//when str=eight
let newStr = str.split(""); // [ 'e', 'i', 'g', 'h', 't' ]
newStr.push('w','a','y') //10
//expected -> [ 'e', 'i', 'g', 'h', 't' , 'w', 'a', 'y']
After splitting the string, I want to push way to the end of the array. Instead of getting an array, I’m getting a number.
Why is it happening like this? This is the first time I have seen something like this. How do I prevent such things to happen in the future?
The push method returns the new length of the array you pushed to. Just make a reference to newStr
again if you want to get the array again.
1 Like
let str = "eight"
let newStr = str.split(""); // [ 'e', 'i', 'g', 'h', 't' ]
console.log(newStr)
newStr.push('w','a','y') //10
console.log(newStr)
Ran the same function and got what you expected. Maybe there was something wrong you did.
["e", "i", "g", "h", "t"]
["e", "i", "g", "h", "t", "w", "a", "y"]
1 Like
Can you please include it in a code? It would be much easier to understand
I’m used node for testing
The only time you will get 10 is if you do the following:
const count = newStr.push('w' , 'a', 'y')
console.log(count)//8
Play with this on MDN and learn how it works
Click here
You have pushed it into the array, the code is doing exactly what you expect it to do
1 Like
system
Closed
8
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.