X



Home>Tutorial>

What is JavaScript Boolean? Its Uses, Methods and Properties

Nuelson Penuel Saturday, July 22, 2023 Javascript

In this article, you are going to learn about JavaScript Boolean, uses of JavaScript Boolean, JavaScript Boolean properties, JavaScript Boolean Methods, as well as examples to help in your understanding.

JavaScript Boolean

Boolean in JavaScript is an object that is uses true or false to represents a value. In order words, Boolean is used when you have to test if the outcome of an expression is true or false It can be created using the Boolean() constructor as shown below.
Boolean myvalue = new Boolean(value); 
JavaScript Boolean object has "false" as its default value.

JavaScript Boolean Examples

<script>  
alert(100<20);//false
alert (100>20);//true
</script>  

JavaScript Boolean Properties

In JavaScript, there are several built-in boolean properties available for boolean values. These properties help in performing various operations and checking the state of boolean values. Here are some commonly used boolean properties in JavaScript: 1. Boolean.prototype: This property represents the prototype object for the Boolean constructor. It allows adding new properties and methods to all Boolean objects. 2. Boolean.prototype.constructor: This property returns a reference to the Boolean constructor function that created the instance object. 3. Boolean.prototype.toString(): This property returns a string representing the specified Boolean object. It converts a boolean value to its string representation (`"true"` or `"false"`). 4. Boolean.prototype.valueOf(): This property returns the primitive value (boolean) of a Boolean object. It is used to retrieve the boolean value of a boolean object. Let's see an example usage of these boolean properties:
let bool = true;
console.log(bool.constructor);             // Output: function Boolean() { [native code] }
console.log(bool.toString());              // Output: "true"
console.log(bool.valueOf());               // Output: true

console.log(bool.constructor === Boolean); // Output: true
console.log(bool instanceof Boolean);      // Output: false (since bool is not an instance of Boolean)
These properties can be used to manipulate and determine the state of boolean values in JavaScript.

JavaScript Boolean Methods

In JavaScript, there are several built-in methods available for boolean values. These methods allow you to perform various operations and transformations on boolean values. Here are some commonly used boolean methods in JavaScript: 1. Boolean(): The Boolean() method is used to convert a value to its corresponding boolean value. It returns `true` if the value is "truthy" (i.e., not false, 0, null, undefined, NaN, or an empty string), and returns `false` otherwise.
console.log(Boolean(10)); // Output: true
console.log(Boolean("Hello")); // Output: true
console.log(Boolean(0));  // Output: false
console.log(Boolean(undefined));   // Output: false
console.log(Boolean(null)); // Output: false
console.log(Boolean("")); // Output: false
2. valueOf(): The valueOf() method is used to return the primitive value (boolean) of a Boolean object.
let bool = new Boolean(true);
console.log(bool.valueOf()); // Output: true
3. toString(): The toString() method is used to convert a boolean to its string representation ("true" or "false").
let bool = true;
console.log(bool.toString()); // Output: "true"
These methods can be used to manipulate and transform boolean values in JavaScript.

Uses of JavaScript Boolean

There are several important uses of JavaScript Boolean in programming. Here are some common uses: 1. Conditionals: Boolean values are widely used in conditional statements like `if`, `else if`, and `else` to execute certain blocks of code based on whether a condition is true or false. For example:
let isLogin = true;

if (isLogin) {
  console.log("Welcome!");
} else {
  console.log("Please log in.");
}
2. Comparisons: Boolean values are generated when comparing two values using comparison operators such as `==`, `===`, `!=`, `!==`, `<`, `>`, `<=`, and `>=`. These comparisons evaluate to either true or false, indicating the relationship between the compared values.
let num1 = 5;
let num2 = 10;
let isEqual = num1 === num2;
console.log(isEqual); // Output: false
3. Logical operators: Boolean values are also used with logical operators like `&&` (AND), `||` (OR), and `!` (NOT). These operators evaluate boolean expressions and return a boolean value. They are frequently used to combine conditions or negate them.
let age = 25;
let isAdult = age >= 18 && age <= 65;

console.log(isAdult); // Output: true
4. Loop conditions: Boolean values can serve as loop conditions. They decide whether a loop should continue iterating or stop based on the evaluation of the boolean expression. Common loop structures like `while` and `do...while` use boolean values as their controlling conditions.
let i = 0;
while (i < 5) {
  console.log(i);
  i++;
}

Conclusion

In conclusion, JavaScript provides several built-in methods for dealing with boolean values. The `Boolean()` method can be used to convert values to their corresponding boolean representation. The `valueOf()` method returns the primitive boolean value of a boolean object, and the `toString()` method converts a boolean to its string representation. These methods are useful for performing operations and transformations on boolean values in JavaScript. They allow you to check the truthiness or falsiness of a value, convert booleans to different data types, and perform various operations based on boolean conditions. Understanding and utilizing these methods can help in writing more efficient and effective JavaScript code.

| | Views(140)

See Demo | Download File


Add your comment. Indicate code using code tag


Words: 0 | Chars: 0 | Reading Duration: 0



Images Attachment




Emmason Integratded Services(2017-2024)
All Rights Reserved
Designed and Maintained By Emmason Integrated Services