Yes/No Question
A Yes/No, or Boolean, question asks respondents to select between two options: yes or no, true or false, etc. Boolean questions can be used to implement conditional survey logic or as standalone questions. This example demonstrates how to create and configure a Boolean question in SurveyJS Form Library. Switch between Angular, React, Vanilla JavaScript, jQuery, and Vue to view an example for your JavaScript framework.
Create a Yes/No Question
To create a Boolean question, define an object with the type property set to "boolean" and add it to the elements array. Within this object, specify the question's title and a unique name that identifies the question.
Change Positive and Negative Text Labels
A Boolean question displays "Yes" for the positive answer and "No" for the negative answer. Use the labelTrue and labelFalse properties to change the displayed text labels. In this demo, you can use Settings to change the labels at runtime.
Save Custom Values for true and false
Survey results store true or false as an answer to a Boolean question. If you want to save custom values instead, assign them to the valueTrue and valueFalse properties. You will still be able to access the selected Boolean value via the booleanValue property. In this demo, Boolean questions save "Yes" or "No" in survey results.
Change Display Mode
Boolean questions support the following UI modes:
- Segmented toggle
- Radio button group
- Checkbox
- Switch
By default, segmented toggle automatically applies when labels are short; radio button group—when they are long.
To use a specific display mode, set the displayMode property to "segmented" (default), "radio", "checkbox", or "switch". This example demonstrates all four display modes.
Require a Positive Answer
To require agreement or acknowledgment, enable the requireTrue property. Only a positive answer passes validation: true by default, or the custom value specified by valueTrue. A negative or empty answer fails validation. Use requireTrueErrorText to customize the error message.
Unlike requireTrue, the isRequired property only checks that the question is answered. Both positive and negative answers pass this check. You do not need to enable isRequired when requireTrue is enabled.
In this demo, leave the checkbox unchecked and click Complete to see the custom error message.