1

I'm doing a like/dislike system. Here's the html code:

<form method="post" name="ratings">
    <input type="submit" name="vote" value="like">
    <input type="submit" name="vote" value="dislike">
</form>

In my index file, if I do

var inputValue = req.body.vote;
console.log(inputValue); 

I get either "like" or "dislike" depending on the button I click. However, I can't seem to use this value. Like if I try using an if statement with this value, I get true regardless of what I compare it to. For example:

if (inputValue = "random") {
    console.log("random");
}

random is returned in console, even though req.body.vote == "like" or "dislike".

indianhottie
  • 323
  • 3
  • 11
  • 1
    You compare with `==` or `===`, so it should be `if (inputValue == "random") {...`, making this a simple typo – adeneo Mar 03 '16 at 17:52

1 Answers1

2

A single = assigns, == or === checks for equality

Change: if (inputValue = "random") {

To: if (inputValue == "random") {

See Which equals operator (== vs ===) should be used in JavaScript comparisons? for information on which operator you should use for which usecase.

Community
  • 1
  • 1
baao
  • 71,625
  • 17
  • 143
  • 203
  • wow....im an idiot. dont know how i missed on this. thank you – indianhottie Mar 03 '16 at 17:53
  • To add to your answer: Never use ==. Because it passes truthiness tests whereas === is a deep equals. Use ===. – Patrick Motard Mar 03 '16 at 17:53
  • @PatrickMotard - *"never"* is a bit harsh, sometimes you want strict comparison, other times you don't. – adeneo Mar 03 '16 at 17:56
  • @adeneo It is harsh, but I believe it is warranted. The danger of == is that it is logically equivalent to && *even when you don't intend it to be*. This unnecessarily puts you at risk for unintended behaviors. If you want to compare truthiness, use &&, else ===. – Patrick Motard Mar 03 '16 at 18:01