---
title: Yes/No Question
product: Form Library
description: Apply conditional logic to your JavaScript form with Boolean question types where respondents choose between two options - usually, yes/no or true/false - leading them to different branches of further questions.
framework: React
source: https://surveyjs.io/form-library/examples/yes-no-question/reactjs
index: https://surveyjs.io/form-library/examples/overview.md
---

# Yes/No Question (React)

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`](https://surveyjs.io/form-library/documentation/pagemodel#elements) array. Within this object, specify the question's [`title`](https://surveyjs.io/form-library/documentation/boolean-question-model#title) and a unique [`name`](https://surveyjs.io/form-library/documentation/boolean-question-model#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`](https://surveyjs.io/form-library/documentation/api-reference/boolean-question-model#labelTrue) and [`labelFalse`](https://surveyjs.io/form-library/documentation/api-reference/boolean-question-model#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`](https://surveyjs.io/form-library/documentation/api-reference/boolean-question-model#valueTrue) and [`valueFalse`](https://surveyjs.io/form-library/documentation/api-reference/boolean-question-model#valueFalse) properties. You will still be able to access the selected Boolean value via the [`booleanValue`](https://surveyjs.io/form-library/documentation/api-reference/boolean-question-model#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&mdash;when they are long.

To use a specific display mode, set the [`displayMode`](/form-library/documentation/api-reference/boolean-question-model#displayMode) property to `"segmented"` (default), `"radio"`, `"checkbox"`, or `"switch"`. This example demonstrates all four display modes.

## Files

### `public/index.html`

```html
<div id="surveyElement" style="position: absolute; top: 0; left: 0; right: 0; bottom: 0; min-height: 100%; height:100%"></div>
```

### `src/SurveyComponent.jsx`

```js
import React from "react";
import { Model } from "survey-core";
import { Survey } from "survey-react-ui";
import "survey-core/survey-core.min.css";
import "./index.css";
import { json } from "./json";

function SurveyComponent() {
    const survey = new Model(json);
    survey.onComplete.add((sender, options) => {
        console.log(JSON.stringify(sender.data, null, 3));
    });
    return (<Survey model={survey} />);
}

export default SurveyComponent;
```

### `src/index.css`

```css
/* You can add your custom CSS here. */
```

### `src/index.js`

```js
import React from "react";
import { createRoot } from "react-dom/client";
import SurveyComponent from "./SurveyComponent";

const root = createRoot(document.getElementById("surveyElement"));
root.render(<SurveyComponent />);
```

### `src/json.js`

```js
export const json = {
  "elements": [{
    "type": "boolean",
    "name": "segmented",
    "title": "Display mode = Segmented (default)",
    "valueTrue": "Yes",
    "valueFalse": "No"
  }, {
    "type": "boolean",
    "name": "radiobutton",
    "title": "Display mode = Radio",
    "valueTrue": "Yes",
    "valueFalse": "No",
    "displayMode": "radio"
  }, {
    "type": "boolean",
    "name": "switch",
    "title": "Display mode = Switch",
    "valueTrue": "Yes",
    "valueFalse": "No",
    "displayMode": "switch"
  }, {
    "type": "boolean",
    "name": "checkbox",
    "title": "Display mode = Checkbox",
    "valueTrue": "Yes",
    "valueFalse": "No",
    "displayMode": "checkbox"
  }]
};
```

### `src/theme.js`

```js
export const themeJson = {};
```

### `package.json`

```json
{
  "dependencies": {
    "react": "latest",
    "react-dom": "latest",
    "survey-core": "latest",
    "survey-react-ui": "latest"
  },
  "devDependencies": {
    "react-scripts": "latest"
  }
}
```

## Other Frameworks

- [Angular](https://surveyjs.io/form-library/examples/yes-no-question/angular.md)
- [Vue 3](https://surveyjs.io/form-library/examples/yes-no-question/vue3js.md)
- [jQuery](https://surveyjs.io/form-library/examples/yes-no-question/jquery.md)
- [Vanilla JS](https://surveyjs.io/form-library/examples/yes-no-question/vanillajs.md)
