Split string and then loop through each item - Javascript String

Javascript examples for String:split

Description

Split string and then loop through each item

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){//w  ww . jav a  2s.  co  m
var str = '2+3+2';
var numbers = str.split('+');
var sum = 0;
for (var i = 0; i < numbers.length; i++) {
    sum += parseInt(numbers[i]);
}
console.log(str + ' = ' + sum);
    }

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials