If there is a mixed data type between string and number, the result is always string?

When asking for data type by using (typeof …), if there is a mixed data type between string and number, the result is always string? Not both? Even though you replace one’s position.

typeof looks for one object reference (or object) on its right. It is one of the unary operators in JS. If the operand is a concatenation of sorts, it will type only the first item and then concatenate with the second.

typeof 'thing' + 42
// 'string42'
typeof 42 + 'thing'
// 'numberthing'

If we group the concatenation, it will take place first, but likely resolve to a string if the types are mixed.

typeof (42 + 'thing')
// 'string'
1 Like

Thank you for the answer, it’s very kind of you.

1 Like

Sir,

Can you please help me out, please. According to this code composition:

Blockquote

let hungerLevel = 7;

if (hungerLevel = 6) {

console.log(‘Time to eat!’);

} else {

console.log(‘We can eat later!’);

}

Blockquote

From my understanding, if the hunger level is 7, it is time to eat. However, I tweaked the value inside the "if (method), and change it to, literally any number, the first condition is printed, while the value is lower and/or greater than 7. Shouldn’t it be resulting different conditional output?

Thank you.

Be sure to use a comparison operator (===) when testing values.

1 Like

Ah I see, at first I thought since the initial value of hunger level is 7 and I tested the value of 6, which logically has smaller amount than 7 and 8 which is bigger, I thought the if will execute the corresponding console. I just realized it is about the matching case/type. I have some other questions as a beginner, how do I make my own thread instead of here, sir? Thank you.

This is now your own thread, but if the question does not relate to the above, then start a New Topic in the JS Help category.

1 Like

Sir, thank you again for your help and explanation. Can you please help me again with this, both have two mutuals, but on the first line only printed one, why not both? or at least one line with only the mutuals. Thank you so much.

Move line 10 to line 14 so it doesn’t print until the loops are finished.