General Assembly

Scope & Variables

Wilson Espina

Review

What did we do last lesson?

Scope & Variables

Learning Objective

Learning Objectives

At the end of this class, you will be able to:

  • Understand the homework submission process
  • Connect to Github with SSH
  • Understand how scope works
  • Determine the scope of local and global variables
  • Describe what hoisting does

Scope & Variables

Agenda

  • Submitting Homework
  • Using SSH
  • Variable scope
  • The var, let, and const keywords
  • Hoisting

Scope & Variables

Exit Ticket Questions

  1. Can the pace be a faster?
  2. Can we get more applicable content?

Scope & Variables

Homework

Scope & Variables

Homework Repo Recap

Pull Request diagram 1

Scope & Variables

Homework Repo Recap

Pull Request diagram 2

Scope & Variables

Push Code to Remote

Pull Request diagram 3

Scope & Variables

Create a Pull Request

Pull Request diagram 4

Scope & Variables

Submitting Homework

Scope & Variables

Submit Homework: Step 1

These are the steps you'll do to submit every Homework assignment:

In Finder:

  • Navigate to firstname folder (example: wilson-espina)
  • Copy your completed Homework-1 folder from last Wednesday into your firstname folder.

Scope & Variables

Submit Homework: Step 1 Illustration

Pull Request copy folders

Scope & Variables

Submit Homework: Step 2

In Finder:

  • Navigate to jsd-9-homework folder
  • git add .
  • git commit -m "submitting Homework 1"
  • git push origin master

Scope & Variables

Submit Homework: Step 2 Illustration

Pull Request diagram 3

Scope & Variables

Submit Homework: Step 3

In Browser:

  • Go to your fork of jsd-9-homework on git.generalassemb.ly
  • Click Pull RequestPull Request
  • Click Create new pull request Pull Request create new button
  • Click Create pull request Pull Request create pull button

Scope & Variables

Create a Pull Request

Pull Request diagram 4

Scope & Variables

Homework Review

Scope & Variables

Setting up SSH

Scope & Variables

What is SSH?

  • Secure Shell or Secure Socket Shell is a network protocol that gives users a secure way to access a computer over an unsecured network.
  • SSH keys are a way to identify trusted computers, without involving passwords.
SSH

Scope & Variables

SSH Keys

SSH keys
  • SSH Client stores a Private key
  • SSH server stores a Public key
  • There is a negotiation starting from the Client to the Server and back again to prove identities.

Scope & Variables

SSH vs HTTPS on Github

Activity - Set up SSH

Install Icon

Tasks

  • Set up a new SSH key and add it to your Github Enterprise account

Timing

6 mins

  1. Follow the instructions on how to create a new SSH key and add link it to Github:

Scope & Variables

Switching from HTTPS to SSH

Open Terminal:

  • Navigate tojsd-9-resources folder
  • Head to your repo on Github and click 'Clone or Download'
  • Click on 'Use SSH' and copy to the clipboard
  • Type git remote set-url origin and paste the copied text
    
    git remote set-url origin git@hostname:USERNAME/jsd-9-resources.git
    								
  • Verify that the remote URL has changed
    
    git remote -v
    # Verify new remote URL
    origin  git@hostname:USERNAME/jsd-9-resources.git.git (fetch)
    origin  git@hostname:USERNAME/jsd-9-resources.git.git (push)
    								

Scope & Variables

Classroom Resources

  1. Pull changes from the wilson-espina/jsd-9-resources repo to your computer:
    • Open the Terminal
    • cd to the Documents/JSD/jsd-9-resources directory
    • Type git pull origin master and press return
  2. In your editor, open the following folder: Documents/JSD/jsd-9-resources/05-scope-variables

Scope & Variables

Scope

Scope & Variables

Global Scope

When you start writing JavaScript in a document, you are already in the Global scope:


let flavour = 'Strawberry'; // global variable

function pick() {
  console.log(flavour); // Strawberry
}
console.log(flavour); // Strawberry
							
  • A variable declared outside of a function is accessible everywhere, even within functions. Such a variable is said to have global scope.

Scope & Variables

Local Scope - Function Scope

Variables defined inside a function are in the local scope.


let flavour = 'Strawberry';

function pick() {
  let newFlavour = 'Chocolate'; // function scope
}

pick();
console.log(newFlavour); // Throws an error

							
  • A variable declared within a function is not accessible outside of that function. Such a variable is said to have function scope.

Scope & Variables

Local Scope - Block Scope

A variable created with let or const creates local scope within any block, including blocks that are part of loops and conditionals:


let flavour = 'Strawberry';
let name = 'Sarah';

if (flavour === 'Strawberry') {
  let message = 'You chose the right flavour';
  console.log(message + ' ' + name); // You chose the right flavour Sarah
}

console.log(message + ' ' + name); // 'message' is undefined
							
  • This is known as block scope.

Lab - Scope

Exercise

Key Objective

  • Create and call a function that accepts parameters to solve a problem

Type of Exercise

  • Individual

Location

  • starter code > 1-scope-lab

Timing

5 mins

  1. Open the index.html file in your browser, view the console, and examine the error.
  2. Follow the instructions in js > main.js to complete parts A and B.

Scope & Variables

Let, Const, Var and Scope

Scope & Variables

Javascript Specification

ECMAScript provides the rules, details, and guidelines that a scripting language must observe to be considered ECMAScript compliant

  • JavaScript is a language that conforms to the ECMAScript specification
Timeline of JavaScript

Scope & Variables

let

  • Keyword in ECMAScript6 (ES6)
  • Local scope within functions and within any block (including loops and conditionals)
  • 
    let broadcasters = ['BBC', 'ITV', 'SKY'];
    							

Scope & Variables

const

  • Part of ES6
  • Local scope within functions and within any block (including loops and conditionals)
  • Used to declare constants
  • 
    const tax = 0.2;
    							

Scope & Variables

var

  • Original JS keyword for creating variables
  • Only type of local scope it can create is function scope
  • Generally, var can be avoided outside of working on legacy code
  • 
    var broadcasters = ['BBC', 'ITV', 'SKY'];
    							

Scope & Variables

let/const vs var


let x = 1;
if (true) {
  let x = 2;
  console.log(x); // 2
}
console.log(x); // 1
								
  • let & const create local scope within any block

var x = 1;
if (true) {
  var x = 2;
  console.log(x); // 2
}
console.log(x); // 2
								
  • var does not create local scope within a block

Scope & Variables

let, const and var

Keyword Scope Can Be Reassigned
var Function scope

Yes

let Block scope

Yes

const Block scope

No

Code along

Code Along

Open up: 2-scope-codealong

Code Along

Lab - let, var and const

Exercise

Key Objective

  • Determine the scope of local and global variables

Type of Exercise

  • Pairs

Location

  • starter code > 3-let-var-const-lab

Timing

8 mins

  1. Open the index.html file in your browser, view the console, and examine the error.
  2. Follow the instructions in js > main.js to complete parts A and B.

Scope & Variables

Hoisting

Scope & Variables

Hoisting

  • When JavaScript compiles all of your code, all variable declarations using var are lifted to the top of their function/local scope.
  • Hoisting doesn't happen in the code, rather the Compiler.
  • Google Chrome uses V8 Compiler / Interpreter.
Hoisting

Scope & Variables

Hoisting

Code written by developer


function celebrate() {

  console.log("Wahoo!");
  var x = 1;
}
								

Code as intrerpreted by parser


function celebrate() {
  var x; // top of function scope
  console.log("Wahoo!");
  x = 1;
}
								
  • Variable names declared with var are hoisted, but not their values.

Scope & Variables

Hoisting

Code written by developer


function celebrate() {
  console.log("Wahoo!");
  let x = 1;
}
								

Code as intrerpreted by parser


function celebrate() {
  console.log("Wahoo!");
  let x = 1;
}
								
  • Variable names declared with let or const are not hoisted.

Scope & Variables

Hoisting - Function Declarations

Code written by developer


foo()

function foo() {
  console.log("Hi!");
}
								

Code as intrerpreted by parser


function foo() {
  console.log("Hi!");
}

foo();
								
  • Function declarations are hoisted.
  • Your code can call a hoisted function before it has been declared.

Scope & Variables

Hoisting - Function Expressions

Code written by developer


foo()


var foo = function() {
  console.log("Hi!");
}
								

Code as intrerpreted by parser


var foo;

foo(); // error foo is not a function
foo = function() {
  console.log("Hi!");
}
								
  • Function expressions are treated like other variables

Scope & Variables

Hoisting - Function Expressions

Code written by developer


foo()

let foo = function() {
  console.log("Hi!");
}
								

Code as intrerpreted by parser


foo(); // foo is not defined

let foo = function() {
  console.log("Hi!");
}
								
  • Function expressions are treated like other variables

Scope & Variables

let, const and var

Keyword Scope Can Be Reassigned Hoisting
var Function scope

Yes

Yes

let Block scope

Yes

No

const Block scope

No

No

Code along

Code Along

Open up: 2-scope-codealong

Code Along

Quiz - Variables & Scope

Quiz

Scope & Variables

Learning Objective

Learning Objectives - Review

  • Understand the homework submission process
  • Connect to Github with SSH
  • Understand how scope works
  • Determine the scope of local and global variables
  • Describe what hoisting does

Scope & Variables

Lookahead to Next Lesson

Objects & JSON

  • Introduction to objects and methods
  • Real World objects
  • Understand what is JSON
  • Work with JSON-formatted data

Scope & Variables

Q&A

Scope & Variables

Homework

Scope & Variables

Exit Ticket

(Lesson #05)