skip to Main Content

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


  1. date = new Date().getDay();
    
                if (date === 4) {
    
                    console.log("Dijous");
                }
    

    Compare first by the number, not a string of a number.

    Second, to log something, we use console.log()

    Login or Signup to reply.
  2. What you want to do is compare the day by value not by the typeof the day. typeof date would return "number".

    The typeof operator returns a string indicating the type of the
    operand’s value.

    developer.mozilla.org

    Also I recommend comparing with the value 4 not "4" since Date::getDay() returns a number. Comparing the string only works due to the implicit conversion and the double equal ==.

    const date = new Date().getDay();
    
    if (date === 4) {
      console.log("Dijous");
    }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search