You can do all of this in one line if you want to:
const getBool = value => value == "true" || value == "false" ? value == "true" : value;
getBool($(this).val());
Explanation
In JavaScript you'll probably be used to using something like
if(answer == "yes"){ ...
answer == "yes"
will evaluate to true
or false
.
For your question, this can certainly help one part of it. If there were only "true"
or "false"
options it would be as simple as:
const getBool = (value) => value == "true"
getBool("true"); // true
getBool("false"); // false
We can now wrap this in a ternary operator, basic overview:
`if this` ? `then do this` : `if not, then do this`
So we check if the value is true or false, and if so use the above technique.
value == "true" || value == "false"
If its not, we can just return the value without any modifications. So the test becomes:
value == "true" || value == "false" ? value == "true" : value;