Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Node.js Basics (2014) Building a Command Line Application Perfecting: Getting Multiple Profiles

what is the global variable

that can be used to see arguments, and resource usage.

2 Answers

Ashlee Brown
Ashlee Brown
8,045 Points

A global variable is one that is declared outside the scope of a function, and therefore able to be used all over the application, or globally. Here's an example:

var globalVar = 'apple';
console.log(globalVar);  //prints 'apple'

function learnScope(){
  var localVar = 'orange';
  console.log(localVar)  //prints 'orange'
  console.log(globalVar); //prints 'apple'
console.log(localVar); //throws a reference error that localVar is not defined

In this example, globalVar is in fact global because it was defined outside the function, and can be used again either on its own or within the context of another function. localVar on the other hand, is defined only in the context of the learnScope function, so its value only exists within the function it was called.

jason chan
jason chan
31,009 Points

Global variable is a variable accessible by everything.

That means any function has access to it. Anywhere.