I have read a lot of good stuff on truthy and falsy (.html), and in particular the differences between '==' and '===' in JavaScript. The best explanations and resources mainly from this Stack Overflow Question:
Which equals operator (== vs ===) should be used in JavaScript parisons?
I am working on a form and need to store a boolean value into a hidden field with the following jQuery syntax:
$('[name="fieldName"]').val(true);
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 pare it as follows:
$('[name="fieldName"]').val() === "true"
Note how I am using the === operator to pare to the string value "true".
My question; if only Object in JavaScript is of type reference, then boolean must be a value type. I would therefore assume, as value parisons with the === operator pare 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?
This code previously used == for the parison, but to avoid falsy risks I am implementing === wherever possible. Could it be something to do with jQuery converting the values for me?
I have read a lot of good stuff on truthy and falsy (http://adripofjavascript./blog/drips/truthy-and-falsy-values-in-javascript.html), and in particular the differences between '==' and '===' in JavaScript. The best explanations and resources mainly from this Stack Overflow Question:
Which equals operator (== vs ===) should be used in JavaScript parisons?
I am working on a form and need to store a boolean value into a hidden field with the following jQuery syntax:
$('[name="fieldName"]').val(true);
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 pare it as follows:
$('[name="fieldName"]').val() === "true"
Note how I am using the === operator to pare to the string value "true".
My question; if only Object in JavaScript is of type reference, then boolean must be a value type. I would therefore assume, as value parisons with the === operator pare 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?
This code previously used == for the parison, but to avoid falsy risks I am implementing === wherever possible. Could it be something to do with jQuery converting the values for me?
Could it be something to do with jQuery converting the values for me?
HTML form elements always store the value as a string, regardless of what the actual content is. In your case it is a boolean
, but even if it was say a number
type, what you would see is "10.5" not 10.5.
If you'd really like to pare boolean to boolean, what I would remend is:
Boolean($j('[name="fieldName"]').val()) === true
Boolean() approach will not work because:
Boolean("true") === true;
Boolean("false") === true;
Seems that only way it is:
$j('[name="fieldName"]').val(true)
$j('[name="fieldName"]').val() === "true"
or
$j('[name="fieldName"]').val(1)
$j('[name="fieldName"]').val() === "1"
etc.