Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • JavaScript - Get Started
  • What is JavaScript
  • Setup JavaScript Dev Environment
  • HTML <script> Tag
  • JavaScript - Syntax
  • JavaScript - Popup Message
  • JavaScript - Variables
  • JavaScript - Operators
  • JavaScript - Data Types
  • JavaScript - String
  • JavaScript - Numbers
  • JavaScript - Boolean
  • JavaScript - Object
  • JavaScript - Date
  • JavaScript - Date Methods
  • JavaScript - Array
  • JavaScript - Array Methods
  • JavaScript - null and undefined
  • JavaScript - Function
  • JavaScript - if condition
  • JavaScript - switch
  • JavaScript - for loop
  • JavaScript - while loop
  • JavaScript - Scope
  • JavaScript - eval
  • JavaScript - Error Handling
  • JavaScript - strict mode
  • JavaScript - Hoisting
  • Define JS Class
  • JS Object In Depth
  • this Keyword
  • new Keyword
  • Prototype
  • Inheritance
  • Closure
  • IIFE
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

JavaScript Booleans

The boolean (not Boolean) is a primitive data type in JavaScript. It can have only two values: true or false. It is useful in controlling program flow using conditional statements like if else, switch, while loop, etc.

The followings are boolean variables.

Example: boolean Variables
var YES = true;

var NO = false;
Try it

The following example demonstrates how a boolean value controls the program flow using the if condition.

Example: Boolean
var YES = true;
var NO = false;

if(YES)
{
    alert("This code block will be executed");
}

if(NO)
{
    alert("This code block will not be executed");
}
Try it

The comparison expressions return boolean values to indicate whether the comparison is true or false. For example, the following expressions return boolean values.

Example: boolean Expressions
var a = 10, b = 20;

var result = 1 &gt; 2; // false

result = a < b; // true

result = a > b; // false

result = a + 20 &gt; b + 5; // true
Try it

Boolean Function

JavaScript provides the Boolean() function that converts other types to a boolean type. The value specified as the first parameter will be converted to a boolean value. The Boolean() will return true for any non-empty, non-zero, object, or array.

Example: Boolean() Function
var a = 10, b = 20;

var b1 = Boolean('Hello'); // true

var b2 = Boolean('h'); // true

var b3 = Boolean(10); // true

var b4 = Boolean([]); // true

var b5 = Boolean(a + b); // true
Try it

If the first parameter is 0, -0, null, false, NaN, undefined, '' (empty string), or no parameter passed then the Boolean() function returns false.

Example: Boolean() Function
var b1 = Boolean(''); // false

var b2 = Boolean(0); // false

var b3 = Boolean(null); // false

var a;
var b4 = Boolean(a); // false
Try it

The new operator with the Boolean() function returns a Boolean object.

Example: Boolean Object
var bool = new Boolean(true);

alert(bool); // true
Try it

Any boolean object, when passed in a conditional statement, will evaluate to true.

Example: Boolean Object in Condition
var bool = new Boolean(false);

if(bool){
    alert('This will be executed.');
}
Try it

Boolean vs boolean

The new Boolean() will return a Boolean object, whereas it returns a boolean without the new keyword. The boolean (lower case) is the primitive type, whereas Boolean (upper case) is an object in JavaScript. Use the typeof operator to check the types.

Example: Boolean vs boolean
var b1 = new Boolean(true);
var b2 = true;

typeof b1; // object
typeof b2; // boolean
Try it

Boolean Methods

Primitive or Boolean object includes following methods.

MethodDescription
toLocaleString()Returns string of boolean value in local browser environment.

Example:var result = (1 > 2); result.toLocaleString(); // returns "false"
toString()Returns a string of Boolean.

Example:var result = (1 > 2); result.toString(); // returns "false"
valueOf()Returns the value of the Boolean object.

Example:var result = (1 > 2); result.valueOf(); // returns false
TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.