Coding is an art, so why not make it as clean as possible? In this video, we'll cover 3 techniques we'd like to implement to make my JavaScript code more readable and extensible.

Source Code

01-avoid-else-statements.jscontent_copy

function getThemeColors() {
	let currentHour = new Date().getHours();
	let textColor;
	let backgroundColor;

	if (currentHour > 20 && currentHour < 7) {
		textColor = "#ffffff";
		backgroundColor = "#222222";
	} else {
		textColor = "#222222";
		backgroundColor = "#ffffff";
	}

	return {
		textColor: textColor,
		backgroundColor: backgroundColor
	};
}

02-skip-loop-iterations.jscontent_copy

const numbers = [4, 9, 2, 7, 11];

// Find factors of 12
for (const n of numbers) {
	if (12 % n === 0) {
		console.log(`${n} is a factor of 12!`);
	}
}

03-array-map-for-transformations.jscontent_copy

const names = ["dom", "peter", "aleisha", "sam"];
const capitalized = [];

for (const n of names) {
	const newName = n[0].toUpperCase() + n.slice(1);

	capitalized.push(newName);
}

View the Starting Code:
https://dcode.domenade.com/tutorials/3-ways-write-clean-code-in-javascript 

Subscribe: https://www.youtube.com/@dcode-software/featured 

#javascript 

Write Clean JavaScript Code | 3 Ways to Write Clean Code in JavaScript
4 Likes36.85 GEEK