Skip to content

Commit 00b6f21

Browse files
committed
Shorthand coding technique.. 🗽🚀
1 parent 100e4d6 commit 00b6f21

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

js-coding-technique/for-loop.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Longhand:
2+
3+
const fruits = ['banana', 'mango', 'avocado', 'apple'];
4+
for (let i = 0; i < fruits.length; i++) {
5+
// something
6+
}
7+
8+
// Shorthand:
9+
10+
for (let fruit of fruits) {
11+
// something
12+
}
13+
14+
// to access just the index
15+
// for (let index in fruits)
16+
17+
// example 1
18+
const obj = { continent: 'Asia', country: 'Bangladesh', city: 'Dhaka' };
19+
for (let key in obj) console.log(key); // output: continent, country, city
20+
21+
// Shorthand for Array.forEach:
22+
function consoleArrayElements(element, index, array) {
23+
console.log(`arr[ ${index} ] = ${element}`);
24+
}
25+
26+
let arr = [2, 4, 5, 8, 9];
27+
arr.forEach(consoleArrayElements);
28+
// arr[0] = 2
29+
// arr[0] = 4
30+
// arr[1] = 5
31+
// arr[1] = 8
32+
// arr[2] = 9
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Longhand:
2+
3+
let databaseHost;
4+
if (process.env.DB_HOST) {
5+
databaseHost = process.env.DB_HOST;
6+
} else {
7+
databaseHost = 'localhost';
8+
}
9+
10+
// Shorthand:
11+
12+
const databaseHost = process.env.DB_HOST || 'localhost';

0 commit comments

Comments
 (0)