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); 
  • if (typeof myNumber === "number") - YozhEzhi
  • return 10 + myNumber; - here you get a string, by the way. - YozhEzhi
  • 2
    Why not read the typeof syntax? Immediately understand the error. - user207618
  • I need the console to have a total of 20, and if a person enters not a number, then an error. If I take the number in quotes it does not solve the problem - Eugene

3 answers 3

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.

enter image description here

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.

enter image description here

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:

      1. Operator syntax: typeof x.
      2. Function syntax: typeof (x).

      https://learn.javascript.ru/types-intro#type-typeof