JavaScript Boolean object
The boolean object in javascript is an object wrapper for boolean values. Booleans in JavaScript can also be defined using the new keyword.
Syntax:
new Boolean(value)
Below are examples of the JavaScript Boolean method.
Example 1: Below program will give false values for the first 4 variables & true for last 2 values as output.
let v1 = false;
let v2 = new Boolean(false);
let v3 = new Boolean("");
let v4 = new Boolean(0);
let v5 = new Boolean(true);
let v6 = new Boolean("w3wiki");
console.log('v1 = ' + v1);
console.log('v2 = ' + v2);
console.log('v3 = ' + v3);
console.log('v4 = ' + v4);
console.log('v5 = ' + v5);
console.log('v6 = ' + v6);
Output
v1 = false v2 = false v3 = false v4 = false v5 = true v6 = true
Example 2: Below program will give true for the first value & false for the second value as output.
let v1 = true;
let v2 = new Boolean(true);
console.log('v1 = = v2 is ' + (v1 == v2));
console.log('v1 = = = v2 is ' + (v1 === v2));
Output
v1 = = v2 is true v1 = = = v2 is false
Note: v1 = = = v2 is not true as the type of v1 and v2(object) is not the same.
Supported Browsers:
- Google Chrome 6 and above
- Edge 12 and above
- Firefox 4 and above
- Opera 12 and above
- Safari 5.1 and above
We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.
JavaScript Boolean
Boolean is a datatype that returns either of two values i.e. true or false. In JavaScript, Boolean is used as a function to get the value of a variable, object, conditions, expressions, etc. in terms of true or false.
Note: A variable or object which has a value is treated as a true boolean value. ‘0‘, ‘NaN’, empty string, ‘undefined’, and ‘null’ is treated as false boolean values.
Here a1 and a2 store the boolean value i.e. true and false respectively.
let a1 = true;
let a2 = false;
Note: BelThe below variables are initialized with strings, not boolean values.
let a1 ="true";
let a2 ="false";