function myFunc (myNumber) {
if (typeof Number! = myNumber) {
alert ("You passed non Number");
return myNumber
}
else {
return 10 + myNumber;
}
}
var myNumber = 10;
myNumber = myFunc (myNumber);
console.log (myNumber); 3 answers
Well, for starters, let's make it clear that the constructors of standard types are not these same types as in other languages. They are all prototype functions. And in turn, return the desired object.
Since they are all functions, it is pointless to compare them in type with something other than functions.
You were correctly told to read the documentation on typeof, this keyword returns a string with the name of the type. Therefore, even if Number were a number, you would end up with a string.
Comparing a string with a number does not give anything normal either. The correct option is to convert the number itself to type and compare.
"number" != typeof myNumber Well, the full code
function myFunc(myNumber) { if ("number" != typeof myNumber){ alert("Вы передали не Number"); return myNumber } else{ return 10 + myNumber; } } var myNumber = 10; myNumber = myFunc(myNumber); console.log(myNumber); When you compare
typeof Number != myNumber You compare the function (typeof Number) with a number. As written above, you need to determine the type of the variable.
function myFunc(myNumber) { console.log(typeof myNumber); if (typeof myNumber !== "number"){ alert("Вы передали не Number"); return myNumber; } else{ return 10 + myNumber; } } var myNumber = 10; myNumber = myFunc(myNumber); console.log(myNumber); The correct syntax is: typeof (myNumber) != "number"
function myFunc(myNumber) { if (typeof (myNumber) != "number"){ alert("Вы передали не Number"); return myNumber; } else{ return 10 + myNumber; } } var myNumber = 10; myNumber = myFunc(myNumber); console.log(myNumber); The typeof operator returns the type of the argument.
It has two syntaxes: with brackets and without:
- Operator syntax: typeof x.
- Function syntax: typeof (x).


if (typeof myNumber === "number")- YozhEzhireturn 10 + myNumber;- here you get a string, by the way. - YozhEzhitypeofsyntax? Immediately understand the error. - user207618