I have a javascript code where I extract the day of the week number. I want to check if that number is a especific number so I can put an output or another.
This is my Javascript code:
date = new Date().getDay();
if (typeof date == "4") {
document.write("Dijous");
}
I run this code in my html file but I get no output.
I have tried to run this code like you see.
2
Answers
Compare first by the number, not a string of a number.
Second, to log something, we use
console.log()
What you want to do is compare the day by value not by the
typeof
the day.typeof date
would return"number"
.developer.mozilla.org
Also I recommend comparing with the value
4
not"4"
sinceDate::getDay()
returns a number. Comparing the string only works due to the implicit conversion and the double equal==
.