Two things:
1) The problem you have noticed is due to your loop:
for(var i=0; i <= new_str.length; i++){
Indexes run from 0 to N - 1, where N is the number of element in the array. You are trying to access 0 to N inclusive, so one of them does not have a value and you get an error.
2) Why do it like that? Just have a MaxValue integer, preset it to the first index value, and compare it to the current length each time round the loop. Why waste time and memory building a array you only use once?