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
1 Like

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.

1 Like

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??

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