05 Boolean

A boolean is a data type that can have only two values:

  • true
  • false

Booleans are used when the answer to a question is simply Yes or No.

For example:

  • Is the user logged in?
  • Has the payment been completed?
  • Is the light on?

The answer to each of these questions is either true or false.

Valid Boolean Values

{
    "isStudent": true,
    "hasHomework": false
}

In this example:

  • "isStudent": true means the person is a student.
  • "hasHomework": false means the person does not have homework.

Rules for JSON Booleans

Rule 1: A Boolean Can Only Be true or false

JSON allows only these two boolean values.

✅ Correct

{
    "isOnline": true
}

✅ Correct

{
    "isOnline": false
}

❌ Wrong

{
    "isOnline": "true"
}

Why is it wrong? "true" is a string (text) because it is inside quotes. A boolean should not be written inside quotation marks.

Rule 2: Do Not Use Quotes Around Boolean Values

Boolean values must be written without double quotes.

✅ Correct

{
    "isAvailable": false
}

❌ Wrong

{
    "isAvailable": "false"
}

Why is it wrong? "false" is treated as text, not as a boolean value.

Rule 3: Use Lowercase Letters

The words true and false must always be written in lowercase.

✅ Correct

{
    "isMember": true
}

❌ Wrong

{
    "isMember": True
}

❌ Wrong

{
    "isMember": FALSE
}

Why is it wrong? JSON only accepts the lowercase words true and false.

Summary

Boolean ValueMeaning
trueYes, correct, or enabled
falseNo, incorrect, or disabled

Remember

  • ✅ A boolean can only be true or false.
  • ✅ Write booleans without quotation marks.
  • ✅ Always use lowercase letters.
  • "true" and "false" are strings, not booleans.