CSCI 3403 - Web Programming | Week 11, Day 22
Name: _________________________________ Date: _____________
Problems with var:
Fill in the characteristics of each declaration type:
Feature | var | let | const |
---|---|---|---|
Scope | ___________ | ___________ | ___________ |
Can be reassigned? | ___________ | ___________ | ___________ |
Can be redeclared? | ___________ | ___________ | ___________ |
Hoisted? | ___________ | ___________ | ___________ |
Initialized when hoisted? | ___________ | ___________ | ___________ |
var is ___________-scoped or ___________-scoped.
Complete the hoisted version:
Original Code:
console.log(greeter); var greeter = "say hello";
How JavaScript interprets it:
_____________________ console.log(greeter); // ___________ _____________________
What happens in this code? Fill in the output:
Why? _________________________________________
Fill in what happens with each declaration:
Scenario | With var | With let |
---|---|---|
Redeclare in same scope | ___________ | ___________ |
Update/reassign value | ___________ | ___________ |
Declare in different scopes | ___________ | ___________ |
Definition: _________________________________________
_________________________________________
Important Concept: With const, the ___________ is constant, not the ___________!
Choose the best declaration (var/let/const) for each scenario:
Scenario | Best Choice | Why? |
---|---|---|
Configuration values (API_URL) | _______ | _____________________ |
Loop counter (i) | _______ | _____________________ |
DOM element reference | _______ | _____________________ |
User login status | _______ | _____________________ |
Mathematical constants | _______ | _____________________ |
Original Code (using var):
Your Refactored Version:
Use _________ by default, _________ when you need to reassign, and never _________!
Use this space for any additional notes, questions, or examples from class: