JavaScript tips


javascript bootstrap hugo

Declare JS variables

Converting between numbers and strings

Manipulating Arrays

Loops in JavaScript

const names = ['Justin','Sarah','Christopher'];
let index = 0;
while (index < names.length) {
	const name = names[index];
	console.log(name);
	index++;
}
const names = ['Justin','Sarah','Christopher'];
for (let index = 0; index < names.length; index++) {
	const name = names[index];
	console.log(name);
}
const names = ['Justin','Sarah','Christopher'];
for (const name of names) {
	console.log(name);
}

Function Syntax

function computePrice(cost, discount){
	let reduction = cost*discount;
	console.log("You saved $"+reduction);
	return cost-reduction;
}

JSON Things to know

	data = bookJSON;
	let parsed = JSON.parse(data);
	console.log(parsed);
	data = myBooksJSON;
	parsed = JSON.parse(data);
	console.log(parsed);
	console.log("Author of 2nd book: "+parsed[1].author);