Wednesday, September 17, 2025
HomeLanguagesJavascriptLess.js Lazy Variables Evaluation

Less.js Lazy Variables Evaluation

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 boosts the functionality of CSS. Cross-browser interoperability is offered via LESS. CSS is improved and compiled for usage by web browsers using a programming language called CSS pre-processor. Additionally, it is a CSS language extension that offers tools like variables, functions, mixins, and operations that enable us to construct dynamic CSS while yet maintaining backward compatibility.

The LESS Variables, as they are known to hold values stored in them and can be used anywhere within the definition of code, js govern the common values used in a single area. These variables can be used with LESS to alter a certain value throughout the entire code. When we need to modify a certain value in our CSS code, it may become challenging, but using variables makes it simple.

Lazy Variables Evaluation means that the variables in Less do not need to be declared before being used. When a variable is defined more than once, the most recent definition is used, working up from the current scope. This is analogous to how CSS works, where the value of the last property inside a definition is determined.

Syntax:

.sel {
    property: @var2;
}

@var2: @var1;
@var1: value;

There is no specific parameter for this functionality.

To know how to Compile LESS code into CSS code open this. 

Example 1: The example given below demonstrates how the usage of a @cond variable and using it in a boolean function before defining or even declaring it.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <link rel="stylesheet" type="text/css" 
          href="style.css"/>
</head>
  
<body>
    <h1 style="color:green">neveropen</h1>  
    <h3><b>Less.js Lazy Variables Evaluation</b></h3>
    <div class="c1">  
        <p class="p1">
            <strong>
                neveropen
            </strong>
        </p>
    </div>
</body>
  
</html>


style.less:

CSS




@body-bg-color: #eeeeee;
@dark: hsl(25, 71%, 8%);
@light: rgb(253, 255, 146);
@val: 30%;
body {
    background-color: @body-bg-color;
}
.c1 {
    width:  @val;
    height: 250px;
    margin: 1rem;
    background-color: if(@cond, @dark, @light);
}
.p1 {
    padding: 100px 0px 0px 140px;
    color: if(@cond, @light, @dark);
    font-size: @val * 5;
}
@cond: boolean(ispercentage(@val));


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

lessc style.less style.css

The compiled CSS file comes to be:

style.css:

CSS




body {
      background-color: #eeeeee;
}
.c1 {
     width: 30%;
      height: 250px;
      margin: 1rem;
      background-color: hsl(25, 71%, 8%);
}
.p1 {
      padding: 100px 0px 0px 140px;
      color: #fdff92;
      font-size: 150%;
}


Output:

 

Example 2: The code below demonstrates how the final value assigned to a variable is the main value that is allotted to the variable after the code is compiled in Lazy Evaluation.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <link rel="stylesheet" type="text/css" 
          href="style.css"/>
</head>
  
<body>
    <h1 style="color:green">neveropen</h1>  
    <h3><b>Less.js Lazy Variables  Evaluation</b></h3>
    <div class="c1">  
        <p class="p1">
            <strong>
                neveropen
            </strong>
        </p>
    </div>
</body>
  
</html>


styles.less:

CSS




@body-bg-color: #eeeeee;
@dark: hsl(25, 71%, 8%);
@light: rgb(253, 255, 146);
@val: 30%;
body {
    background-color: @body-bg-color;
}
.c1 {
    width:  450px;
    height: @val;
    margin: 1rem;
    background-color: if(@cond, @dark, @light);
}
.p1 {
    padding: 100px 0px 0px 140px;
    color: if(@cond, @light, @dark);
    font-size: @val * 0.1;
}
@cond: boolean(ispercentage(@val));
@val: 15rem;


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

lessc styles.less style.css

The compiled CSS file comes to be:

style.css:

CSS




body {
      background-color: #eeeeee;
}
.c1 {
      width: 450px;
      height: 15rem;
      margin: 1rem;
      background-color: #fdff92;
}
.p1 {
      padding: 100px 0px 0px 140px;
      color: hsl(25, 71%, 8%);
      font-size: 1.5rem;
}


Output:

 

Reference: https://lesscss.org/features/#variables-feature-lazy-evaluation 

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!
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32299 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6660 POSTS0 COMMENTS
Nicole Veronica
11833 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11895 POSTS0 COMMENTS
Shaida Kate Naidoo
6779 POSTS0 COMMENTS
Ted Musemwa
7050 POSTS0 COMMENTS
Thapelo Manthata
6735 POSTS0 COMMENTS
Umr Jansen
6741 POSTS0 COMMENTS