Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

Sass Introspection

Sass variable-exists() Function

Posted in Sass Tutorial
Updated on Sep 30, 2024
By Mari Selvan
👁ī¸ 11 - Views
âŗ 4 mins
đŸ’Ŧ 1 Comment
Sass variable-exists() Function

Photo Credit to CodeToFun

🙋 Introduction

The variable-exists() function in Sass is a utility that allows you to check whether a variable has been defined in your stylesheet.

This function is particularly useful when you want to ensure that a variable exists before attempting to use or manipulate it, which can help prevent errors and improve the robustness of your code.

💡 Syntax

The syntax for the variable-exists() function is simple and takes one argument:

Syntax
Copied
Copy To Clipboard
variable-exists(name)

đŸ”ĸ Parameters

  • name: A string representing the name of the variable you wish to check. The variable name should be provided without the $ symbol.

↩ī¸ Return Value

The function returns a Boolean value:

  • true: if the variable is defined.
  • false: if the variable is not defined.

📝 Example Usage

Let's explore some examples to understand how variable-exists() can be used in different scenarios.

📜 Example 1: Basic Check

example.scss
Copied
Copy To Clipboard
$primary-color: #3498db;

@if variable-exists(primary-color) {
  .button {
    background-color: $primary-color;
  }
} else {
  .button {
    background-color: #ccc;
  }
}

In this example, the variable-exists() function checks if $primary-color is defined. If it is, the button's background color is set to the value of $primary-color; otherwise, a fallback color of #ccc is used.

📜 Example 2: Handling Undefined Variables

example.scss
Copied
Copy To Clipboard
@if variable-exists(secondary-color) {
  .header {
    color: $secondary-color;
  }
} else {
  .header {
    color: #333;
  }
}

Here, variable-exists() checks for the existence of $secondary-color. If the variable is not defined, a default color of #333 is applied.

📜 Example 3: Using in Mixins

example.scss
Copied
Copy To Clipboard
@mixin apply-color($color-name) {
  @if variable-exists($color-name) {
    color: variable-exists($color-name);
  } else {
    color: inherit;
  }
}

$header-color: #e74c3c;

.header {
  @include apply-color('header-color');
}

In this example, a mixin named apply-color checks if a variable exists before applying it. If the variable doesn't exist, it falls back to the inherited color.

🎉 Conclusion

The variable-exists() function in Sass is an essential tool for writing defensive code. helps ensure your code remains reliable.

By incorporating variable-exists() into your workflow, you can handle undefined variables gracefully, making your Sass projects more resilient to changes and easier to debug. Experiment with this function to see how it can enhance the flexibility and stability of your stylesheets.

👨‍đŸ’ģ Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy