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