DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • Future of Software Development: Generative AI Augmenting Roles and Unlocking Co-Innovation
  • Understanding Data Compaction in 3 Minutes
  • How to Use an Anti-Corruption Layer Pattern for Improved Microservices Communication
  • Five Java Books Beginners and Professionals Should Read

Trending

  • Future of Software Development: Generative AI Augmenting Roles and Unlocking Co-Innovation
  • Understanding Data Compaction in 3 Minutes
  • How to Use an Anti-Corruption Layer Pattern for Improved Microservices Communication
  • Five Java Books Beginners and Professionals Should Read
  1. DZone
  2. Coding
  3. Java
  4. Clojure: Truthy and Falsey

Clojure: Truthy and Falsey

Jay Fields user avatar by
Jay Fields
·
Feb. 28, 11 · Interview
Like (0)
Save
Tweet
Share
6.31K Views

Join the DZone community and get the full member experience.

Join For Free
Truthy and Falsey are important concepts in Clojure. The best description I've seen is in The Joy of Clojure, go buy it.

Depending on your language background you may have very different ideas on what evaluates to true and what evaluates to false. In Clojure, the answer to this is very straightforward.
Both nil and false are treated as "false" and everything else is treated as true.
Let's hit the REPL.
user=> (if true "yes" "no")
"yes"
user=> (if false "yes" "no")
"no"
Okay, that much should be obvious. However, it's important to note that nil is also considered false, which is why we say:
In Clojure nil and false are considered "false" and therefore we say they are both "falsey".
Another quick trip to the REPL to verify that nil is falsey.
user=> (if nil "yes" "no")  
"no"
As expected, the else is evaluated.

There are other values that are considered false in other languages, this does not apply to clojure.
All non-falsey values are considered "truthy" and evaluate as such.
Here are some common examples of truthy values in Clojure that are falsey in other languages.
user=> (if -1 "still true" "false")
"still true"
user=> (if 0 "still true" "false") 
"still true"
user=> (if [] "still true" "false")
"still true"
user=> (if (list) "still true" "false")
"still true"
As it says in The Joy of Clojure: Every[thing] is "true" all the time, unless it is nil or false.

From http://blog.jayfields.com/2011/02/clojure-truthy-and-falsey.html

Clojure

Opinions expressed by DZone contributors are their own.

Trending

  • Future of Software Development: Generative AI Augmenting Roles and Unlocking Co-Innovation
  • Understanding Data Compaction in 3 Minutes
  • How to Use an Anti-Corruption Layer Pattern for Improved Microservices Communication
  • Five Java Books Beginners and Professionals Should Read

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: