2018-09-01 22:35:20 +02:00
|
|
|
---
|
|
|
|
title: Booleans
|
2019-03-05 01:24:31 +01:00
|
|
|
introduction: >
|
|
|
|
Booleans are the logical values `true` and `false`. In addition their literal
|
|
|
|
forms, booleans are returned by [equality](../operators/equality) and
|
|
|
|
[relational](../operators/relational) operators, as well as many built-in
|
2019-09-03 01:32:14 +02:00
|
|
|
functions like [`math.comparable()`](../modules/math#comparable) and
|
|
|
|
[`map.has-key()`](../modules/map#has-key).
|
2018-09-01 22:35:20 +02:00
|
|
|
---
|
|
|
|
|
|
|
|
<% example(autogen_css: false) do %>
|
2019-09-03 01:32:14 +02:00
|
|
|
@use "sass:math";
|
|
|
|
|
2018-10-23 22:42:40 +02:00
|
|
|
@debug 1px == 2px; // false
|
|
|
|
@debug 1px == 1px; // true
|
|
|
|
@debug 10px < 3px; // false
|
2019-09-03 01:32:14 +02:00
|
|
|
@debug math.comparable(100px, 3in); // true
|
2018-10-23 22:42:40 +02:00
|
|
|
===
|
2020-10-09 19:55:09 +02:00
|
|
|
@use "sass:math"
|
2019-09-03 01:32:14 +02:00
|
|
|
|
2018-10-23 22:42:40 +02:00
|
|
|
@debug 1px == 2px // false
|
|
|
|
@debug 1px == 1px // true
|
|
|
|
@debug 10px < 3px // false
|
2019-09-03 01:32:14 +02:00
|
|
|
@debug math.comparable(100px, 3in) // true
|
2018-09-01 22:35:20 +02:00
|
|
|
<% end %>
|
|
|
|
|
|
|
|
You can work with booleans using [boolean operators][]. The `and` operator
|
|
|
|
returns `true` if *both* sides are `true`, and the `or` operator returns `true`
|
|
|
|
if *either* side is `true`. The `not` operator returns the opposite of a single
|
|
|
|
boolean value.
|
|
|
|
|
|
|
|
[boolean operators]: ../operators/boolean
|
|
|
|
|
|
|
|
<% example do %>
|
2018-10-23 22:42:40 +02:00
|
|
|
@debug true and true; // true
|
|
|
|
@debug true and false; // false
|
2018-09-01 22:35:20 +02:00
|
|
|
|
2018-10-23 22:42:40 +02:00
|
|
|
@debug true or false; // true
|
|
|
|
@debug false or false; // false
|
2018-09-01 22:35:20 +02:00
|
|
|
|
2018-10-23 22:42:40 +02:00
|
|
|
@debug not true; // false
|
|
|
|
@debug not false; // true
|
|
|
|
===
|
|
|
|
@debug true and true // true
|
|
|
|
@debug true and false // false
|
2018-09-01 22:35:20 +02:00
|
|
|
|
2018-10-23 22:42:40 +02:00
|
|
|
@debug true or false // true
|
|
|
|
@debug false or false // false
|
2018-09-01 22:35:20 +02:00
|
|
|
|
2018-10-23 22:42:40 +02:00
|
|
|
@debug not true // false
|
|
|
|
@debug not false // true
|
2018-09-01 22:35:20 +02:00
|
|
|
<% end %>
|
|
|
|
|
|
|
|
## Using Booleans
|
|
|
|
|
|
|
|
You can use booleans to choose whether or not to do various things in Sass. The
|
|
|
|
[`@if` rule][] evaluates a block of styles if its argument is `true`:
|
|
|
|
|
|
|
|
[`@if` rule]: ../at-rules/control/if
|
|
|
|
|
|
|
|
<%= partial 'code-snippets/example-if' %>
|
|
|
|
|
2020-08-26 18:26:00 +02:00
|
|
|
The [`if()` function][] returns one value if its argument is `true` and
|
2018-09-01 22:35:20 +02:00
|
|
|
another if its argument is `false`:
|
|
|
|
|
2019-09-03 00:20:24 +02:00
|
|
|
[`if()` function]: ../modules#if
|
2018-09-01 22:35:20 +02:00
|
|
|
|
|
|
|
<% example(autogen_css: false) do %>
|
2018-10-23 22:42:40 +02:00
|
|
|
@debug if(true, 10px, 30px); // 10px
|
|
|
|
@debug if(false, 10px, 30px); // 30px
|
|
|
|
===
|
|
|
|
@debug if(true, 10px, 30px) // 10px
|
|
|
|
@debug if(false, 10px, 30px) // 30px
|
2018-09-01 22:35:20 +02:00
|
|
|
<% end %>
|
|
|
|
|
|
|
|
<%= partial 'documentation/snippets/truthiness-and-falsiness' %>
|