About JavaScript judging whether it is a number or not

topic description

write an expression that results in whether 2018 is a leap year.

conditions for judging leap years:
is a multiple of 400, or a multiple of 4 and not a multiple of 100.
1900 false
2000 true
2004 true

ideas

first judge whether it is a number by if. When it is a number, the nested if judges the condition of leap year
if it is not a number, then output "input is not a number"

problems encountered

  if (typeof year != "number") {
        return console.log("");
   }

determine the type of year through typeof, but if you enter a non-numeric number, you will directly report an error "xxx is not defined"
, but just add "" to the input box

.

overall code

function isLeapYear(year) {
    if (typeof year != "number") {
        return console.log("");
    } else {
        if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) {
            return console.log(year + "");
        } else {
            return console.log(year + "");
        }
    }
}
isLeapYear(asd);

expect to solve doubts

1. Why did this happen? what is the reason for it?
2. How to get the desired effect without quotation marks when calling (prompt "input is not a number")

Thank you for your answer

Feb.19,2022

do you directly use isLeapYear (asd); to report an error? asd is this variable defined?

isLeapYear ('asd'); this is the string asd passed in, and a variable is passed without a comma. If the variable is not defined, the error is reported. Define

first as follows.
var asd = 'abcd';

isLeapYear(asd);

clipboard.png

asd does not add single / double quotation marks, the default is a variable, not a string. As shown in the figure.

Menu