Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k points)

Can I convert a string representing a boolean value (e.g., 'true', 'false') into an intrinsic type in JavaScript?

I have a hidden form in HTML that is updated based upon a user's selection within a list. This form contains some fields which represent boolean values and are dynamically populated with an intrinsic boolean value. However, once this value is placed into the hidden input field it becomes a string.

The only way I could find to determine the field's boolean value, once it was converted into a string, was to depend upon the literal value of its string representation.

var myValue = document.myForm.IS_TRUE.value; 

var isTrueSet = myValue == 'true';

Is there a better way to accomplish this?

1 Answer

0 votes
by (106k points)
edited by

To convert a string to boolean in JavaScript you can use the below-mentioned way:-

var isTrueSet = (myValue == 'true');

If you will use the identity operator (===) then you can make it more strict, what identity operator does is it doesn't make any implicit type conversions when the compared variables have different types, instead of the equality operator (==).

Check out Web Development Online Courses and enroll today!

Related questions

0 votes
1 answer
0 votes
1 answer
asked Aug 6, 2019 in Java by Anvi (10.2k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...