Node Global Lab

Objectives

  1. Use REPL
  2. Access global
  3. Use explicit vs. implicit declarations

Introduction

Scoping is about defining and accessing variables/objects/functions. Here's a small example which can illustrate how dangerous it can be to not understand scope: our program uses a variable named list and module A uses a variable named list. When we import module A, the original list of the program is lost!

Understanding global and scoping will give you a good foundation for future development.

In this lab, we're going to be using the Node REPL to create several bank accounts and users in different scopes.

Instructions

  1. Enter Node REPL
  2. Print the global object
  3. Print the process object (global.process)
  4. Create a global variable user explicitly with global.user = {admin: false}
  5. Create a global variable implicitly with var account = {balance: 1000}
  6. Open a new REPL and check for values of user and account (ReferenceError means the variable is undefined)
  7. Go back to the first REPL and print user and account with global.user and global.account
  8. Delete user with delete user (true) and print it again. You should get ReferenceError because you deleted user.
  9. Delete account with delete account (false) and print it again ({balance: 1000})
  10. Exit REPL
  11. Contemplate on the fact that user and account reacted differently to the delete operator. Read the extra info if you forgot how delete works (it's JavaScript and not exclusive to Node).

Extra Info

To understand the discrepancy, you need to know how delete works and what was the difference between user and account in the ways they were created (explicit with global vs. implicit with var ).

View node-global-lab on Learn.co and start learning to code for free.

View Node Global Lab on Learn.co and start learning to code for free.

Unlock your future in tech
Learn to code.

Learn about Flatiron School's Mission

With a new take on education that falls somewhere between self-taught prodigy and four-year computer science degree, the Flatiron School promises to turn students with little programming experience into developers.

In the six months since the Manhattan coding school was acquired by WeWork, it has spawned locations in Washington, D.C., Brooklyn, and London. Now, WeWork is opening a fourth Flatiron School location, this time in Houston.

Adam Enbar, Flatiron School's cofounder, believes now is the time to grow. "How the world is changing has impacted working and learning in very similar ways. We think education fundamentally is about one thing: enabling people to pursue a better life."

Learn. Love. Code.
Students come to Flatiron School to change their lives. Join our driven community of career-changers and master the skills you need to become a software engineer or a data scientist.
Find Us