Javascript Looping Constructs

JavaScript Looping Constructs

These are some looping constructs that I seem to use frequently....

USAGE: Copy and paste the code samples below into the Javascript console in your browser, then simply hit return on your keyboard to see the code in action.

//standard looping code
let numbers = [1, 2, 3, 4, 5];

function sumUp() {
  let result = 0;
  let toAdd=[];
  for (let i = 0; i < numbers.length; i++) {
    result += toAdd[i];
  }
  return result;
}
console.log(sumUp(numbers));

//for-of-loop
let testResults = [1.23, 3.12, 5.33];
for (let testResult of testResults) {
  console.log(testResult);
};


Link your website to this page! Copy and paste the URL below:
http://www.cfsnap.com/javascript/javascript-looping-constructs/
Copyright 2019. All Rights Reserved.