Storing boolean values in hidden fields for form processing
<p>I have read a lot of good stuff on truthy and falsy (<a href="http://adripofjavascript.com/blog/drips/truthy-and-falsy-values-in-javascript.html" rel="nofollow noreferrer">http://adripofjavascript.com/blog/drips/truthy-and-falsy-values-in-javascript.html</a>), and in particular the differences between '==' and '===' in JavaScript. The best explanations and resources mainly from this Stack Overflow Question:</p>

<p><a href="https://stackoverflow.com/questions/359494/does-it-matter-which-equals-operator-vs-i-use-in-javascript-comparisons">Which equals operator (== vs ===) should be used in JavaScript comparisons?</a> </p>

<p>I am working on a form and need to store a boolean value into a hidden field with the following jQuery syntax:</p>

<p><code>$('[name="fieldName"]').val(true);</code></p>

<p>So I am inserting a boolean value, true or false, into the hidden field. During on screen processing, I grab this field value in a number of if-statements with the following code, and compare it as follows:</p>

<p><code>$('[name="fieldName"]').val() === "true"</code></p>

<p>Note how I am using the === operator to compare to the string value "true". </p>

<p>My question; if only Object in JavaScript is of type reference, then boolean must be a value type. I would therefore assume, as value comparisons with the === operator compare not only contents but the type, that the above expression should return false? However in my code it returns true - can anyone explain why this is?</p>

<p>This code previously used == for the comparison, but to avoid falsy risks I am implementing === wherever possible. Could it be something to do with jQuery converting the values for me? </p>