Skip to content

Instantly share code, notes, and snippets.

@kotobuki
Last active October 7, 2018 01:20
Show Gist options
  • Save kotobuki/78949ccc9ee096916bd68fa9373c6241 to your computer and use it in GitHub Desktop.
Save kotobuki/78949ccc9ee096916bd68fa9373c6241 to your computer and use it in GitHub Desktop.
Getting Started with JavaScript
// A "hello world" example
console.log("Hello world!");
// Operators
console.log(600 + 150);
console.log(2018 - 1970);
console.log(500 * 1.08);
console.log(1480 / 2);
// Variables
let price = 100;
let taxRate = 0.08;
let priceIncludingTax = price * (1 + taxRate);
console.log(priceIncludingTax);
// Arrays
let dayOfTheWeek = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
// Print the first (0th) element
console.log(dayOfTheWeek[0]);
// Print the length of the array
console.log(dayOfTheWeek.length);
// Conditionals
const age = 16;
const adultAge = 20;
if (age < adultAge) {
console.log("Child");
} else {
console.log("Adult");
}
// Loops
// A counter, an exit condition, an iterator
for (let i = 0; i < dayOfTheWeek.length; i++) {
console.log(dayOfTheWeek[i]);
}
// Functions
// The name of the function, a list of parameters, the JavaScript statements that define the function
function calculateTaxIncludedPrice(price) {
const taxRate = 0.05;
return Math.floor(price * (1 + taxRate));
}
console.log(calculateTaxIncludedPrice(980));
// Property accessors
const player1 = { name: "Parzival", realName: "Wade Owen Watts", age: 18 };
console.log(player1["name"]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment