FAQ: Variables - Review Variables

Perhaps that’s why I was initially confused, I just copied the question as is from the exercise. Your explanation makes sense to me, thanks for clarifying.

Does my code in my follow up post look right to you?

No.

You’re checking the type of a string instead of checking the type of the value you obtained from using +

If you set sentence to 5 then you’d expect to see the type written out as “number” but you’d still write out “string” since you’re pretty much ignoring sentence

1 Like

Thanks. I can see what you mean now. typeof is always going to return string in this scenario regardless of the value in the sentence variable.

So I think I now have it by making a slight tweak:

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

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

console.log(typeof sentence);

Returns:

"string"

I can test it as you suggested by changing the value of sentence to:

4

Doing this returns:

"number"

So I now know that typeof is looking at the value of the variable named sentence.

Thanks for your help and patience.

1 Like

Hi. So the last challenge was to use typeof on concatenate variables containing different data types. I can see why you were using typeof to a string interpolation which the answer resulted to string. so here is an example of what the challenge meant by concatenate only variables.

`let myName = ‘Joseph’;
let myValue = true;
let myNumber = 24;

console.log(typeof myName + typeof myValue + typeof myNumber);`

1 Like

Everything made sense to me except one thing. Why would one use const in any situation when you could just use let?

Not only are you able to change the value of let but it does the same thing as const. Why not just use let all the time in case you do need to change it.

You might find this article an interesting read…

https://mathiasbynens.be/notes/es6-const

1 Like

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

console.log('10' + 1); returns 101
console.log('10' - 1); returns 9
Why is that?

Javascripts type coercion, the types don’t match, so JS tries to make the best of it.

while using +, JS things you want to concat strings. This isn’t possible when doing minus, so then JS thinks you intended to use numbers

i am sure there are good explanations on the internet

OHHH, Thank you. I got that now.