Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,008 Points

Global variable is a variable accessible by everything.

That means any function has access to it. Anywhere.