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

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!

So can I reassign variables using let just one time or several times?

I think it is a some kind of very confusing mistake there:

The var keyword is used in pre-ES6 versions of JS.
let is the preferred way to declare a variable when it can be reassigned,

Actually it is not true about the difference between let and var.
There’s a difference in scope, but not in reassignment. Reassigned could be variables declared by any non-const keyword. That’s the definition of variable. It can be reassigned by using assignment operator.

If they meant redeclared, it works only in strict mode and is just the polar opposite.

Redeclaration:
Assuming strict mode, var will let you re-declare the same variable in the same scope. On the other hand, let will not:

‘use strict’;
let me = ‘foo’;
let me = ‘bar’; // SyntaxError: Identifier ‘me’ has already been declared
‘use strict’;
var me = ‘foo’;
var me = ‘bar’; // No problem, me is replaced.

1 Like

Just completed the Variable assignment, and decided to switch to the forum for some additional questions.

So, the keyword let is not replacing var, right? You are saying that even though the ‘purpose’ is the same, it alters when applying ‘use strict’ directive? And, if it’s not too much to ask (newbie question), when would I have to, or when should I, use the ‘use strict’ directive?

  • Use typeof to find the data type of the resulting value when you concatenate variables containing two different data types. any example on how to do this
2 Likes

I did the following. Please correct any errors.

let string ='moon';

let number =50;

string += ' landing';

number +=' years';

console.log(typeof `It's been ${number} since the ${string}.`);

WITHOUT typeof it logged:

It’s been 50 years since the moon landing.

WITH typeof it logged:

string

Even though my code contained a variable whose value was a number (50), I’m assuming typeof threw string to the console because the whole thing was a sentence, aka a string.

5 Likes

Not sure how to follow this suggestion:

Use typeof to find the data type of the resulting value when you concatenate variables containing two different data types.

We’ve used concatenation to log something to the console. We now have to assign the result to a variable and then use the ‘typeof’ operator on that variable … is that right??

1 Like
console.log(k);
const k="figure";

Error: Reference Error k is not defined

console.log(k);
let k="figure";

Error: Reference Error k is not defined

console.log(k);
var k="figure";

O/p:Undefined

Explain this phenomenon what’s the difference using let & const throwing error .using var code is executed .

I found an article:

https://www.geeksforgeeks.org/difference-between-var-and-let-in-javascript/

which shows an example similar to yours, but doesn’t explain it perfectly.

let and const where introduced in an update (es6, also know as emcascript 2015), and are an improvement.

the error is certainly better, if the program just continues with undefined, the program might crash later and its more difficult to debug, while if an error is thrown, you instantly know where the problem is.

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

I could do with some help with this one as well. Using typeof where I’ve interpolated multiple values into a string seemed to work fine, so:

console.log(typeof My cat is called ${catName}, and she is ${catAge} years old. My dog's name is ${dogName} and he is ${dogAge} years old. I've just interpolated multiple variables into this string!);

Returns string in the console. Which is what I was expecting.

However the final question asks us to:

  • Use typeof to find the data type of the resulting value when you concatenate variables containing two different data types.

Doing this:

console.log(typeof ‘My cat is called ’ + catName + ’ and she is ’ + catAge + ’ years old. Ive just concatenated this string using variables of different data types.’);

Returns this in the console:

stringMonty and she is 5 years old. Ive just concatenated this string using variables of different data types.

I can’t work out why the typeof method is only acting on the first string. How do I get it to act on everything between the brackets?

what would make you say that? You use what is known as template literals:

Template literals are string literals allowing embedded expressions

they are strings, so the expressions are resolved before typeof. calling typeof on a string, will always tell you the type is a string

if you want to know the type of a variable, you just do: typeof variableName

My lack of knowledge made me say it…

So safariblue’s suggested solution is correct then? I’m not on my laptop to test it right now but I’d set a variable to the value of the console.log output and then use that variable name to run typeof against?

that depends what you want to achieve. What do you want to do? This is recap lesson, the main objective is to understand the concepts taught. Although recap is good, it doesn’t give us a clear goal to achieve.

you now learned calling typeof on a template literal will result in type being a string

Both of us are trying to achieve the below (the final question in the recap section).

concatenate seems to imply using + to concatenate strings.

but you have achieved that goal? you now know the result is a string. To be absolutely certain you could use brackets as extra prove. But i will predict it doesn’t make a difference.

Really appreciate you trying to help me. I’m still confused I’m afraid and googling hasn’t helped me.

I’ll try and break it down.

The question is asking:

My code is:

let catName = 'Minnie';
let catAge = 5;

console.log('My cat is called ' + catName + ' , and is ' + catAge + ' years old.');

The output of this code is: “My cat is called Minnie , and is 5 years old.” So as the question has asked I’ve concatenated variables from two different data types (a string and a number).

I know the final output is a string by looking at it. What I’m struggling with is how I use the typeof operator to prove it’s a string as per the question.

I’m expecting a result in the console that says string but I’m not getting it.

I hope you can see what I’m trying to do now.

Sweet Jesus I think I’ve done it!

This code:

let catName = 'Minnie';
let catAge = 5;

let sentence = 'My cat is called ' + catName + ' , and is ' + catAge + ' years old.';

console.log(typeof `${sentence}`);

Returns:

"string"

in the console!

Just typing out that first post helped me work out what I’d achieved and what I was trying to do…phew.

1 Like

this is poorly worded.
it’s not variables that are concatenated, it’s values
variables don’t contain types, they refer to values. values have types.

what they’re asking you to do is to “concatenate” (presumably they mean use the + operator) and then use the typeof operator on the result to see what the type of the resulting value is

1 Like