FAQ: Variables - Introduction to Variables

This community-built FAQ covers the “Introduction to Variables” exercise from the lesson “Variables”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn How To Code

FAQs on the exercise Introduction to Variables

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

1 Like

what are values i went threw the lesson and i still dont understand

5 Likes

I filled the variables in order for each row, box by box and couldn’t progress. There’s a mistake in the progress of this lesson.

3 Likes

6 Likes

how did you done exercise let you help me

Enter the information given, into the input boxes. @mtf has the picture of how they should be entered above, where you’ve asked for the help.

See how his picture has “grass rocks grass - forest rocks forest - rocks grass rocks” vertically, down the left-hand side? This is how you should enter your values, to obtain the output as seen on the majority of his picture.

1 Like

So I’m a pretty visual learner, my brain can sometimes glaze over words it’s not used to seeing often, “Values” and "Variables’ for example. Can anyone think of a an everyday example that could help give me a visual representation between the two?

When we hang a tag on something, that’s a variable. The something the tag is hanging from is the value.

Values are objects, variables are references.

a = 42

42 is primitive, wherever it lives in memory. The primitive is given an object wrapper according to its detected type before assignment to the reference; the variable, a references a ‘number’ type object in JavaScript.

4 Likes

So…

Value- I’m looking at a candle but it has no name or designation yet so it is only an object at this point (a)

Variable- Then someone slaps a label on it that says “candle”. It went from just an object and is now known to be a candle (42)

object(a) = candle(42)

and from here on out, the object will be known as candle anytime “object” is entered, or until redefined.

Right?

2 Likes

Pretty much, but,

candle_a = object_42

The object is always on the right side of the assignment, and the variable always on the left.

We can give a variable a new assignment any time unless it is defined in JS as a const. The key to remember is that the variable is not the object, the object is.

An object can have multiple references.

a = 42
b = 42

Both variables refer to the same object as long as neither of them is given a new object to reference. The value is the same for both.

4 Likes

It keeps saying “Connecting to codecadamy” But it never loads it and my internet is just fine…

what is an assignment

What is the difference between an object an a variable? Can we say that an object is nothing unless it is given a value? and so the value makes the object a variable?

Assignment is when a value (an object) is given a reference, something we can refer to it with.

a = 42

42 is a value, a number object; a is the variable, a label, essentially. The single equals sign is the assignment operator.

A variable is not an object, per se, unless we consider that it is a string object in memory. What it refers to is the object.

Variables can be reassigned, meaning we can have them reference a new object. Once reassigned, the old reference disappears.

a = a * 2

Above we have reassigned a with a new value, relative to its original value. We gave it a new state derived from its old state. Now a will reference the value, 84. The old value is gone.

Often we give our variables names that describe the object they refer to.

height = 10
width = 5
length = 15

From those names we can piece together the concepts and deduce that we are describing a rectangular solid such as a block of wood or a brick. By themselves they don’t have any real meaning, but together they mean something.

Advanced topic

You will learn about data structures in the upcoming lessons, one of which we call on object literal in JavaScript. Don’t worry if this is all foreign to you. Pretty soon you’ll be working with this in all your projects. For now just have a look…

block = {
    height: 10,
    width: 5,
    length: 15
}

This lets us group all the known properties of the block object so their true meaning is made more apparent. The object syntax even lets us define methods that can help us derive other concepts such as volume and surface area, so the block becomes even more tangible.

block = {
    height: 10,
    width: 5,
    length: 15,
    volume () {
        return this.height * this.width * this.length;
    },
    surfaceArea () {
        return 2 * (this.height * this.width + this.height * this.length + this.width * this.length);
    }
}
console.log(block.volume());         //  750 (cubic units)
console.log(block.surfaceArea());    //  550 (square units)
3 Likes
,
  perimeter (a, b) {
    return 2 * (this[a] + this[b])
  },
  diagonal (a, b) {
    return (this[a] ** 2 + this[b] ** 2) ** 0.5
  }

What are the parameters, a and b in the above?

So in this first exercise the values are the different terrains and the variables are the numbers (one, two, three etc).

Am I right?

Yes, your observation is correct.

How does writing “grass” or “rocks” tell the computer to put rocks in the square shouldn’t I have to describe what rocks are to the computer first?

We will have given enough information for the computer to be able to act upon our instructions. Given the term grass may simply mean look up the name in a list of image files and load the one with the name, grass.jpg (png, svg, gif, bmp, etc. are all valid image extensions). The computer cannot act on its own and requires explicit data and instruction from the programmer.

the lesson itself is one of the first in the intro to coding course, so for the new coders (like myself) all we do is tell the computer what to do but who ever programed the code (did I use “programed” right or should that be coded the code?) had already told the computer what “grass” and “forest” means?