<?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>DEV Community: Nik Dyankov</title>
    <description>The latest articles on DEV Community by Nik Dyankov (@nikdyankov).</description>
    <link>https://dev.to/nikdyankov</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%2F2487412%2F6423d7cd-81f0-46b6-8146-bb66c353091a.png</url>
      <title>DEV Community: Nik Dyankov</title>
      <link>https://dev.to/nikdyankov</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nikdyankov"/>
    <language>en</language>
    <item>
      <title>Debounce | JS coding challenge</title>
      <dc:creator>Nik Dyankov</dc:creator>
      <pubDate>Fri, 21 Mar 2025 07:43:25 +0000</pubDate>
      <link>https://dev.to/nikdyankov/debounce-js-coding-challenge-58d7</link>
      <guid>https://dev.to/nikdyankov/debounce-js-coding-challenge-58d7</guid>
      <description>&lt;p&gt;In the world of frontend development, performance and user experience are everything. If you’ve ever typed into a search input and noticed it firing a request on every keystroke, you know how annoying and inefficient that can be.&lt;/p&gt;

&lt;p&gt;That’s where debounce comes in.&lt;/p&gt;

&lt;p&gt;Today, I’ll walk you through a JS coding challenge that covers what debounce is, how it works, and why it’s incredibly useful in real-world applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  🚦 Too many function calls!
&lt;/h2&gt;

&lt;p&gt;Imagine building a search bar. Each time a user types a letter, you want to send a request to fetch search results. But if they type fast, you could be firing off dozens of requests per second, many of which are useless because the user hasn’t finished typing yet.&lt;/p&gt;

&lt;p&gt;You need a way to wait until the user stops typing for a bit before running the function.&lt;/p&gt;

&lt;p&gt;That’s what debouncing does.&lt;/p&gt;

&lt;h2&gt;
  
  
  🧠 What Is Debounce?
&lt;/h2&gt;

&lt;p&gt;Debounce is a technique to limit the rate at which a function is executed. Instead of firing the function immediately, you delay its execution until a specified time has passed since the last time it was invoked.&lt;/p&gt;

&lt;p&gt;If it gets called again before the delay ends, the timer resets.&lt;/p&gt;

&lt;h2&gt;
  
  
  🛠️ Writing a Debounce function
&lt;/h2&gt;

&lt;p&gt;Here’s an example implementation of debounce in JavaScript:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function debounce(func, delay) {
  let timeoutId;

  return function(...args) {
    // Clear the previous timeout, so the function won't be executed yet
    clearTimeout(timeoutId);

    // Set a new timeout to call the function after the delay
    timeoutId = setTimeout(() =&amp;gt; {
      func(...args);
    }, delay);
  };
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function takes two arguments:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;func&lt;/strong&gt;: the function you want to debounce.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;delay&lt;/strong&gt;: how long to wait (in ms) after the last call before executing &lt;strong&gt;func&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  💡 Debounced search as use case
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Example function that will be debounced
function searchQuery(query) {
  console.log(`Searching for: ${query}`);
}

// Create a debounced version of the searchQuery function
const debouncedSearch = debounce(searchQuery, 300);

// Now you can call debouncedSearch, and it will only trigger searchQuery once the user stops typing for 300ms.
debouncedSearch('apple');
debouncedSearch('apple pie');
debouncedSearch('apple pie recipe');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even though debouncedSearch is called three times quickly, searchQuery will only run once, with 'apple pie recipe', after the user has stopped typing for 300 milliseconds.&lt;/p&gt;

&lt;h2&gt;
  
  
  🧪 Why it matters?
&lt;/h2&gt;

&lt;p&gt;Using debounce helps with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reducing API calls or expensive operations&lt;/li&gt;
&lt;li&gt;Improving performance&lt;/li&gt;
&lt;li&gt;Smoothing the user experience&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It’s especially useful for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Search inputs&lt;/li&gt;
&lt;li&gt;Resizing events&lt;/li&gt;
&lt;li&gt;Scrolling performance&lt;/li&gt;
&lt;li&gt;Auto-saving forms&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  🧼 TL;DR
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Debounce delays a function until a period of inactivity.&lt;/li&gt;
&lt;li&gt;It helps avoid redundant or expensive executions.&lt;/li&gt;
&lt;li&gt;Easy to implement, and extremely powerful in frontend apps.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Next time you build a feature that responds to frequent user input debounce it. Your app (and your backend) will thank you.&lt;/p&gt;




&lt;p&gt;✍️ Got thoughts or improvements on this debounce implementation? Drop a comment or let’s connect!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>tutorial</category>
      <category>career</category>
    </item>
    <item>
      <title>Is number odd or even | JS coding challenge</title>
      <dc:creator>Nik Dyankov</dc:creator>
      <pubDate>Tue, 10 Dec 2024 09:24:48 +0000</pubDate>
      <link>https://dev.to/nikdyankov/coding-challenge-how-to-check-if-a-number-is-odd-or-even-using-js-580g</link>
      <guid>https://dev.to/nikdyankov/coding-challenge-how-to-check-if-a-number-is-odd-or-even-using-js-580g</guid>
      <description>&lt;p&gt;Determining whether a number is odd or even is a fundamental programming task. In JavaScript, there are several ways to accomplish this. This tutorial will walk you through two simple methods - using the modulus operator and leveraging the bitwise AND operator.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Using the modulus operator
&lt;/h2&gt;

&lt;p&gt;The modulus operator (%) is the most common and straightforward way to determine if a number is odd or even. This operator returns the remainder of a division operation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key concept:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A number is even if it is divisible by 2 (remainder = 0).&lt;/li&gt;
&lt;li&gt;A number is odd if it is not divisible by 2 (remainder ≠ 0).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Code example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function isEven(number) {
    return number % 2 === 0;
}

function isOdd(number) {
    return number % 2 !== 0;
}

// Usage:
console.log(isEven(4)); // true
console.log(isOdd(4));  // false
console.log(isEven(7)); // false
console.log(isOdd(7));  // true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;isEven(4) returns true, meaning 4 is even.&lt;/li&gt;
&lt;li&gt;isOdd(7) returns true, meaning 7 is odd.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  2. Using the bitwise AND operator
&lt;/h2&gt;

&lt;p&gt;The bitwise AND operator &lt;strong&gt;(&amp;amp;)&lt;/strong&gt; can also be used to determine odd or even numbers. This approach relies on binary representation:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key concept:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Even numbers have a 0 as the least significant bit (e.g., 2 = 10 in binary).&lt;/li&gt;
&lt;li&gt;Odd numbers have a 1 as the least significant bit (e.g., 3 = 11 in binary).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;How it works:&lt;/strong&gt;&lt;br&gt;
Performing number &amp;amp; 1 checks the last bit of the number:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If number &amp;amp; 1 === 0, the number is even.&lt;/li&gt;
&lt;li&gt;If number &amp;amp; 1 === 1, the number is odd.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Code example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function isEven(number) {
    return (number &amp;amp; 1) === 0;
}

function isOdd(number) {
    return (number &amp;amp; 1) === 1;
}

// Usage:
console.log(isEven(4)); // true
console.log(isOdd(4));  // false
console.log(isEven(7)); // false
console.log(isOdd(7));  // true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;isEven(4) returns true, meaning 4 is even.&lt;/li&gt;
&lt;li&gt;isOdd(7) returns true, meaning 7 is odd.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Comparison of methods:
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Modulus Operator %&lt;/th&gt;
&lt;th&gt;Bitwise AND Operator &amp;amp;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Readability&lt;/td&gt;
&lt;td&gt;Very easy to understand&lt;/td&gt;
&lt;td&gt;Less intuitive&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Readability&lt;/td&gt;
&lt;td&gt;Slightly slower for large numbers&lt;/td&gt;
&lt;td&gt;Slightly faster for large numbers&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Use Case&lt;/td&gt;
&lt;td&gt;General-purpose applications&lt;/td&gt;
&lt;td&gt;Optimised low-level operations&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Which method should you use?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;strong&gt;the Modulus operator&lt;/strong&gt; if you want a solution that is simple and easy to understand. It’s the most common method and is suitable for most scenarios.&lt;/li&gt;
&lt;li&gt;Use &lt;strong&gt;the bitwise AND operator&lt;/strong&gt; if performance is critical (e.g., in high-performance code or embedded systems).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Final thoughts
&lt;/h2&gt;

&lt;p&gt;Both methods are effective for checking if a number is odd or even in JavaScript. Choose the one that best fits your specific use case. Happy coding! 🎉&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
