At the end of this class, you will be able to:
var, let, and const keywords
In Finder:
firstname folder (example: wilson-espina)Homework-1 folder from last Wednesday into your firstname folder.
In Finder:
jsd-9-homework foldergit add .git commit -m "submitting Homework 1"git push origin master
In Browser:
jsd-9-homework on git.generalassemb.ly


pull and push code from Github without having to enter credentials each time! 🥳 🎉
Tasks
Timing
6 mins |
|
Open Terminal:
jsd-9-resources foldergit remote set-url origin and paste the copied text
git remote set-url origin git@hostname:USERNAME/jsd-9-resources.git
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)
wilson-espina/jsd-9-resources repo to your computer:cd to the Documents/JSD/jsd-9-resources directorygit pull origin master and press returnDocuments/JSD/jsd-9-resources/05-scope-variablesWhen 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
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 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
Key Objective
Type of Exercise
Location
Timing
5 mins |
|
ECMAScript provides the rules, details, and guidelines that a scripting language must observe to be considered ECMAScript compliant
let broadcasters = ['BBC', 'ITV', 'SKY'];
const tax = 0.2;
var can be avoided outside of working on legacy code
var broadcasters = ['BBC', 'ITV', 'SKY'];
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| Keyword | Scope | Can Be Reassigned |
|---|---|---|
var | Function scope | Yes |
let | Block scope | Yes |
const | Block scope | No |
Open up: 2-scope-codealong
Key Objective
Type of Exercise
Location
Timing
8 mins |
|
var are lifted to the top of their function/local scope.
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;
}
var are hoisted, but not their values.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;
}
let or const are not hoisted.Code written by developer
foo()
function foo() {
console.log("Hi!");
}
Code as intrerpreted by parser
function foo() {
console.log("Hi!");
}
foo();
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!");
}
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!");
}
| Keyword | Scope | Can Be Reassigned | Hoisting |
|---|---|---|---|
var | Function scope | Yes | Yes |
let | Block scope | Yes | No |
const | Block scope | No | No |
Open up: 2-scope-codealong
Objects & JSON
(Lesson #05)