You are modifying the variable prevalue (shared by the loop condition) inside the loop where you check for length each time as a loop condition. And each time it adds something to it inside the loop so it goes on an on. It used to be an array and after adding the string to it, it coerses to string from array and then on wards it checks for the length of the string and you keep on appending to it, it goes on and on.
Try:
$(function(){
$('#sum').keyup(function(){
var prevalue=$('#sum').val().split(","), sum = 0;
for (var i=0;i<prevalue.length;i++){
sum += parseInt(prevalue[i], 10) || 0; //<--- Use a parseInt to cast it or use parseFloat
}
$('h1').html(sum); //<-- move it to out of the loop
});
});
Fiddle
Your code:
$(function () {
$('#sum').keyup(function () {
var prevalue = $('#sum').val().split(","); //<-- Iteration 1 prevalue is an array
for (i = 0; i < prevalue.length; i++) { //iteration1 : it looks for array.length
prevalue += prevalue[i]; //Changes the variable shared by the loop to string from array and string also has its length. And next time onwards it adds to itself a char from the string and length increases and loop goes infinitely.
$('h1').html(prevalue); //<-- Doesn't make any sense here.
}
});
});