Sunday, 21 August 2016

JavaScript Boolean Object

The Boolean Object
It returns either true or false. It is wrapper class and a member of global objects. It returns false when the object is passed with values, such as 0, empty string, false, null, undefined, not a number(NaN).

It can be created in following ways
1.       Using boolean literal
2.       Using the Boolean object as function
3.       Using the testable expressions

Using Boolean literal
It uses true or false, to inherit the member of the Boolean object.
var bool=true;
document.write(bool);
document.write(bool.toString());

Using Boolean object as function
They are used to pass the value as an argument.
var bool=Boolean(false);

Using testable expressions
They are used to evaluate the output in Boolean values.
If(true)
{
}

Properties of Boolean object
Property
Description
constructor
Returns the function, which has created the prototype of the Boolean object
prototype
Allows us to add properties and methods to an object

Methods of the Boolean object

Method
Description
toString()
Converts the Boolean value into string and returns the string
valueOf()
Returns the primitive value of an Boolean object

Example
<!DOCTYPE html>
<html>
<head>
    <title>JavaScript Boolean Objects</title>
</head>
<body>
    <h1>JavaScript Boolean Objects</h1>
    <script>
        var bool1 = new Boolean(0);
        var bool2 = new Boolean(7);
        var bool3 = new Boolean('');
        var bool4 = new Boolean('One');
        var bool5 = new Boolean('false');
        var bool6 = new Boolean(null);
        var bool7 = new Boolean(NaN);
        document.write('0 value is :' + bool1 + '<br/>');
        document.write('5 value is :' + bool2 + '<br/>');
        document.write('empty string value is :' + bool3 + '<br/>');
        document.write('One value is :' + bool4 + '<br/>');
        document.write('false value is :' + bool5 + '<br/>');
        document.write('null value is :' + bool6 + '<br/>');
        document.write('NaN value is :' + bool7 + '<br/>');
    </script>
</body>
</html>


0 comments:

Post a Comment