FAQ: Variables - Review Variables

This community-built FAQ covers the “Review Variables” exercise from the lesson "Variables ".

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

Web Development

Introduction To JavaScript

FAQs on the exercise Review Variables

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!

5 posts were split to a new topic: What is the difference between let and var when declaring and what about reassigning?

22 posts were split to a new topic: How to use typeof when concatenating two data types?

Why is the output here not null but undefined ? The tutotial states null is also a datatype.

let temp = null ;
console.log(typeof temp);

null is not a datatype, but an object.

 > let temp = null
<- undefined
 > typeof temp
<- "object"
 > console.log(typeof temp)
   object
<- undefined

Ignore the undefined.

1 Like

2 posts were merged into an existing topic: What is the difference between let and var when declaring and what about reassigning?

Hello! I feel I’m failing to understand a concept of variables. That is the idea that you could somehow change what’s inside a variable using another variable using the let. EX:

let mass = 1;
const earthGravity = mass * 6;
console.log('Earth has a gravity of ' + earthGravity + '.');
//Output: Earth has a gravity of 6.
mass = 2;
console.log("If you double Earth's mass it will have a gravity of " + earthGravity + '.');
//Output: Earth has a gravity of 6. No change.

I realize now that this is a backwards way to do things when i could just use the mathematical assignment operators, but the question still stands as to why changing mass to 2 changes nothing inside earthGravity variable. Is it because I’ve placed a variable within a variable? Is that poor programing? I’m at a loss.

It has to do with earthGravity having a persistent binding to the original value, 6, which cannot be changed. mass may have changed, but it will have no effect on earthGravity.

1 Like

OHHH i think i get it. The variable is originaly 1 * 6, so earthGravity IS 6, changing the mass doesn’t somehow make the framework re-read the variable as it’s already been declared as 1*6. Defiantly gonna stick with Mathematical operators when trying to manipulate variables.

Thank you for the help I really appreciate it!

1 Like

Exactly, especially since the value is a singular number, akin to primitive.

const declares an immutable binding on its defined value. If the value is an object or array, the binding is to the object, but not its properties or elements.

const a = []

We can add and remove or change (mutate) the contents of the array but cannot change its type or assign a new array.

const b = {}

Likewise, we can add and remove or change properties of the object but not its type, nor assign a new object.

1 Like

What is a template literal? Please use basic definitions wherever possible. I do know the definition of ‘literal’, but i’m finding it difficult to wrap my mind around the definition of ‘template literal’ (of which i also understand that it is also synonymous to ‘string literal’. Correct me if i am wrong). I do understand that they are alternatives for concatenation. Please help.

Have you checked the documentation?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

The simplest explanation is that this is a much nicer way (IMO) to insert variables into strings.

1 Like

How do I cut short the the length of my coding as shown in the line of codes I’ve been experimenting with as seen here?

let test1;
let test2;
let test3;
let test4;
let test5;
test1 = 'example';
test2 = 'usage';
test3 = 'me';
test4 = 'I';
test5 = `The reason ${test4} am using this ${test1} is for ${test3} to fully understand the ${test2} of this syntax.`;`

console.log(test5); // prints 'The reason I am using this example is for me to fully understand the usage of this syntax.'
test3 = 'her'; // I've now declared and changed the properties of test3 to her.
console.log(test5); //in this log, test3 still prints as 'me' even after I've change it to 'her'.

after trying a few times, I’ve found out that the properties of test3 changes to ‘her’ after I’ve copied and pasted test5 below test3 = ‘her’;. As seen in the example below.

let test1;
let test2;
let test3;
let test4;
let test5;
test1 = 'example';
test2 = 'usage';
test3 = 'me';
test4 = 'I';
test5 = `The reason ${test4} am using this ${test1} is for ${test3} to fully understand the ${test2} of this syntax.`;`

console.log(test5); // test3 prints as 'me'.
test3 = 'her';
test5 = `The reason ${test4} am using this ${test1} is for ${test3} to fully understand the ${test2} of this syntax.`;`

console.log(test5); // here test3 prints as 'her'.

I’m confused as to why it only prints ‘her’ after I’ve copied the exact same string interpolation in test5 on a new line after test3 = ‘her’;? Why doesn’t the latter changes in properties of the variable overwrites the previous properties of the variables? yet it only works after I’ve “technically” changed the properties of test5 by “remaining the same”? What can I do for it to print the latter changes of variables without having to re-paste the same string interpolation in test5 for the changes to take effect?

strings are immutable. So when declaring test5, the the strings from the other test variable (1 till 4) are a copy and a new string is constructed.

what you want to do, would involve pointers. Which is not possible or really difficult in python

  1. Does that mean that the strings from the initial test variables (1 till 4) including the string interpolation of test5 had became an independent construct after test 5 was declared? So that any latter changes (test3 = 'her') wouldn’t alter the string interpolations in test5, as the initial declaration of test5 is already a construct with the immutable strings from the other test variables?

  2. If it has become a construct, why would declaring test5 again after changing the properties of the test variables as shown in my previous post change the output of console.log(test5), yet keeping the other test variables the same rather than being “undefined” as the new declaration should be a new construct in itself.

  3. if the construct would recognize the prior strings from the previous test variables, why does it not recognize any latter changes until test5(unchanged) is declared again?

  1. Given this is the behavior we are seeing/observing, I think the question can answer itself?

  2. variable are just labels. A way for us developers to access the string at a later point

when you create/concatenate a new string (test5) all the strings in the other variables are copied and used in the new construction.

you re-assign the variable, this will remove the old string stored in the variable, and assign it a new value.

I see! Thank you for the explanation, now it is much clearer.