<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Albert Slyvester Duro</title>
    <description>The latest articles on Forem by Albert Slyvester Duro (@durosly).</description>
    <link>https://forem.com/durosly</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F632513%2Fb987642a-986a-4fe4-af8e-5e632318102e.jpeg</url>
      <title>Forem: Albert Slyvester Duro</title>
      <link>https://forem.com/durosly</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/durosly"/>
    <language>en</language>
    <item>
      <title>How to use caching to improve JavaScript performance</title>
      <dc:creator>Albert Slyvester Duro</dc:creator>
      <pubDate>Sun, 01 Jan 2023 19:34:02 +0000</pubDate>
      <link>https://forem.com/durosly/how-to-use-caching-to-improve-javascript-performance-4jl0</link>
      <guid>https://forem.com/durosly/how-to-use-caching-to-improve-javascript-performance-4jl0</guid>
      <description>&lt;p&gt;Caching the results of expensive operations can improve the performance of JavaScript code by reducing the need to recalculate the same value multiple times. Here are a few tips for implementing caching in JavaScript:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use a cache object&lt;/strong&gt;: To implement caching in JavaScript, you can create a cache object that stores the results of expensive operations as key-value pairs. When you need to perform an operation, you can check the cache object to see if the result has already been calculated. If it has, you can return the cached result, which is faster than recalculating the result.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use a cache eviction policy&lt;/strong&gt;: A cache eviction policy determines how long to keep the results of an operation in the cache. There are several strategies you can use, such as a fixed size cache that removes the least recently used (LRU) item when it reaches capacity, or a time-based cache that removes items after a certain amount of time has passed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use a hash function to generate cache keys&lt;/strong&gt;: To store the results of an operation in the cache, you'll need to use a unique key to identify the result. One option is to use a hash function to generate a unique key based on the input to the operation. This can help ensure that the same operation is not stored multiple times in the cache.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Invalidate the cache when necessary&lt;/strong&gt;: There may be times when the results of an operation are no longer valid and need to be recalculated. In these cases, you'll need to invalidate the cache so that the result is recalculated the next time the operation is performed.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;By following these tips, you can implement caching in your JavaScript code and improve its performance. It's important to note that caching is not always the best solution, and you should consider the specific needs of your application before implementing it.&lt;/p&gt;

</description>
      <category>portfolio</category>
      <category>github</category>
      <category>career</category>
      <category>learning</category>
    </item>
    <item>
      <title>How to optimize your JavaScript by reducing the number of function calls</title>
      <dc:creator>Albert Slyvester Duro</dc:creator>
      <pubDate>Sat, 31 Dec 2022 03:27:15 +0000</pubDate>
      <link>https://forem.com/durosly/how-to-optimize-your-javascript-by-reducing-the-number-of-function-calls-2o9i</link>
      <guid>https://forem.com/durosly/how-to-optimize-your-javascript-by-reducing-the-number-of-function-calls-2o9i</guid>
      <description>&lt;p&gt;Reducing the number of function calls in JavaScript can help improve the performance of your code, especially in cases where functions are called frequently. Here are a few strategies for reducing the number of function calls in JavaScript:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Avoid nesting functions&lt;/strong&gt;: Nested functions can increase the number of function calls, as each inner function must be called by the outer function. Instead of nesting functions, consider using a single function or breaking the logic into smaller, independent functions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use function arguments instead of accessing global variables&lt;/strong&gt;: If you need to pass data to a function, consider using function arguments instead of accessing global variables. Accessing global variables can be slower than passing data as an argument, and it can also make your code less modular and more difficult to maintain.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Avoid using function calls as conditionals&lt;/strong&gt;: Function calls can be slow, especially if the function performs a lot of work. Instead of using function calls as conditionals, consider using a simple comparison or Boolean expression.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use memoization&lt;/strong&gt;: Memoization is a technique that involves storing the results of expensive function calls in a cache. By using memoization, you can avoid calling the same function multiple times with the same input, which can improve the performance of your code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use function composition&lt;/strong&gt;: Function composition is a technique that involves combining multiple functions into a single function. By using function composition, you can avoid calling multiple functions and instead use a single function that performs all the necessary operations.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;By following these strategies, you can reduce the number of function calls in your JavaScript code and improve its performance. It's important to note that these techniques may not be applicable in all cases, and the best approach will depend on the specific needs of your application.&lt;/p&gt;

</description>
      <category>tutorial</category>
    </item>
    <item>
      <title>Using efficient data structures and algorithms to optimize your JavaScript</title>
      <dc:creator>Albert Slyvester Duro</dc:creator>
      <pubDate>Sat, 31 Dec 2022 03:16:38 +0000</pubDate>
      <link>https://forem.com/durosly/using-efficient-data-structures-and-algorithms-to-optimize-your-javascript-14ok</link>
      <guid>https://forem.com/durosly/using-efficient-data-structures-and-algorithms-to-optimize-your-javascript-14ok</guid>
      <description>&lt;p&gt;Using efficient data structures and algorithms can have a significant impact on the performance of JavaScript code. Choosing the right data structure or algorithm for a given task can make the difference between code that runs quickly and code that is slow and resource-intensive. Here are a few tips for using efficient data structures and algorithms in JavaScript:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Choose the right data structure&lt;/strong&gt;: Different data structures are better suited to different tasks. For example, arrays are a good choice for storing lists of items, while objects are a good choice for storing key-value pairs. Choosing the right data structure can make it easier to access and manipulate data, and can also improve the performance of your code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use built-in array methods&lt;/strong&gt;: JavaScript provides a range of built-in array methods that can be used to perform common tasks, such as sorting, filtering, and mapping. These methods can be more efficient than writing your own algorithms to perform the same tasks.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use sorting algorithms appropriately&lt;/strong&gt;: Different sorting algorithms are better suited to different situations. For example, bubble sort is a simple algorithm that is easy to understand, but it is not very efficient for large lists. On the other hand, quicksort is a more efficient algorithm, but it can be more difficult to implement. Choosing the right sorting algorithm for a given task can improve the performance of your code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use search algorithms appropriately&lt;/strong&gt;: Like sorting algorithms, different search algorithms are better suited to different situations. For example, linear search is a simple algorithm that can be used to search an unsorted list, but it is not very efficient. On the other hand, binary search is a more efficient algorithm, but it can only be used on sorted lists. Choosing the right search algorithm for a given task can improve the performance of your code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use hashing appropriately&lt;/strong&gt;: Hashing is a technique that can be used to store and retrieve data efficiently. By using a hash function to map data to a specific location in a hash table, you can perform searches and inserts in constant time. However, hash tables can be more complex to implement than other data structures, and they may not be the best choice for all tasks.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By following these tips, you can choose the most appropriate data structures and algorithms for your tasks, which can improve the performance of your JavaScript code. It's important to note that these techniques may not be applicable in all cases, and the best approach will depend on the specific needs of your application.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>5 easy ways to reduce DOM manipulations</title>
      <dc:creator>Albert Slyvester Duro</dc:creator>
      <pubDate>Fri, 30 Dec 2022 21:46:24 +0000</pubDate>
      <link>https://forem.com/durosly/5-easy-ways-to-reduce-dom-manipulations-4l29</link>
      <guid>https://forem.com/durosly/5-easy-ways-to-reduce-dom-manipulations-4l29</guid>
      <description>&lt;p&gt;DOM manipulation refers to the process of modifying the structure, content, or style of an HTML or XML document using JavaScript. While DOM manipulation is a powerful tool for building dynamic web applications, it can also be a resource-intensive operation that can negatively impact the performance of an application. In this article, we'll explore some strategies for minimizing the number of DOM manipulation operations in JavaScript.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use document fragments&lt;/strong&gt;: Document fragments are in-memory representations of a portion of a document. By using document fragments to perform multiple DOM manipulation operations at once, you can minimize the number of times the DOM is modified. To use document fragments, you can create a new fragment, append your DOM elements to the fragment, and then append the fragment to the DOM once all the elements have been added.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use CSS transitions and animations&lt;/strong&gt;: Instead of using JavaScript to animate elements on the page, consider using CSS transitions and animations. These techniques can be more efficient and performant than using JavaScript to animate elements.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Avoid repeatedly modifying the same element&lt;/strong&gt;: If you need to modify the same element multiple times, consider using a single DOM manipulation operation instead of multiple operations. For example, if you need to change the text of an element multiple times, consider using the textContent property instead of the innerHTML property.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use event delegation&lt;/strong&gt;: Event delegation is a technique that allows you to attach a single event handler to a parent element, rather than attaching individual event handlers to each child element. This can be more efficient, especially in cases where the number of child elements is large or dynamic.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use requestAnimationFrame&lt;/strong&gt;: The requestAnimationFrame function allows you to schedule DOM manipulation operations to occur at the next repaint of the browser window. This can help reduce the number of times the DOM is modified, as the operations will be grouped together and performed at once.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;By following these strategies, you can minimize the number of DOM manipulation operations in your JavaScript code and improve the performance of your application. It's important to note that these techniques may not be applicable in all cases, and the best approach will depend on the specific needs of your application.&lt;/p&gt;




&lt;p&gt;The goal is to optimize our JavaScript and greatly improve the performance of a web application.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>7 tips to measure and analyze JavaScript performance</title>
      <dc:creator>Albert Slyvester Duro</dc:creator>
      <pubDate>Fri, 30 Dec 2022 16:17:31 +0000</pubDate>
      <link>https://forem.com/durosly/7-tips-to-measure-and-analyze-javascript-performance-gc6</link>
      <guid>https://forem.com/durosly/7-tips-to-measure-and-analyze-javascript-performance-gc6</guid>
      <description>&lt;p&gt;Performance is an important consideration when developing a website or an app. Nobody wants an app that crashes or a website that doesn't load, especially when the users' waiting time is limited. According to Kissmetrics, 47% of website visitors expect a website to load in less than 2 seconds, and 40% will leave if the loading process takes more than 3 seconds.&lt;/p&gt;

&lt;p&gt;Although JavaScript is a fascinating language to work with, it can have a negative impact on the performance of your website. With that in mind, here are seven methods for monitoring the performance of your JavaScript.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use the performance-related APIs in modern browsers&lt;/strong&gt;: Many modern browsers provide APIs for measuring and analyzing the performance of JavaScript code, such as the performance object and the &lt;code&gt;console.profile()&lt;/code&gt; method.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use a JavaScript profiler&lt;/strong&gt;: A JavaScript profiler is a tool that can help identify performance bottlenecks in JavaScript code. Some popular JavaScript profilers include the Chrome DevTools Profiler and the Firefox DevTools Profiler.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use a performance monitoring service&lt;/strong&gt;: There are many services available that can help monitor the performance of JavaScript code, such as New Relic and AppDynamics. These services can provide valuable insights into the performance of an application.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use the browser's performance timeline&lt;/strong&gt;: The browser's performance timeline can provide detailed information about the performance of an application, including the time it takes for the page to load and the time it takes for individual JavaScript functions to execute.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Measure the real-world performance of an application&lt;/strong&gt;: It's important to measure the performance of an application in a real-world scenario, rather than just in a controlled environment. This can help identify performance issues that may not be apparent in a controlled setting.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use synthetic performance tests&lt;/strong&gt;: Synthetic performance tests are tests that are designed to simulate the performance of an application under specific conditions. These tests can help identify performance issues that may not be apparent in real-world usage.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use benchmarks&lt;/strong&gt;: Benchmarks are tests that measure the performance of specific code snippets or algorithms. Comparing the results of benchmarks can help identify performance issues and suggest ways to improve the performance of JavaScript code.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;It is critical to strike a balance between code readability and optimization. Computers interpret the code, but we need to ensure that it can be maintained in the future by ourselves or others, so it must be understandable.&lt;/p&gt;

&lt;p&gt;Remember that performance should always be considered, but it should never be prioritized over debugging and adding functionalities.&lt;/p&gt;

&lt;p&gt;Sometimes, people really just care about what works.&lt;/p&gt;

</description>
      <category>emptystring</category>
    </item>
    <item>
      <title>How to use Jest for JavaScript code testing</title>
      <dc:creator>Albert Slyvester Duro</dc:creator>
      <pubDate>Fri, 30 Dec 2022 03:10:01 +0000</pubDate>
      <link>https://forem.com/durosly/how-to-use-jest-for-javascript-testing-28nl</link>
      <guid>https://forem.com/durosly/how-to-use-jest-for-javascript-testing-28nl</guid>
      <description>&lt;p&gt;Testing is an essential part of the software development process. It helps ensure that the code you write is reliable, maintainable, and bug-free. One popular JavaScript testing library is Jest, which is a comprehensive test runner and assertion library that makes it easy to write and run tests.&lt;/p&gt;

&lt;p&gt;In this article, we'll cover the basics of testing with Jest. We'll start by looking at why testing is important, then move on to installing and configuring Jest. Finally, we'll look at some examples of how to write and run tests using Jest.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Test?
&lt;/h2&gt;

&lt;p&gt;There are many reasons why testing is important in software development. Some of the main benefits of testing include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Finding bugs early&lt;/strong&gt;: By writing tests for your code, you can catch bugs early on in the development process, before they have a chance to cause serious problems.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Ensuring code quality&lt;/strong&gt;: Tests can help ensure that your code is of high quality, by checking that it does what it's supposed to do and that it behaves as expected.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Facilitating refactoring&lt;/strong&gt;: Tests can make it easier to refactor your code, by providing confidence that the changes you make won't break existing functionality.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Documenting your code&lt;/strong&gt;: Tests can serve as documentation for your code, by providing examples of how it should be used and what it should do.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Installing and Configuring Jest
&lt;/h2&gt;

&lt;p&gt;To get started with Jest, you'll need to install it as a dev dependency in your project. You can do this by running the following command in your terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--save-dev&lt;/span&gt; jest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once Jest is installed, you'll need to configure it to run your tests. You can do this by creating a jest.config.js file in the root of your project, and adding the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;testMatch&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;**/*.test.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This configuration tells Jest to look for files with a .test.js extension and treat them as test files.&lt;/p&gt;

&lt;h2&gt;
  
  
  Writing and Running Tests
&lt;/h2&gt;

&lt;p&gt;Now that Jest is installed and configured, you're ready to start writing and running tests. Jest tests are written using the describe and test functions.&lt;/p&gt;

&lt;p&gt;Here's an example of a simple test that checks that the add function returns the correct result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;adds 1 + 2 to equal 3&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nf"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To run this test, you can use the jest command in your terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;jest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Jest will automatically find and run all of the test files in your project, and report the results.&lt;/p&gt;

&lt;p&gt;There are many other features and assertions available in Jest, such as support for asynchronous code, snapshot testing, and mocking. You can find more information about these features in the &lt;a href="https://jestjs.io/docs/en/getting-started" rel="noopener noreferrer"&gt;Jest documentation&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this article, we've covered the basics of testing with Jest. We've looked at why testing is important, how to install and configure Jest, and how to write and run tests. By using Jest and other testing tools, you can ensure that your code is reliable, maintainable, and bug&lt;/p&gt;

&lt;p&gt;Happy testing☺️&lt;/p&gt;

</description>
      <category>ai</category>
      <category>discuss</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Beginners guide to working with Sass</title>
      <dc:creator>Albert Slyvester Duro</dc:creator>
      <pubDate>Thu, 29 Dec 2022 21:25:01 +0000</pubDate>
      <link>https://forem.com/durosly/beginners-guide-to-working-with-sass-9p7</link>
      <guid>https://forem.com/durosly/beginners-guide-to-working-with-sass-9p7</guid>
      <description>&lt;p&gt;Sass is a popular CSS preprocessor that adds powerful features to traditional CSS, such as variables, mixins, and functions. It allows developers to write cleaner, more organized, and more maintainable stylesheets by allowing them to use the same set of styles in multiple places and avoid repetitive code.&lt;/p&gt;

&lt;p&gt;To use Sass, you first need to install a Sass compiler, which will take your Sass code and convert it into regular CSS that can be interpreted by browsers. There are a number of different ways to install a Sass compiler, including using a command line tool like Ruby, npm, choco, brew or a standalone application like Koala.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to install sass using npm
&lt;/h2&gt;

&lt;p&gt;Npm is a node package manager which comes with nodejs. Once you have this installed you can open your terminal and type:&lt;br&gt;
&lt;code&gt;npm install -g sass&lt;/code&gt;&lt;br&gt;
This would install the sass compiler globally on your system.&lt;/p&gt;
&lt;h2&gt;
  
  
  How to install sass compiler using Homebrew
&lt;/h2&gt;

&lt;p&gt;This is suitable for Mac and Linux OS. You simply open up your terminal and type the command:&lt;br&gt;
&lt;code&gt;brew install sass/sass/sass&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For more installation methods visit the &lt;a href="https://sass-lang.com/install" rel="noopener noreferrer"&gt;sass&lt;/a&gt; documentation.&lt;/p&gt;

&lt;p&gt;Once you have a compiler installed, you can start writing Sass by creating a .scss file and saving it with a .scss extension inside a project directory. Sass uses a syntax that is similar to CSS, but with some key differences. For example, Sass allows you to use variables to store values that can be reused throughout your stylesheet.&lt;/p&gt;

&lt;p&gt;Here is an example of how you might use variables in Sass:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sass"&gt;&lt;code&gt;&lt;span class="nv"&gt;$primary-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mh"&gt;#333&lt;/span&gt;&lt;span class="err"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$secondary-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mh"&gt;#777&lt;/span&gt;&lt;span class="err"&gt;;&lt;/span&gt;

&lt;span class="nt"&gt;body&lt;/span&gt; &lt;span class="err"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;$primary-color&lt;/span&gt;&lt;span class="err"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;$secondary-color&lt;/span&gt;&lt;span class="err"&gt;;&lt;/span&gt;
&lt;span class="err"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we define two variables, $primary-color and $secondary-color, and then use them to set the background-color and color properties of the body element. This allows us to easily change the colors of our website by simply changing the values of the variables, rather than having to search through our stylesheet and update the values manually.&lt;/p&gt;

&lt;p&gt;Sass also allows you to use mixins, which are reusable blocks of styles that can be included in multiple places throughout your stylesheet. Mixins are especially useful for creating complex styles that may need to be used in multiple places, such as styles for responsive layouts or CSS3 transitions and transformations.&lt;/p&gt;

&lt;p&gt;Here is an example of how you might use a mixin in Sass:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sass"&gt;&lt;code&gt;&lt;span class="k"&gt;@mixin&lt;/span&gt; &lt;span class="nf"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$radius&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="err"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;-webkit-border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;$radius&lt;/span&gt;&lt;span class="err"&gt;;&lt;/span&gt;
     &lt;span class="na"&gt;-moz-border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;$radius&lt;/span&gt;&lt;span class="err"&gt;;&lt;/span&gt;
      &lt;span class="na"&gt;-ms-border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;$radius&lt;/span&gt;&lt;span class="err"&gt;;&lt;/span&gt;
          &lt;span class="nl"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;$radius&lt;/span&gt;&lt;span class="err"&gt;;&lt;/span&gt;
&lt;span class="err"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.button&lt;/span&gt; &lt;span class="err"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;@include&lt;/span&gt; &lt;span class="nd"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;5px&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="err"&gt;;&lt;/span&gt;
&lt;span class="err"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we define a mixin called border-radius that applies a border radius to an element using vendor-prefixes to ensure compatibility with a variety of different browsers. We then use the &lt;a class="mentioned-user" href="https://dev.to/include"&gt;@include&lt;/a&gt; directive to include the mixin in the .button class, which will apply the border radius to any element with the .button class.&lt;/p&gt;

&lt;p&gt;Finally, Sass also includes a number of functions that can be used to perform calculations and manipulate values within your stylesheets. For example, you can use the lighten() function to lighten a color by a specified percentage, or the darken() function to darken a color.&lt;/p&gt;

&lt;p&gt;Here is an example of how you might use functions in Sass:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sass"&gt;&lt;code&gt;&lt;span class="nv"&gt;$primary-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mh"&gt;#333&lt;/span&gt;&lt;span class="err"&gt;;&lt;/span&gt;

&lt;span class="nc"&gt;.button&lt;/span&gt; &lt;span class="err"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;lighten&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$primary-color&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="m"&gt;10%&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="err"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;darken&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$primary-color&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="m"&gt;10%&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="err"&gt;;&lt;/span&gt;
&lt;span class="err"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we use the lighten() and darken() functions to create a button with a background color that is 10% lighter than the $primary-color variable, and a color that is 10% darker.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to compile sass files to css files
&lt;/h2&gt;

&lt;p&gt;You open your terminal and navigate into your project directory and then type the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sass [PATH_TO_SASS_FILE]/[FILENAME].scss [PATH_TO_OUTPUT_CSS_FILE]/[FILENAME].css
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And you are good to go. There are more flags you can add to the command to enhance it e.g. --watch. This keeps the compiler running and you don't have run the command everytime you make changes to your sass files.&lt;/p&gt;

&lt;p&gt;Thank you for reading 💙&lt;/p&gt;

</description>
      <category>react</category>
      <category>nextjs</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
