JavaScript

coding learning websites codepractice

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JS Booleans


📘 JavaScript Booleans – True or False Values in Programming

In JavaScript, a Boolean represents one of two values:

  • true

  • false

These values are used to control program logic in:

  • if/else conditions

  • Loops

  • Comparisons

  • Logical operations (&&, ||, !)


🔹 Declaring Boolean Values

let isOnline = true;
let isLoggedIn = false;

They can be assigned directly or returned from expressions:

let x = 10 > 5;    // true

🔹 Boolean as Return Values

function isAdult(age) {
  return age >= 18;
}

console.log(isAdult(20)); // true

Functions often return Boolean values to indicate conditions.


🔹 JavaScript Boolean Function

You can convert any value to Boolean using the Boolean() function:

Boolean(0);         // false
Boolean(1);         // true
Boolean("");        // false
Boolean("hello");   // true
Boolean(null);      // false

🔹 Truthy vs Falsy Values

Truthy values:
  • Non-empty strings: "hello"

  • Non-zero numbers: -1, 3.14

  • Objects and arrays: [], {}

Falsy values:
  • false

  • 0

  • "" (empty string)

  • null

  • undefined

  • NaN

if ("hello") {
  console.log("This is truthy");
}

if (0) {
  console.log("This won't run");
}

🔹 Boolean in Conditions

Booleans are mostly used inside if, while, and other conditions:

let loggedIn = false;

if (!loggedIn) {
  console.log("Please log in.");
}

Practice Questions

Q1. How do you declare a Boolean variable in JavaScript?

Q2. What value does the expression 10 > 5 return in JavaScript?

Q3. Which function is used to convert any value into a Boolean?

Q4. What will Boolean("0") return? Why?

Q5. How do you check if a user is not logged in using a Boolean variable?

Q6. Which values are considered falsy in JavaScript?

Q7. What will be the Boolean value of an empty array ([])?

Q8. How do you return a Boolean from a function checking if a number is even?

Q9. What happens when you use a Boolean inside an if statement?

Q10. How can you convert a number to Boolean using double negation (!!)?


JavaScript

online coding class codepractice

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

Go Back Top