Use typeof to Check the Type of a Variable

Tell us what’s happening:

Look at the third one console.log(). It returns me a typeof string where, sum variable has a type of number and a type of string.

My question is why not it return a type of number?

Your code so far


let seven = 7;
let three = "3";
let sum = (seven + three);
// Add your code below this line
console.log(typeof seven);
console.log(typeof three);
console.log(typeof sum);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/debugging/use-typeof-to-check-the-type-of-a-variable

My best guess is that it returns “string” because your variable “three” is a string, not a number.

It’s because your sum right now has the value of 73, not 10 as you expected. What you’re doing with seven + three right now is concatenating strings, hence 73, you can use console.log(sum) to check that. If you want to make sure they will do math instead of strings concatenation, you should do like this: let sum = seven + Number(three); so that sum will have its value as a number.

1 Like