• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / Basics / JavaScript Booleans Explained

JavaScript Booleans Explained

JavaScript Booleans Explained

A boolean value is one that can either be TRUE or FALSE. If you need to know “yes” or “no” about something, then you would want to use the boolean function. Anything that needs to be “on” or “off”, “yes” or “no”, “true” or “false”, or which just has a temporary purpose, is usually a good fit for booleans.

here is an example of how to use booleans in JavaScript:

var kitchenLights = false;
kitchenLights = true;
kitchenLights;

// Output
true

In this example, the variable “kitchenLights” being set to “true” would indicate that the lights are on. If it was set to “false” then that would mean they are off. 

It’s useful to store booleans in variables to keep track of their values and change them over time. Booleans are used as functions to get the values of variables, objects, conditions, and expressions. In fact, Booleans are critical for conditionals to work. 

In your code, when you need to know whether or not a condition is being met before proceeding to the next step, the boolean function becomes your best friend. If such and such is TRUE, then do this. If it is FALSE, then do something else.

Source

https://www.javascript.com/learn/booleans

Basics

Related Snippets:

  • Bitwise Operators in JavaScript
  • The Difference Between “var” and “let” in JavaScript
  • The onblur Event in JavaScript
  • Compress For Loops

Primary Sidebar

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2025 JavaScriptSource.com

  • About
  • Privacy Policy
  • FAQ
  • Jobs For Developers