Skip to content

Instantly share code, notes, and snippets.

@Nathan-Nesbitt
Nathan-Nesbitt / BetterSwitchStatement.js
Created May 9, 2020 21:35
Better Switch Statement Example
var fruit = "Apple";
switch(fruit) {
case "Banana":
console.log("Loads of K");
break;
case "Orange":
console.log("Zesty");
break;
case "Apple":
const age = 21;
switch(age) {
case 0:
console.log("You cannot drink or drive");
break;
case 1:
console.log("You cannot drink or drive");
break;
case 2:
@Nathan-Nesbitt
Nathan-Nesbitt / IfElse.js
Created May 9, 2020 20:56
If/Else Statements
const age = 21;
// If someone is less than 16
if(age < 16) {
console.log("You cannot drink or drive");
}
// If someone is less than 19 but greater than 16
else if(age < 19) {
console.log("You cannot drink but you can drive");
}
@Nathan-Nesbitt
Nathan-Nesbitt / ArithmeticOperators.js
Created May 7, 2020 18:44
List of Basic Operators
var a = 1;
var b = 2;
// Addition Operator
console.log(a + b);
// Subtraction Operator //
console.log(a - b);
// Multiplication Operator //
@Nathan-Nesbitt
Nathan-Nesbitt / BlockScopeVariables.js
Created May 7, 2020 18:27
Example of Block Scoped Variables
// Create variable for only the outer block //
let i = 22;
// Create and loop over inner block variable //
for(let i = 0; i < 10; i++) {
console.log(i);
}
// Print outer block variable //
console.log(i);
@Nathan-Nesbitt
Nathan-Nesbitt / NeedConstant.js
Created May 7, 2020 18:22
Example of Need of Constant
// Assume this creates a connection to a DB //
var myDatabaseConnection = new CreateConnectionToDatabase();
// Oops we accidentally overwrote the database connection //
myDatabaseConnection = 10;
// This would throw an error saying WTF?! how do I run this?
myDatabaseConnection.query("SELECT * FROM school");
@Nathan-Nesbitt
Nathan-Nesbitt / Constants.js
Created May 7, 2020 18:19
Constant Variables Example
// Creates a constant variable //
const myConstant = 10;
// Tries to reassign the variable but will
// throw TypeError: invalid assignment to const `myVariable'
myConstant = 11;
// Prints 10 //
console.log(myConstant);
@Nathan-Nesbitt
Nathan-Nesbitt / ReassignVariable.js
Created May 7, 2020 17:59
Reassigning A Variable
var greeting = "Hello World!";
console.log(greeting);
// Reassigns the variable //
greeting = "Goodbye!"
console.log(greeting);
@Nathan-Nesbitt
Nathan-Nesbitt / SimpleVariable.js
Created May 7, 2020 17:31
Simple Example of a Variable
var greeting = "Hello World!";
console.log(greeting);
@Nathan-Nesbitt
Nathan-Nesbitt / Embedded.html
Created May 7, 2020 17:01
Embedded JavaScript Example
<!DOCTYPE html>
<html lang="en">
<body>
<script>
// Your JS code can go here! //
</script>
</body>
</html>