Skip to content
the poor coder

javascript boolean change value

Poor Man
Published
Reading time
2 min read
On this page

Javascript Boolean Change Value#

Boolean is a data type in Javascript which can have two values: true or false. Sometimes, we need to change the boolean value based on certain conditions. In this blog post, I will explain how to change the boolean value in Javascript.

Method 1: Using the NOT operator (!)#

The easiest way to change the boolean value in Javascript is by using the NOT operator. The NOT operator is represented by an exclamation mark (!) and it flips the boolean value. For example:

Text

let boolValue = true;
boolValue = !boolValue;
console.log(boolValue); // false

In the above example, we have initialized a boolean variable boolValue with a value of true. We then use the NOT operator to flip the boolean value and assign it back to the same variable. Finally, we log the updated value of the variable which is false.

Method 2: Using the Comparison Operator (===)#

We can also change the boolean value in Javascript by comparing it with another value using the comparison operator (===). The comparison operator returns a boolean value which can be assigned to a variable. For example:

Text

let num1 = 10;
let num2 = 20;
let boolValue = num1 === num2;
console.log(boolValue); // false

In the above example, we are comparing two variables num1 and num2 using the comparison operator. Since num1 is not equal to num2, the comparison operator returns false which is then assigned to the variable boolValue.

Method 3: Using the Ternary Operator (? :)#

The last method to change the boolean value in Javascript is by using the ternary operator. The ternary operator is represented by a question mark followed by a colon (? :). It is a shorthand way of writing an if-else statement. For example:

Text

let num1 = 10;
let num2 = 20;
let boolValue = (num1 === num2) ? true : false;
console.log(boolValue); // false

In the above example, we are using the ternary operator to check if num1 is equal to num2. If it is, then the ternary operator returns true, otherwise it returns false. The returned value is then assigned to the variable boolValue.

These are three different ways to change the boolean value in Javascript. You can use any of these methods depending on your use case.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC