A daily summary of javascript Learning (Boolean objects)

  • 2020-10-31 21:34:39
  • OfStack

Syntax for creating Boolean objects:
new Boolean(value); // constructors
Boolean(value); // Convert functions
The parameter value is the value stored by a Boolean object or the value to be converted to a Boolean value.
The return value
When called as a constructor (with the operator new), Boolean() converts its arguments to a Boolean value and returns an Boolean object containing that value.
If called as a function (without the operator new), Boolean() simply converts its argument to a primitive Boolean value and returns this value.
Note: If the value parameter is omitted or set to 0, -0, null, "", false, undefined, or NaN, the object is set to false. Otherwise, set to true (even if the value parameter is the string "false").


  var falseObject = new Boolean(false);
  var result = falseObject && true;
  alert(result); //true

  var falseValue = false;
  result = falseValue && true;
  alert(result); //false
  
  alert(typeof falseObject); //object
  alert(typeof falseValue); //boolean
  alert(falseObject instanceof Boolean); //true
  alert(falseValue instanceof Boolean); //false

Boolean object description
In JavaScript, a Boolean is a basic data type. The Boolean object is a Boolean object that wraps a Boolean value. The Boolean object is primarily used to provide the toString() method for converting Boolean values to strings.
When the toString() method is called to convert a Boolean value to a string (usually called implicitly by JavaScript), JavaScript intrinsically converts the Boolean value to a temporary Boolean object, and then calls the toString() method of that object.

The above is the summary of today's javascript study, and we will continue to update it every day. I hope you will continue to pay attention to it.


Related articles: