Skip to content

Instantly share code, notes, and snippets.

@kevinchisholm
Last active March 3, 2020 14:03
Show Gist options
  • Save kevinchisholm/a744eafe5df2b24a1943205ff46fe241 to your computer and use it in GitHub Desktop.
Save kevinchisholm/a744eafe5df2b24a1943205ff46fe241 to your computer and use it in GitHub Desktop.
// Example 1 - Using a for loop
var i = 0,
records = ['foo', 'bar', 'baz'],
arrayLength = records.length;
for (; i < arrayLength; i++) {
console.log(`(${i}) The record is: ${records[i]}`);
}
// Example 2 - Using the forEach method
var records = ['foo', 'bar', 'baz'];
// anonymous function
records.forEach(function (record) {
console.log(`(1) The record is: ${record}`);
});
// Example 3 - Using a fat arrow function
records.forEach(record => {
console.log(`(2) The record is: ${record}`);
});
// Example 4 - fat arrow function -> function declaration
records.forEach(record => processRecord(record));
function processRecord (record) {
console.log(`(3) The record is: ${record}`);
}
// Example 5 - Using an anonymous function
records.forEach(function (record, index, originalArray, thisArg) {
console.log(`(1) The record is: ${record}`);
});
// Example 6 - fat arrow function
records.forEach((record, index, originalArray) => {
console.log(`(2) The record is: ${record}`);
});
// Example 7 - fat arrow function -> function declaration
records.forEach((record, index, originalArray) => processRecord(record, index, originalArray, ));
function processRecord (record, index, originalArray) {
console.log(`(3) The record is: ${record}`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment