homepage Welcome to WebmasterWorld Guest from 54.205.254.108
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Comparison with 0 and JSLint error
Rain_Lover




msg:4686853
 7:27 am on Jul 11, 2014 (gmt 0)

Here's the code:

<input type="number" value="0" id="input">
<script>
if (document.getElementById('input').value == 0) {
alert('Hello, world!');
}
</script>


DEMO [jsfiddle.net]

And here's the JSLint error:

Expected '===' and instead saw '=='.


But when I listen to the advice and change
== to ===, the alert stops appearing.

 

birdbrain




msg:4686875
 8:35 am on Jul 11, 2014 (gmt 0)

Hi there Rain Lover,
JSLint requires...



if (document.getElementById('input').value === '0')




birdbrain

penders




msg:4686933
 12:13 pm on Jul 11, 2014 (gmt 0)

JSLint error: "Expected '===' and instead saw '=='."


But when I ... change == to ===, the alert stops appearing.


?

Are you asking why JSLint is suggesting this?

Rain_Lover




msg:4686935
 12:18 pm on Jul 11, 2014 (gmt 0)

Yes, and if it's OK to just leave it as is:
input.value == 0
Sorry if the question wasn't very clear!

Fotiman




msg:4686945
 1:47 pm on Jul 11, 2014 (gmt 0)

You're comparing a string value to an integer value. Accessing the value property of an input element will always return a string value:

document.getElementById('input').value

The == operator will attempt to type-convert the values to do the comparison. The === operator will not, meaning if you try to compare a string with a number, it will ALWAYS return false. When you use == instead of === the string value type is converted to a number for the comparison.

As birdbrain suggested, changing 0 to '0' and using === will work. It's then comparing 2 string values. Another alternative would be to explicitly convert the value of the input field to a number first.

It is ok to leave it as input.value == 0, and let JavaScript handle the type conversion in cases where you're getting a value from an input element. There are legitimate cases where you would want strict comparisons and you would want '5' !== 5, but generally that's not the case when getting a value directly from an input element (as that always returns a string).

Rain_Lover




msg:4686946
 1:54 pm on Jul 11, 2014 (gmt 0)

As birdbrain suggested, changing 0 to '0' and using === will work. It's then comparing 2 string values.


How about
input.value == '0'?
Am I right in my assumption that
input.value == '0' is faster than input.value == 0 as the first one doesn't need a conversion?
Fotiman




msg:4686955
 2:18 pm on Jul 11, 2014 (gmt 0)

Yes, that's essentially the same thing without the type conversion (but JSLint will probably still complain about the ==). If you were going to do that, you might as well use the strict comparison operator instead.

While I haven't tested it, I suspect that yes, a comparison that doesn't need a type conversion would be faster. With that said, the difference would be so small (minuscule really) that it would not be noticeable unless you were looping over HUGE data (and I mean really huge), so there's no real performance gains to be had by using one or the other.

penders




msg:4686957
 2:29 pm on Jul 11, 2014 (gmt 0)

(EDIT: Didn't see Fotiman's post just above before posting this...)

How about input.value == '0'?


Yes, that is OK. (Comparing two known strings.) I don't see as there is any benefit to using the identity operator (===) in this instance. And JSLint is OK with this too.

Am I right in my assumption that input.value == '0' is faster than input.value == 0 as the first one doesn't need a conversion?


Well, yes... marginally... a micro optimisation at best. But it is not for performance that you would choose one over the other. It could depend on your program logic, on the data being entered. As Fotiman states, in the case of the (input.value == 0) comparison, the value (a string) is first converted to a number....

'0' == 0 [TRUE]
'00' == 0 [TRUE]
'0x0' == 0 [TRUE]

'0' == '0' [TRUE]
'00' == '0' [FALSE]
'0x0' == '0' [FALSE]

Rain_Lover




msg:4687135
 8:04 am on Jul 12, 2014 (gmt 0)

Accessing the value property of an input element will always return a string value


That's the key point. Thank you!

Rain_Lover




msg:4687136
 8:08 am on Jul 12, 2014 (gmt 0)

I don't see as there is any benefit to using the identity operator (===) in this instance.


That's what I thought although everyone recommends
=== over ==. Thanks!
Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved