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 JavaScript Basics (Retired) Storing and Tracking Information with Variables Working with Strings and Finding Help

Variable vs Object

So, I was wondering what the difference between a variable and an object is. Is it that a variable a type of object, but an object isn't always a variable?

Also, do variables only exist if the author puts "var" in front of something? Or is console.log a variable? If so, how could it be a variable if the author never created it?

Are all of the above objects? I had to watch this video several times, and I'm still a little confused on the terminology.

Thanks!!

2 Answers

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Great question Jesse,

First, here's some documentation to help get you started (MDN is a great resource):

  1. Data Types
  2. Object

In the grand scheme of things, I think of all variables as objects, actually I consider everything an object...

But, in your case, seeing that you are in the Javascript basics class. Let's narrow down the scope and consider just user defined variables (system Javascript has system defined variables, objects and probably a bunch of other stuff).

Therefore in your individual scripts, the only variables are the ones you (the author) declares with var (and let or const, the updated new and improved replacements to var). And, objects are 1 of seven types of variables you can define.

So, play around with your own scripts, with stuff like the following:

// User defined variables and objects

// the 7 data types
let booleanVariable = true;
console.log(booleanVariable);
let nullVariable = null;
console.log(nullVariable);
let undefinedVariable = undefined;
console.log(undefinedVariable);
let numberVariable = 42;
console.log(numberVariable);
let stringVariable = 'A string in here';
console.log(stringVariable);
let symbolVariable = Symbol('Silly Symbol');
console.log(symbolVariable);
let objectVariable = {
    name : 'Funny',
    type : 'Object'
};
console.log(objectVariable);

// output
true
null
undefined
42
A string in here
Symbol(Silly Symbol)
{ name: 'Funny', type: 'Object' }
Junet Ismail
Junet Ismail
3,563 Points

The best way to look at it within object oriented programming terms is an object is an entity and a variable is a piece of data that could potentially relate to a an object. In the simplest of terms an example would be an object such as a person and variable could be pieces of data that relate to that person such as their name, date of birth height etc.. Variables can be independent of objects and therefore be used to store any piece of information anywhere within a programme however objects cannot be independent of variables as they are required to define the object.