How to toggle a boolean using JavaScript ? Last Updated : 16 Jan, 2024 Comments Improve Suggest changes Like Article Like Report A boolean value can be toggled in JavaScript by using two approaches which are discussed below: Table of Content Using the logical NOT operatorUsing the ternary operatorUsing the XOR (^) operatorMethod 1: Using the logical NOT operator The logical NOT operator in Boolean algebra is used to negate an expression or value. Using this operator on a true value would return false and using it on a false value would return true. This property can be used to toggle a boolean value. The NOT operator is used before the variable is toggled and the result is assigned to the same variable. This will effectively toggle the boolean value. Syntax:booleanValue = !booleanValueExample: We are using the logical NOT operator html <html> <head> <title> How to toggle a boolean using JavaScript? </title> </head> <body> <h1 style="color: green"> GeeksforGeeks </h1> <b> How to toggle a boolean using JavaScript? </b> <p> The boolean is toggled whenever the button is pressed. </p> <p> See the console for the output </p> <button onclick="toggle()"> Toggle Bool </button> <p id="toggled"> </p> <script> let testBool = true; function toggle() { testBool = !testBool; let text = document.getElementById('toggled') text.innerHTML = testBool } </script> </body> </html> Output: Method 2: Using the ternary operatorThe ternary operator is used as a shortcut to using the if/else statement. It is used to make a decision based on the condition of expression. It takes three parameters, the condition statement, the expression to be executed if the condition is satisfied and the expression to be executed if the condition is not satisfied. The boolean value to be toggled is passed as the condition. The expression to be executed if true is given as 'false' and expression to be executed if false is given as 'true'. The result of this operator is assigned back to the boolean value to be toggled. This will effectively toggle the value as a true condition will return false, and a false condition would return true. Syntax:booleanValue = booleanValue? false : trueExample: We are using the ternary operator html <!DOCTYPE html> <html> <head> <title> How to toggle a boolean using JavaScript? </title> </head> <body> <h1 style="color: green"> GeeksforGeeks </h1> <b> How to toggle a boolean using JavaScript? </b> <p> The boolean is toggled whenever the button is pressed. </p> <button onclick="toggle()"> Toggle Bool </button> <p id="toggled"> </p> <script> let testBool = true; console.log('Default value of bool is', testBool); function toggle() { testBool = testBool ? false : true; let text = document.getElementById('toggled') text.innerHTML = testBool } </script> </body> </html> Output: Method 3: Using the XOR (^) operatorIn this method, If the operands have different boolean values, the XOR operator returns true; otherwise, it returns false. Example: We are using XOR (^) operator. HTML <html> <head> <title> How to toggle a boolean using XOR in JavaScript? </title> </head> <body> <h1 style="color: green"> GeeksforGeeks </h1> <b> How to toggle a boolean using XOR in JavaScript? </b> <p> The boolean is toggled whenever the button is pressed. </p> <p> See the console for the output </p> <button onclick="toggle()"> Toggle Bool </button> <p id="toggled"> </p> <script> let testBool = true; console.log('Default value of bool is', testBool); function toggle() { testBool = testBool ^ true; let text = document.getElementById('toggled') text.innerHTML = testBool ? "true" : "false" // console.log('Toggled bool is', testBool); } </script> </body> </html> Output: Comment More infoAdvertise with us Next Article How to toggle a boolean using JavaScript ? sayantanm19 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to add bootstrap toggle-switch using JavaScript ? Bootstrap toggle-switch is a simple component used for activating one of the two given options. Commonly used as an on/off button. The CSS framework is a library that allows for easier, more standards-compliant web design using the Cascading Style Sheets language. Bootstrap 4 is one such widely used 2 min read How to Toggle Popup using JavaScript ? A popup is a graphical user interface element that appears on top of the current page, often used for notifications, alerts, or additional information. These are the following approaches to toggle a popup using JavaScript: Table of Content Using visibility propertyUsing ClassListUsing visibility pro 3 min read JavaScript Boolean toString() Method The boolean.toString() method is used to return a string either "true" or "false" depending upon the value of the specified boolean object. Syntax: boolean.toString() Parameter: This method does not accept any parameter. Return Values: It returns a string either "true" or "false" depending upon the 3 min read How to disable radio button using JavaScript ? Radio buttons let users select one option from many, commonly used in forms. Sometimes, based on a user's previous choice, certain options need to be disabled. For example, if a user selects "No" to playing games, we can disable related game options. Using JavaScript, about 65% of forms use such log 4 min read How to Toggle an Element Class in JavaScript ? In JavaScript, toggling an element class refers to the process of dynamically adding or removing a CSS class from an HTML element. This allows developers to easily change an element's appearance or behavior in response to user actions, such as clicks or events.These are the following methods for tog 2 min read How to Toggle Text with JavaScript? Toggling text is a common functionality in web development. Text toggling means switching between two or more text values when a user performs a specific action, such as clicking a button. This feature can be used in creating expandable/collapsible sections, toggling visibility of text etc.Below are 2 min read How to Toggle between Hiding and Showing an Element using JavaScript ? Toggle between hiding and showing an element using JavaScript provides the feature of efficient space usage by allowing users to hide content sections they may not need to minimize distractions, leading to a cleaner and more organized layout. Syntaxif (paragraph.style.display === 'none') { paragraph 3 min read JavaScript - Convert a String to Boolean in JS Here are different ways to convert string to boolean in JavaScript.1. Using JavaScript == OperatorThe == operator compares the equality of two operands. If equal then the condition is true otherwise false. Syntaxconsole.log(YOUR_STRING == 'true');JavaScriptlet str1 = "false"; console.log(str1 == 'tr 3 min read How to Check/Uncheck the checkbox using JavaScript ? To check and uncheck the checkbox using JavaScript we can use the onclick event to toggle the checkbox value. We can also check or uncheck all the checkboxes on loading the webpage using JavaScript onload function.In this article, we will learn how to check/uncheck the checkbox using JavaScript. As 3 min read How to unchecked a radio button using JavaScript/jQuery? In order to uncheck a radio button, there are a lot of methods available, but we are going to see the most preferred methods. In HTML, we can create radio buttons using the same name attribute, and by setting a unique value for each radio button within the group. Only one radio button within the gro 2 min read Like