Returns string. What’s the logic here?

I did the following and it still returned string for typeof operator (apologies for the mess)

const myName ='Titi'; let myAge = 27; let boolean = true; let myFavouriteFood = 'sweets'; console.log(`My name is ${myName}. I am ${myAge} years old. I love to eat ${myFavouriteFood}`); console.log('Diabetes is not good: ' + boolean); console.log(typeof boolean); let sentence = myName + ' is ' + myAge + ' years old: ' + boolean; console.log(typeof sentence); console.log(sentence);

the console.log(typeof sentence); line returns string. What’s the logic here?

Simple, the boolean is being cast to string and Bob’s your uncle.

2 Likes

@mtf on line 15 even though myAge is a number/integer the typeof operator will still return string when evaluating the concatenation. I think that’s what I wanted clarification on

When you concatenate anything, it all becomes a string. So, when concatenating floats, ints, bools, they all get cast to string on concatenation.

1 Like

Thank you both @mtf and @codeneutrino youse are very helpful. Early Happy NY and cheers to infinite learning

2 Likes