Sass Reference - - By Hugo Giraudel

Bools

Bools, often referred to as booleans, are specific values that exist in almost any programming language in the world. There are only two boolean values: true and false (written slightly differently depending on the language).

The not keyword

Sass does not have the bang operator (!) like other languages to emit a negative condition, such as if (!value). Instead, it provides the not keyword which works sensibly the same.

$bool: false;

@if not $bool {
    // We get in there
}

Along the same lines, while being a bad pattern from C world, the very popular “bang bang you’re a boolean” technique to coerce a value to a bool (!!value) is doable in Sass by chaining two not keywords.

$value: 'Hello world!';
$bool: not not $value; // true

A falsy value would return false, while a truthy value would return true. In Sass, only two values are falsy: false and null, so basically anything else would return true.

Example

A couple of native Sass functions returns a bool, for instance the unitless function that checks whether the given number has a unit or not.

$value: 42em;
$unitless: unitless($value); // false

Similarly, all *-exists functions return a bool. Taking variable-exists as an example:

$unicorn: 'rainbow';
$variable-exists: variable-exists('unicorn'); // true

Beware: when testing for the existence of a variable, you have to pass the variable name as a string to the variable-exists function. If you pass an actual variable, it will get evaluated, resulting in an error if it actually does not exist.

Engine compatibility

Bools are fully compatible across all Sass engines and there is no known bug to this day about their implementation.

Further Reading