Teachnique
      CourseRoadmaps
      Login

      OverviewPlacementSyntaxHello WorldConsole.log()CommentsVariableslet StatementConstantsData TypesType ConversionsStrict ModeReserved Keywords

      OperatorsArithmetic OperatorsComparison OperatorsLogical OperatorsBitwise OperatorsAssignment OperatorsConditional Operatorstypeof OperatorNullish Coalescing OperatorDelete OperatorComma OperatorGrouping OperatorYield OperatorSpread OperatorExponentiation OperatorOperator Precedence

      If...ElseWhile LoopsFor LoopFor...in LoopFor...of LoopLoop ControlBreak StatementContinue StatementSwitch CaseUser Defined Iterators

      FunctionsFunction ExpressionsFunction ParametersDefault ParametersFunction() ConstructorFunction HoistingArrow FunctionsFunction InvocationFunction call() MethodFunction apply() MethodFunction bind() MethodClosuresVariable ScopeGlobal VariablesSmart Function Parameters

      NumberBooleanStringsArraysDateMathRegExpSymbolSetsWeakSetMapsWeakMapIterablesReflectTypedArrayTempate LiteralsTagged Templates

      Objects OverviewClassesObject PropertiesObject MethodsStatic MethodsDisplay ObjectsObject AccessorsObject ConstructorsNative PrototypesES5 Object MethodsEncapsulationInheritanceAbstractionPolymorphismDestructuring AssignmentObject DestructuringArray DestructuringNested DestructuringOptional ChainingGlobal ObjectMixinsProxies

      HistoryVersionsES5ES6ECMAScript 2016ECMAScript 2017ECMAScript 2018ECMAScript 2019ECMAScript 2020ECMAScript 2021ECMAScript 2022

      CookiesCookie AttributesDeleting Cookies

      Browser Object ModelWindow ObjectDocument ObjectScreen ObjectHistory ObjectNavigator ObjectLocation ObjectConsole Object

      Web APIHistory APIStorage APIForms APIWorker APIFetch APIGeolocation API

      EventsDOM Events

      Feedback

      Submit request if you have any questions.

      Course
      Boolean

      JavaScript Tutorial

      This JavaScript tutorial is crafted for beginners to introduce them to the basics and advanced concepts of JavaScript. By the end of this guide, you'll reach a proficiency level that sets the stage for further growth. Aimed at empowering you to progress towards becoming a world-class software developer, this tutorial paves the way for a successful career in web development and beyond.

      Boolean Object

      The JavaScript Boolean object represents two values, either "true" or "false". You can create a boolean object using the Boolean() constructor with a 'new' keyword. It takes a value as parameter and returns a boolean object. If value parameter is omitted or is 0, -0, null, false, NaN, undefined, or the empty string (""), the object has an initial value of false. In programming, the if-else statement evaluates either the code of the 'if' block or the 'else' block based on the boolean value of the conditional expression.

      Syntax

      Use the following syntax to create a boolean object.
      const val = new Boolean(value);
      Here value is an expression to convert into a Boolean object.
      It returns an object containing the boolean value.
      You can create a boolean primitive in JavaScript by assigning a boolean value to a variable
      let bool = true;

      Boolean Properties

      Here is a list of the properties of Boolean object
      Sr.No.
      Property & Description
      1
      AI
      Returns a reference to the Boolean function that created the object.
      2
      AI
      The prototype property allows you to add properties and methods to an object.
      In the following sections, we will have a few examples to illustrate the properties of Boolean object.

      Boolean Methods

      Here is a list of the methods of Boolean object and their description.
      Sr.No.
      Method & Description
      1
      AI
      Returns a string containing the source of the Boolean object; you can use this string to create an equivalent object.
      2
      AI
      Returns a string of either "true" or "false" depending upon the value of the object.
      3
      AI
      Returns the primitive value of the Boolean object.
      In the following sections, we will have a few examples to demonstrate the usage of the Boolean methods.

      Example: Creating a Boolean Object

      In the example below, we have defined the boolObj variable and stored the boolean object.
      We used the typeof operator to check the type boolObj variable. In the output, you can observe that the type of the boolObj is the object.
      <html>
      <body>
      <p id = "output"> </p>
      <script>
      const boolObj = new Boolean('true'); //defining boolean object
      document.getElementById("output").innerHTML = "typof boolObj == " + typeof boolObj;
      </script>
      </body>
      </html>

      Output

      typof boolObj == object

      JavaScript Boolean() Function

      The Boolean() function allows the developer to get the boolean value after evaluating the particular expression passed as a parameter.
      Boolean(Expression);
      Here value is an expression to evaluate and get related boolean values. The Boolean() function returns the true or false based on the expression passed as a parameter.

      Example

      In the example below, We used the Boolean() function and passed different expressions as a parameter. In the output, you can observe the boolean value returned by the Boolean() function.
      <html>
      <body>
      <p id = "output"> </p>
      <script>
      let res = Boolean(100 > 90);
      document.getElementById("output").innerHTML = "Boolean(100 > 90) : " + res + "<br>";
      res = Boolean(100 < 90);
      document.getElementById("output").innerHTML = "Boolean(100 < 90) : " + res + "<br>";
      res = 100 == 90;
      document.getElementById("output").innerHTML = "100 == 90 : " + res + "<br>";
      </script>
      </body>
      </html>

      Output

      Boolean(100 > 90) : true
      Boolean(100 < 90) : false
      100 == 90 : false

      JavaScript Falsy Boolean Values

      The Boolean() function returns false for the falsy values. There are six falsy values –false, null, undefined, 0 (zero), "" (empty string), NaN.
      Let's look at the example below.

      Example

      In the code below, we have passed all the falsy values as a parameter of the Boolean() function and printed the returned value from the Boolean() function. The Boolean() function returns false for all 7 values.
      <html>
      <body>
      <p id = "demo"> </p>
      <script>
      document.getElementById("demo").innerHTML =
      "Boolean(0) : " + Boolean(0) + "<br>" +
      "Boolean(-0) : " + Boolean(-0) + "<br>" +
      "Boolean(null) : " + Boolean(null) + "<br>" +
      "Boolean(undefined) : " + Boolean(undefined) + "<br>" +
      "Boolean('') : " + Boolean('') + "<br>" +
      "Boolean(NaN) : " + Boolean(NaN) + "<br>" +
      "Boolean(false) : " + Boolean(false);
      </script>
      </body>
      </html>

      Output

      Boolean(0) : false
      Boolean(-0) : false
      Boolean(null) : false
      Boolean(undefined) : false
      Boolean('') : false
      Boolean(NaN) : false
      Boolean(false) : false
      All other values are truthy, and the Boolean() function returns true.

      Example

      In the below code, we passed the truthy values as a Boolean() function parameter. The Boolean() function returns true for all truthy values. Even if we have passed the object and function as a
      
      function parameter, it returns true.
      <html>
      <body>
      <p id = "demo"> </p>
      <script>
      document.getElementById("demo").innerHTML =
      "Boolean(1) : " + Boolean(1) + "<br>" +
      "Boolean(-1) : " + Boolean(-1) + "<br>" +
      "Boolean('Hello') : " + Boolean('Hello') + "<br>" +
      "Boolean(true) : " + Boolean(true) + "<br>" +
      "Boolean(10.99) : " + Boolean(10.99) + "<br>" +
      "Boolean({name: 'John'}) : " + Boolean({ name: 'John' }) + "<br>" +
      "Boolean(() => {return 1;}) : " + Boolean(() => { return 1; });
      </script>
      </body>
      </html>

      Output

      Boolean(1) : true
      Boolean(-1) : true
      Boolean('Hello') : true
      Boolean(true) : true
      Boolean(10.99) : true
      Boolean({name: 'John'}) : true
      Boolean(() => {return 1;}) : true