How to use `typeof` when concatenating two data types?

  • 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
3 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.

57 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

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

7 Likes

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?

2 Likes

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

2 Likes

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.

1 Like

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.

11 Likes

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

2 Likes

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

3 Likes

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.

4 Likes

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);`

10 Likes

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

2 Likes

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

6 Likes

OHHH, Thank you. I got that now.

1 Like

yeah but instead of writing whole sentence i also tested it by just writing a word and a number in two different variables but still it gives string as an answer.

let word = 'yes';
let no = 101;
let twoVariable = word + no;
console.log(twoVariable);
console.log (typeof twoVariable);

output is
yes101
string

3 Likes