All posts

Check if a value is present and is not a blank string on one line in JavaScript

To check if a reference is present and does not contain a blank string in JavaScript reference the following code snippet:

(options.override_text && options.override_text !== "") ? "some-className" : "other-className"

The first part of the code snippet checks to see if there is a truthy value for options.override_text, if that Boolean expression is true the second part of the logical conjunction (&&) checks to make sure that if it is a string, that string is not blank.

Postscript:

I built two products to help fund continued efforts for this website, it would mean a lot if you would check them out:
1. 2500+ Words for the Undermotivated Software Engineer - A short essay I wrote on the titled topic ($3).
2. Build 3 Chrome Extensions in Under 2 Hours - A course I built to teach chrome extension development ($9-20).