-5

I'm sure there is a perfectly logical explanation to why this isn't working the way I want it to but I am new to JavaScript so would love some help. Is there any reason you would know as to why it prints out yes even when I want it to print out no. Thanks in advance

<script type="text/javascript">
    var username = prompt("What is your VC?");
    if (username = "wow") {
        greeting = document.write("yes");
    } else {
        document.write("no");
    }  
</script>
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71

3 Answers3

2

The = operator is used for assignment, for checking the value you can use ===. So, change the if as follows:

if (username === "wow")

Working code is given below:

var username = prompt("What is your VC?");
if (username === "wow") { 
    greeting = document.write("yes"); 
} else 
{ 
    document.write("no"); 
}
I. Ahmed
  • 2,438
  • 1
  • 12
  • 29
0

Yes. One = is assignment. Two == tests equality. And three === tests strict equality.

var username = prompt("What is your VC?");
if (username === "wow") {
  document.write("yes");
} else {
  document.write("no");
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

When you say:

a = b

You're saying "set a to the value in b."

If you out that in an if statement's expression, it assigns b to a and thrn checks if a's new value is "truthy."

If you want to ask "is a equal to b," you have to say:

a == b

I know that there is a difference between == and ===, but I am not a JavaScript master, so I cannot tell you what the difference is!

Sean
  • 265
  • 2
  • 10