1

I am trying to understand equality in JavaScript. Here is the code.

    var x = prompt("What is 10 + 10");
    if (x === 10) {
        document.write("Correct")
    }
    else {
        document.write("Incorrect")
    }

Why wouldn't I make the equals sign like "===". So if "10" is equal("===") to "x"(user answer) then it should be correct right?

I searched on both Stack Overflow and W3Schools, but couldn't find what I was looking for. I guess I'm just nor getting this "true or false" thing. I mean this seems like a very simple equation. Help would be great thanks guys!

Richard Hamilton
  • 25,478
  • 10
  • 60
  • 87
  • A prompt returns a string, and you're comparing agains a number. If you do strict comparison (three equal signs), the type will be checked as well, so type number and type string will not be equal, but if you use non-strict comparison (two equal signs) it won't check the type and `10 == "10"` etc – adeneo Dec 13 '15 at 16:45
  • I disagree because the other questions doesn't EXACTLY explain in detail to detail what I need I wanted a simple answer to a simple equation and most questions and answers Ive read don't make sense to me. Or have Ridiculously complicated answers. I figured asking something simple that I understand the answer would be clearer to me. – Brandon Langlois Dec 13 '15 at 16:57
  • Thanks, I just get tired of these crazy answers that make no sense and the Moderators up people backs all the time I get your trying to get rid of certain stuff. But someone like me I want an answer not a bunch of edits and whatnot you know... and I see it a lot.... – Brandon Langlois Dec 13 '15 at 17:09

2 Answers2

5

=== is strict type equality which compares by both value and type

== is non-strict type equality, which compares only by value.

In other words, == performs type conversion and then compares values for equality. Here are some examples

"3" == 3
=> true

Explanation: The string 3 is converted to the number 3, which is equal to 3.

"3" === 3
=> false

Explanation: The string is not converted to a number. Thus the string 3 does not equal the number 3.

In your example, incorrect would be written to the document. That is because the result of prompt returns a string, and you are performing strict equality with a number.

In your case, the interpreter sees it like this

if ("10" === 10) {
    // does the string "10" equal the number 10? If so
    document.write("Correct")
}
else {
    // Hey, wait a minute. It doesn't equal the number. I should write "Incorrect" instead.
    document.write("Incorrect")
}
Richard Hamilton
  • 25,478
  • 10
  • 60
  • 87
  • So what your saying is that "===" is for a specific type like a string not a number? – Brandon Langlois Dec 13 '15 at 16:53
  • 1
    No. For `===`, no automatic type conversion is performed, so it compares both the value AND type. In other words, this one cares about the type you use. `==` only cares about the value – Richard Hamilton Dec 13 '15 at 16:54
  • OHHH i see thanks GREAT answer.. simple and understood thank you sir have a great day. Happy Holidays! – Brandon Langlois Dec 13 '15 at 16:59
  • Something else I dont get is that === reads specific things like strings, but if i dont use the quotation and just put a number like 10 then why would it read it as a string not a number? – Brandon Langlois Dec 13 '15 at 17:02
  • The prompt method returns a String. http://www.w3schools.com/jsref/met_win_prompt.asp – Greg Syme Dec 13 '15 at 17:41
1

In Javascript,

== means: is equivalent to

=== means: is identical to

When the value of x is "10", x is equivalent to 10.

But it isn't identical to 10.

Rounin
  • 27,134
  • 9
  • 83
  • 108