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