Skip to content

Instantly share code, notes, and snippets.

View pszponder's full-sized avatar

Piotr Szponder pszponder

View GitHub Profile
@pszponder
pszponder / README-Template.md
Created January 19, 2020 02:26 — forked from PurpleBooth/README-Template.md
A template to make good README.md

Project Title

One Paragraph of project description goes here

Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.

Prerequisites

@pszponder
pszponder / global-scope.js
Last active June 20, 2021 03:19
Global Scope
// Define Variables in the JavaScript Global Scope
var vGlobal = "I was defined in the global scope using the var keyword";
let lGlobal = "I was defined in the global scope using the let keyword";
const cGlobal = "I was defined in the global scope using the const keyword";
// Access the variables defined in the global scope from the global scope
console.log("Accessing vGlobal from global scope: ", vGlobal);
console.log("Accessing lGlobal from global scope: ", lGlobal);
console.log("Accessing cGlobal from global scope: ", cGlobal);
function myFunc() {
// Declare a variable inside myFunc's funciton scope
var x = 1;
console.log(x);
if (true) {
var y = 2;
console.log(y);
}
console.log(y);
function myFuncVar() {
// Declare a variable using var inside myFunc's funciton scope
var x = 1;
console.log(x);
if (true) {
// Since var is function scoped and not block scoped,
// we are actually overwriting the value of the x variable
var x = 2;
console.log(x);
// This is an example of block scope,
// typically, the curly brackets will be attached to something like an if, for, while loop
// and not by themselves as seen in this example
// but this makes it easier to illustrate hw block scope works
// Lets call this block scope A
{
// Anything defined inside these braces is block scope
let a = 1;
console.log(a); // 1
// Var does not follow block scoping
{
var a = 1;
console.log(a); // 1
}
console.log(a); // 1
{
{
// Define variable x and initialize its value to 1
let x = 1;
// Define function that will console.log the value of x
function myFunc() {
console.log(x);
}
// Define function that defines a local variable x = 50 and calls myFunc()
function logVariable() {
// Defining x in the global scope of the file
let x = 0;
// Defining myFunc, everything inside is in it's function scope
function myFunc() {
// In order to set a value for y, JavaScript needs to find the value of x
// Since the value of x is not defined within the funcion scope,
// we have to go to the next scope outside the function (which happens to be the global scope)
// In the global scope, we find x = 0 so y will be initialized with a value of 0 as well
let y = x;
let x = "Hello";
console.log(x);
```js
// Lines 1 and 2 are equivalent to stating:
// let x = "Hello";
let x; // Line 1
x = "Hello"; // Line 2
console.log(x); // Line 3
```