Skip to content

Instantly share code, notes, and snippets.

@draffauf
Created January 5, 2020 18:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save draffauf/8b67e4a88fbe962743258c52854cae55 to your computer and use it in GitHub Desktop.
Save draffauf/8b67e4a88fbe962743258c52854cae55 to your computer and use it in GitHub Desktop.
JavaScript Fundamentals
//
// Console.log (output)
//
console.log("Basics ---------------------------------------");
//
// Comments
//
console.log();
console.log();
console.log("Comments ---------------------------------------");
// This is a comment
/*
This is a
multi-line comment
*/
//
// Primitives
//
console.log();
console.log();
console.log("Primitives ---------------------------------------");
// Number
console.log(typeof 4);
// Boolean
console.log(typeof true);
// String
console.log(typeof "David");
// Object
console.log(typeof {});
// Function
console.log(typeof function add() {});
// Null (object)
console.log(typeof null);
// undefined
console.log(typeof undefined);
//
// Variables and assignments
//
console.log();
console.log();
console.log("Assignments ---------------------------------------");
// var
var a;
console.log(a);
// let
let b = 5;
console.log(b);
// const
const c = 10;
console.log(10);
// Pass by value (primitives)
b = a;
console.log(b);
a = 10;
console.log(b);
//
// Arrays
//
console.log();
console.log();
console.log("Arrays ---------------------------------------");
// Create
a = [1, 2, 3];
console.log(a);
// Access
console.log(a[0]);
console.log(a[1]);
console.log(a[2]);
// Out of bounds access
console.log(a[3]);
// Assignment
a[3] = 4;
console.log(a[3]);
//
// Functions
//
console.log();
console.log();
console.log("Functions ---------------------------------------");
// Define a named functions
function hello() { return "Hello"; }
// Call/execute the function
console.log(hello());
// Assigning functions
a = function() { return "Hello"; }
console.log(a);
console.log(a());
// Multi-line functions
a = function() {
let greeting = "Hello";
return greeting;
}
// Anonymous, fat arrow declarations
a = () => { return "Hello"; }
console.log(a());
// Shortened syntax
a = () => "Hello";
console.log(a());
// Parameters
add = (x, y) => x + y;
console.log(add(2, 2));
//
// Lexical Scope
//
console.log();
console.log();
console.log("Lexical Scope ---------------------------------------");
// Functions can access surrounding context
a = 5
b = () => { return a; }
console.log(b());
// Functions can update surrounding context
b = () => {
a = 10;
return a;
}
console.log(b());
console.log(a);
// Functions can define their own scope/context
a = 5;
b = () => {
let a = 10;
return a;
}
console.log(b());
console.log(a);
//
// Objects (Dictionary)
//
console.log();
console.log();
console.log("Objects ---------------------------------------");
// Define an object with a key and value
a = { first: "David" }
console.log(a);
// Access the value
console.log(a.first);
console.log(a["first"]);
// Add a new key and value
a.last = "Raffauf";
console.log(a);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment