JavaScript

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

JavaScript

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 (!!)?


JS Booleans Quiz

Q1: Which of the following is a Boolean value in JavaScript?

A. "true"
B. 1
C. true
D. "false"

Q2: What will Boolean(0) return?

A. true
B. "false"
C. 0
D. false

Q3: What is the output of: Boolean("hello")?

A. false
B. true
C. "hello"
D. null

Q4: Which of these is considered falsy?

A. "0"
B. 0
C. []
D. {}

Q5: What is the Boolean result of Boolean(null)?

A. true
B. false
C. null
D. undefined

Q6: What does the ! (not) operator do in JavaScript?

A. Adds 1
B. Converts to Boolean
C. Inverts a Boolean value
D. Assigns true

Q7: What is the output of !!"JavaScript"?

A. true
B. false
C. "JavaScript"
D. Error

Q8: Which of the following values is truthy?

A. ""
B. null
C. undefined
D. "false"

Q9: Which operator is used to check if a Boolean variable is false?

A. if (var)
B. if (!var)
C. if (== false)
D. if (var = false)

Go Back Top