Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Js Booleans in JavaScript

Js Booleans in JavaScript

? JavaScript Booleans


What is a Boolean?

  • A Boolean represents a logical entity and can have two values:

    • true

    • false


Creating Boolean Values

let isActive = true;let isLoggedIn = false;

Boolean in Expressions

  • Results from comparisons or logical operations.

console.log(5 > 3);  // trueconsole.log(10 === "10"); // false

Boolean Conversion (Truthy and Falsy)

Values convert to Boolean automatically in conditions:

Falsy ValuesAll Other Values Are Truthy
false"hello" (non-empty string)
042
"" (empty string){} (object)
null[] (array)
undefinedfunction() {}
NaNAny non-zero number

Example:

if ("") {  console.log("This won't run");  // Because "" is falsy}if ("hello") {  console.log("This will run");  // Because non-empty string is truthy}

Boolean Object vs Boolean Primitive

  • Avoid using new Boolean(). It creates an object, which is always truthy, even if false is stored inside.

const b1 = new Boolean(false);console.log(b1);          // [Boolean: false]if (b1) {  console.log("Runs");    // This runs, because objects are truthy!}

Use boolean primitives (true or false) instead.


Logical Operators with Booleans

  • && (AND)

  • || (OR)

  • ! (NOT)

console.log(true && false); // falseconsole.log(true || false); // trueconsole.log(!true);         // false

Want examples on truthy/falsy in conditions or logical operator tricks?

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql