Friday, October 17, 2025
HomeLanguagesJavascriptLess.js Logical boolean() Function

Less.js Logical boolean() Function

Less.js is a simple CSS pre-processor that facilitates the creation of manageable, customizable, and reusable style sheets for websites. It is a dynamic style sheet language that improves CSS’s functionality. Cross-browser interoperability is supported by LESS. A scripting language known as CSS pre-processor is used to enhance CSS and compile it into standard CSS syntax for use by web browsers. Additionally, it is a language extension for CSS that supports backward compatibility and offers functionality like variables, functions, mixins, and operations that let us create dynamic CSS.

Less.js Logical boolean() Function receives a condition and, depending on it, outputs True or False. Generally, you can use this method to save a condition’s result in a variable so that you can utilize it later, as needed, in an “if” function.

Syntax:

boolean(condition);

 

Parameters:

  • condition: This is the parameter that is compulsory for the boolean function, this condition is evaluated and the result is returned.

Example 1: The code below demonstrates how we can use the boolean Logical function in Less.js and we can pair it with another LESS function to produce an output.

index.html




<!DOCTYPE html>
<html>
  
<head>
    <link rel="stylesheet" 
          type="text/css" 
          href="style.css"/>
</head>
  
<body>
    <h1>neveropen</h1>
    <h3>Less.js Logical boolean() Function</h3>
</body>
  
</html>


styles.less




@body-bg-color: #eeeeee;
@text-color: rgb(70, 172, 121);
@button-bg-color: boolean(color(@body-bg-color) = #a7a7a7);
  
body {
    background: @body-bg-color;
}
  
h3{
    color: if(@button-bg-color, grey, green);
}


Now, to compile the above LESS code to CSS code, run the following command:

lessc styles.less styles.css

The compiled CSS file comes to be: 

style.css




body {
      background: #eeeeee;
}
h3 {
      color: green;
}


Output:

 

Example 2: The code below demonstrates how we can use multiple boolean Logical functions in Less.js and we can pair them with various LESS functions to produce an output.

index.html




<!DOCTYPE html>
<html>
  
<head>
    <link rel="stylesheet" type="text/css" href="style.css"/>
</head>
  
<body>
    <h1>neveropen</h1>
    <h3>Less.js Logical boolean() Function</h3>
    <div class="container">
    </div>
</body>
  
</html>


styles.less




/* styles.less */
@body-bg-color: #eeeeee;
@text-color: rgb(71, 167, 119);
@container-bg: rgb(220, 43, 55);
@cond1: boolean(luma(@body-bg-color) > 50%);
@cond2: boolean(luma(@container-bg) > 50%);
  
body {
    background: @body-bg-color;
}
  
h3{
    color: if(@cond2, grey, green);
}
.container{
    width: 15rem;
    height: 9rem;
    background-color: if(@cond1, @container-bg, @button-bg-color);
}


Now, to compile the above LESS code to CSS code, run the following command:

lessc styles.less styles.css

The compiled CSS file comes to be: 

style.css




body {
      background: #eeeeee;
}
h3 {
      color: green;
}
.container {
      width: 15rem;
      height: 9rem;
      background-color: #dc2b37;
}


Output:

 

Reference: https://lesscss.org/functions/#logical-functions-boolean 

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS