<?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: Odhiambo Ouko</title>
    <description>The latest articles on DEV Community by Odhiambo Ouko (@odhiambo_ouko).</description>
    <link>https://dev.to/odhiambo_ouko</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%2F1527720%2Fb4a8559e-9277-497f-9416-a87b8ccba320.png</url>
      <title>DEV Community: Odhiambo Ouko</title>
      <link>https://dev.to/odhiambo_ouko</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/odhiambo_ouko"/>
    <language>en</language>
    <item>
      <title>5 Unique Features that Put Goleko Ahead of the Game</title>
      <dc:creator>Odhiambo Ouko</dc:creator>
      <pubDate>Fri, 14 Jun 2024 12:45:05 +0000</pubDate>
      <link>https://dev.to/odhiambo_ouko/5-unique-features-that-put-goleko-ahead-of-the-game-3o0d</link>
      <guid>https://dev.to/odhiambo_ouko/5-unique-features-that-put-goleko-ahead-of-the-game-3o0d</guid>
      <description>&lt;p&gt;Project management platforms are requisite tools when it comes to structuring and assigning tasks, promoting collaborations, sharing files, and, of course, boosting productivity. The need for project management solutions is even higher today, given the increasing number of remote and distributed teams around the world. While there are many project management tools to select from, one that stands out is Goleko – an up-and-coming solution with immense capabilities. &lt;/p&gt;

&lt;p&gt;Let’s dive into a few features that make Goleko give other project management tools a run for their money. &lt;/p&gt;

&lt;h2&gt;
  
  
  Automatic Smartboard
&lt;/h2&gt;

&lt;p&gt;One of Goleko’s intriguing features is its cutting-edge smartboard, which delivers easy viewing and flexibility. Thanks to its compelling UI design and one-click functionality, the smartboard allows users to move seamlessly from one part of the platform to another, whether you want to assign tasks, check tasks in progress, or locate important files. These smartboard attributes enhance user experience and retention – ultimately improving productivity. &lt;/p&gt;

&lt;h2&gt;
  
  
  Time-Tracking Capability
&lt;/h2&gt;

&lt;p&gt;Not all project management tools have time trackers. Fortunately, Goleko has a time-tracking feature to take project management to the next level. With Goleko’s time-tracking capability, project managers and team members can check how much time was spent completing a project or the time left until specific tasks reach their deadline. As a project manager, you can use the tracking feature to not only evaluate your team members’ effectiveness but also export time logs for invoices. &lt;/p&gt;

&lt;h2&gt;
  
  
  Permission Granting
&lt;/h2&gt;

&lt;p&gt;Geleko has a permission feature that lets you dictate how other people interact with your projects. You can use the feature to allow select team members to create, edit, or upload a task. The feature also lets you show or hide comments and display potential price quotes to particular clients. Besides promoting collaboration, the permission-granting capability gives managers complete control of their projects for improved management and outcomes. &lt;/p&gt;

&lt;h2&gt;
  
  
  Screen Recording
&lt;/h2&gt;

&lt;p&gt;Another unique feature Goleko has is a screencast for recording screen videos. By recording your screen, you can explain how to do specific tasks, comment on projects, and provide real-time feedback to your team members. Since videos are easier to follow and understand than written comments and feedback, Goleko’s screen recording feature expedites communication and helps improve project success. &lt;/p&gt;

&lt;h2&gt;
  
  
  Files Importation
&lt;/h2&gt;

&lt;p&gt;While working on projects, you may sometimes need to use files on other tools on your computer. Goleko was made with this issue in mind, which is why it has an import feature that enables you to extract tasks and projects. The import feature is essential for quickly and efficiently transferring all your work to Goleko for centralized management. &lt;/p&gt;

&lt;h2&gt;
  
  
  Leverage the Power of Goleko
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://goleko.com/"&gt;Goleko&lt;/a&gt; has several trailblazing features that make it the go-to project management tool. From its one-click smartboard to file importation capabilities, Goleko is designed to make project management easy and fun. Goleko is available in three packages – including a &lt;a href="https://goleko.com/app"&gt;Free starter pack&lt;/a&gt; that has a total capacity of 300 megabytes and accommodates up to 3 users, 3 projects, and 300 tasks. &lt;/p&gt;

</description>
      <category>productivity</category>
      <category>productmanagement</category>
      <category>programming</category>
      <category>discuss</category>
    </item>
    <item>
      <title>If, Else, Else If, and Switch JavaScript Conditional Statement</title>
      <dc:creator>Odhiambo Ouko</dc:creator>
      <pubDate>Fri, 07 Jun 2024 08:30:59 +0000</pubDate>
      <link>https://dev.to/odhiambo_ouko/if-else-else-if-and-switch-javascript-conditional-statement-39mo</link>
      <guid>https://dev.to/odhiambo_ouko/if-else-else-if-and-switch-javascript-conditional-statement-39mo</guid>
      <description>&lt;p&gt;In our everyday lives, we always make decisions depending on circumstances. Chew over a daily task, such as making coffee in the morning. If we have coffee beans, we can make coffee; otherwise, we won’t. &lt;/p&gt;

&lt;p&gt;In programming, we may need our code to run depending on certain conditions. For instance, you may want your program to assign an A grade to students if they score more than 80 points or program a website to display a checkout tab on a liquor website only if the user is 18 or older. That's where conditional JavaScript statements come in. The conditions specify the code block to be executed if a specified condition is true. &lt;/p&gt;

&lt;p&gt;Let's explore if, if…else, else if, and switch statements to understand how they are used in JavaScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  If Statement
&lt;/h2&gt;

&lt;p&gt;The &lt;em&gt;if&lt;/em&gt; statement is the most basic conditional statement in JavaScript. This statement evaluates a condition and executes the code block within its body if the condition is true. If the condition is evaluated as false, then the code block will not run. &lt;/p&gt;

&lt;p&gt;If Statement Syntax&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c1"&gt;//code block to be executed if the condition is true&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  If Statement in Action
&lt;/h3&gt;

&lt;p&gt;We can use the &lt;em&gt;if&lt;/em&gt; statement to execute a given code block provided a given condition is true. For example, we can write our code to send interviewees a congratulations message if they score 75 points or more.&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;score&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;75&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Congratulations&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="nx"&gt;You&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="nx"&gt;re&lt;/span&gt; &lt;span class="nx"&gt;hired&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;Output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Congratulations! You’re hired
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  If…Else Statement
&lt;/h2&gt;

&lt;p&gt;What if we want to run a second code if the preset condition returns a false value? In that case, we employ the &lt;em&gt;if…else statement&lt;/em&gt;. This statement checks a condition and runs the first code block if the condition evaluates to true. If the condition is false, the second code will execute instead.&lt;/p&gt;

&lt;p&gt;If…Else Statement Syntax&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c1"&gt;//first code block to be executed if the condition is true&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c1"&gt;//second code block to be executed if the condition is false&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  If…Else Statement in Action
&lt;/h3&gt;

&lt;p&gt;We can instruct our program to let users aged 24 and above create a customer account but encourage those below 24 to try the process later.&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;24&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Welcome&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="nx"&gt;Proceed&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;open&lt;/span&gt; &lt;span class="nx"&gt;an&lt;/span&gt; &lt;span class="nx"&gt;account&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Age&lt;/span&gt; &lt;span class="nx"&gt;limit&lt;/span&gt; &lt;span class="nx"&gt;not&lt;/span&gt; &lt;span class="nx"&gt;reached&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="nx"&gt;Try&lt;/span&gt; &lt;span class="nx"&gt;again&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;few&lt;/span&gt; &lt;span class="nx"&gt;months&lt;/span&gt;&lt;span class="o"&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;h3&gt;
  
  
  Ternary Operator
&lt;/h3&gt;

&lt;p&gt;Although &lt;em&gt;if…else&lt;/em&gt; statements are easy to write, there is a shorter way to write them. The shorthand Is known as a ternary or conditional operator. &lt;/p&gt;

&lt;h3&gt;
  
  
  Ternary Operator Syntax
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;Condition&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;Expression&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Expression&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The example above represents the syntax of a basic ternary operator. The condition to be evaluated is put before the question mark &lt;em&gt;?&lt;/em&gt;, followed by the statements to be executed. A colon &lt;em&gt;:&lt;/em&gt; separates the two expressions. The entire operator ends with a semicolon. In the ternary operator, the first statement will execute if the condition is true. If it's false, the second expression will run.&lt;/p&gt;

&lt;h3&gt;
  
  
  Ternary Operator in Action
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;passMark&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)?&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Pass&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="nx"&gt;Go&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="nx"&gt;level&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Fail&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="nx"&gt;Try&lt;/span&gt; &lt;span class="nx"&gt;again&lt;/span&gt; &lt;span class="nx"&gt;later&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, the program will print &lt;strong&gt;Pass! Go to the next level&lt;/strong&gt; if the score is 50 or more. It will print &lt;strong&gt;Fail! Try again later&lt;/strong&gt; if the score is below 50. &lt;/p&gt;

&lt;h2&gt;
  
  
  Else If Statement
&lt;/h2&gt;

&lt;p&gt;We usually use the if…else statement when evaluating one condition. What if we want to add more conditions to our if…else statement? Simple, we use the &lt;em&gt;else if&lt;/em&gt; statement! Unlike the if…else statement, the else if statement allows us to have more than two possible outcomes. We can also use it as many times as we want.&lt;/p&gt;

&lt;h3&gt;
  
  
  Else If Statement Syntax
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c1"&gt;//Code block to be executed&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Else If in Action
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;Let&lt;/span&gt; &lt;span class="nx"&gt;time&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;00&lt;/span&gt; &lt;span class="nx"&gt;am&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;
&lt;span class="nc"&gt;If &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;time&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;9:00 am&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="nx"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;Good&lt;/span&gt; &lt;span class="nx"&gt;Morning&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="nx"&gt;Time&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;wake&lt;/span&gt; &lt;span class="nx"&gt;up&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;time&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;00&lt;/span&gt; &lt;span class="nx"&gt;am&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nx"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;Good&lt;/span&gt; &lt;span class="nx"&gt;afternoon&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="nx"&gt;Time&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nx"&gt;lunch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;time&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;00&lt;/span&gt; &lt;span class="nx"&gt;am&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nx"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;Good&lt;/span&gt; &lt;span class="nx"&gt;Afternoon&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="nx"&gt;Time&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;go&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;gym&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;time&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;00&lt;/span&gt; &lt;span class="nx"&gt;pm&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nx"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;Goodnight&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="nx"&gt;Time&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nx"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;Invalid&lt;/span&gt; &lt;span class="nx"&gt;time&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="err"&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;In the above example, the program will print &lt;strong&gt;Good afternoon! Time for lunch.&lt;/strong&gt; because the time is set to 12:00 am. &lt;/p&gt;

&lt;h2&gt;
  
  
  Switch Statement
&lt;/h2&gt;

&lt;p&gt;While the else if statements are incredible for introducing more conditions to our if…else statement, writing many statements can be cumbersome. Moreover, numerous else if statements chained in an if…else statement can be hard to evaluate. As a result, they often have readability issues and are notorious for causing errors. &lt;/p&gt;

&lt;p&gt;Luckily, we can take advantage of the &lt;em&gt;switch statement&lt;/em&gt; to avoid dealing with multiple if…else and else if statements. A switch statement is not only straightforward to write but also easy to read and maintain. &lt;/p&gt;

&lt;h3&gt;
  
  
  Switch Statement Syntax
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;switch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;expression&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; 
&lt;span class="c1"&gt;//code block to be executed &lt;/span&gt;
&lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; 
&lt;span class="c1"&gt;//code block to be executed &lt;/span&gt;
&lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; 
&lt;span class="c1"&gt;//code block to be executed &lt;/span&gt;
&lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nl"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="c1"&gt;//code block to be executed &lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The example above shows a switch statement. A switch statement begins with a variable called a switch followed by an expression enclosed in parenthesis. Curly brackets encircling case clauses come after the expression. Each case clause ends with a semicolon to tell the JavaScript interpreter to skip to the subsequent clause. &lt;/p&gt;

&lt;p&gt;The switch statement evaluates the expression once and compares its value against those in the case clauses. Only the code block in the case clause that matches the expression will be executed. If there is no match, the default code will run. &lt;/p&gt;

&lt;h3&gt;
  
  
  Switch Statement in Action
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;shoeName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;adidasSamba&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;
&lt;span class="k"&gt;switch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;shoeName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;adidasGazelle&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; 
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;Adidas&lt;/span&gt; &lt;span class="nx"&gt;Gazelle&lt;/span&gt; &lt;span class="nx"&gt;costs&lt;/span&gt; &lt;span class="nx"&gt;$49&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;adidaSamba&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;Adidas&lt;/span&gt; &lt;span class="nx"&gt;Samba&lt;/span&gt; &lt;span class="nx"&gt;costs&lt;/span&gt; &lt;span class="nx"&gt;$99&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;adidasSuperstar&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;Adidas&lt;/span&gt; &lt;span class="nx"&gt;Superstar&lt;/span&gt; &lt;span class="nx"&gt;costs&lt;/span&gt; &lt;span class="nx"&gt;$149&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;adidasNMD&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;Adidas&lt;/span&gt; &lt;span class="nx"&gt;NMD&lt;/span&gt; &lt;span class="nx"&gt;costs&lt;/span&gt; &lt;span class="nx"&gt;$199&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;adidasCampus&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;Adidas&lt;/span&gt; &lt;span class="nx"&gt;Campus&lt;/span&gt; &lt;span class="nx"&gt;costs&lt;/span&gt; &lt;span class="nx"&gt;$249&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nl"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; 
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Invalid&lt;/span&gt; &lt;span class="nx"&gt;option&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;break&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;h3&gt;
  
  
  Output
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;Adidas&lt;/span&gt; &lt;span class="nx"&gt;Samba&lt;/span&gt; &lt;span class="nx"&gt;costs&lt;/span&gt; &lt;span class="nx"&gt;$99&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Parting Shot
&lt;/h2&gt;

&lt;p&gt;Throughout this article, we’ve discussed JavaScript if, else, else if, and switch statements. These statements are essential when it comes to executing specific code blocks based on preset conditions. Each of the statements plays a distinctive role and must be used correctly to achieve the desired results. In conclusion, applying best practices and avoiding common pitfalls when using these conditions to unlock their full potential is imperative.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>development</category>
      <category>beginners</category>
    </item>
    <item>
      <title>A Comprehensive Guide to Media Queries in CSS</title>
      <dc:creator>Odhiambo Ouko</dc:creator>
      <pubDate>Tue, 04 Jun 2024 10:22:11 +0000</pubDate>
      <link>https://dev.to/odhiambo_ouko/a-comprehensive-guide-to-media-queries-in-css-1kc6</link>
      <guid>https://dev.to/odhiambo_ouko/a-comprehensive-guide-to-media-queries-in-css-1kc6</guid>
      <description>&lt;p&gt;Today, people use differently-sized devices, including smartphones, tablets, and laptops to access the internet. Since screen sizes differ from user to user, creating websites that respond to varying screen sizes, no matter the device, is essential. That’s where CSS media queries come in. But what are media queries, and how do they work? The following guide provides a detailed overview of media queries in CSS. &lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Media Query?
&lt;/h2&gt;

&lt;p&gt;A media query is a CSS feature that allows you to apply certain styles on a website browser depending on your users’ device or viewport. Nevertheless, media queries can target many other things apart from the viewport, including orientation and resolution. According to the Web Design Museum, media queries were first introduced in the 1990s, but they became popular in the late 2000s and 2010s with the increase in mobile phone manufacturing and usage. These days, media queries are vital to creating responsive web designs for excellent appearance and functionality. &lt;/p&gt;

&lt;h2&gt;
  
  
  Media Query Syntax
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="k"&gt;@media&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;media&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;operator&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;media&lt;/span&gt; &lt;span class="n"&gt;feature&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c"&gt;/*css styles*/&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The example above represents a media query. Let’s dissect each part of the query for a better understanding. &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a class="mentioned-user" href="https://dev.to/media"&gt;@media&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;The first part of a media query anatomy is the &lt;a class="mentioned-user" href="https://dev.to/media"&gt;@media&lt;/a&gt; keyword, the at-rule. It is a logical operator for defining specific media types and features that must be met for a particular CSS style to run. &lt;/p&gt;

&lt;h3&gt;
  
  
  Media Type
&lt;/h3&gt;

&lt;p&gt;Media type, which specifies the targeted media, is the second item in a media query. Since various media types exist, media types can define one or more devices. The media type is optional and automatically set to all if not specified. We can combine multiple media types with a comma. Here are the most popular values for media queries: &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;All&lt;/strong&gt;: Targets all devices&lt;br&gt;
&lt;strong&gt;Screen&lt;/strong&gt;: Targets devices with a screen&lt;br&gt;
&lt;strong&gt;Print&lt;/strong&gt;: Targets devices with a preview mode&lt;br&gt;
&lt;strong&gt;Speech&lt;/strong&gt;: Targets speech devices&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="k"&gt;@media&lt;/span&gt; &lt;span class="n"&gt;all&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c"&gt;/*CSS styles targeting all devices*/&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="k"&gt;@media&lt;/span&gt; &lt;span class="n"&gt;screen&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c"&gt;/*CSS styles for screen devices*/&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="c"&gt;/*(,) comma combines multiple media types*/&lt;/span&gt;
&lt;span class="k"&gt;@media&lt;/span&gt; &lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;speech&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c"&gt;/*CSS styles for print and speech devices*/&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Media Features
&lt;/h3&gt;

&lt;p&gt;After targeting specific media types, you can indicate the media features you want to modify. These media features are the conditions that must be fulfilled to run the CSS code put in the query. The media features can include page characteristics, display quality, user preference, interaction, color, and more. Here are some popular media features: &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Height&lt;/strong&gt;: Defines the height of the viewport&lt;br&gt;
&lt;strong&gt;Width&lt;/strong&gt;: Defines the width of the viewport&lt;br&gt;
&lt;strong&gt;Orientation&lt;/strong&gt;: Defines the viewport’s orientation (landscape or portrait)&lt;br&gt;
&lt;strong&gt;Resolution&lt;/strong&gt;: Defines the number of pixels a device can render&lt;/p&gt;
&lt;h3&gt;
  
  
  Operator(s)
&lt;/h3&gt;

&lt;p&gt;Media queries accept logical operators, instrumental in creating complex queries and combining several queries into a single rule separated by commas. &lt;/p&gt;
&lt;h4&gt;
  
  
  and
&lt;/h4&gt;

&lt;p&gt;We can use the operator to include multiple media features in the same query. In addition, we can apply the operator to create a range for our media types.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="c"&gt;/*and combines two media queries*/&lt;/span&gt;
&lt;span class="k"&gt;@media&lt;/span&gt; &lt;span class="n"&gt;screen&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;min-width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;280px&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;and&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;resolution&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c"&gt;/*CSS styles run if the media features for both media types are true*/&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="c"&gt;/*and allows us to include a range for our media features*/&lt;/span&gt;
&lt;span class="k"&gt;@media&lt;/span&gt; &lt;span class="n"&gt;screen&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;min-width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;280px&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;and&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max-width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;680px&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c"&gt;/*CSS styles run if both media features are true*/&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  or
&lt;/h4&gt;

&lt;p&gt;We can use the or operator in a media query if only one of the targeted media features must be met. Contrary to common practice, applying the or operator in media queries involves separating the media features with a comma.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="c"&gt;/*or separates two media features*/&lt;/span&gt;
&lt;span class="k"&gt;@media&lt;/span&gt; &lt;span class="n"&gt;screen&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;min-width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;680px&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;orientation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;portrait&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c"&gt;/*CSS styles run if either one of the media features is true*/&lt;/span&gt;

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  not
&lt;/h4&gt;

&lt;p&gt;The not operator negates parts of or an entire media query depending on its position. Since the not operator nullifies everything following it, using a comma-separated list effectively applies it to certain parts of the query.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="c"&gt;/*not negates the entire media type*/&lt;/span&gt;
&lt;span class="k"&gt;@media&lt;/span&gt; &lt;span class="n"&gt;not&lt;/span&gt; &lt;span class="n"&gt;screen&lt;/span&gt; &lt;span class="n"&gt;and&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;min-resolution&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c"&gt;/*CSS styles for devices without screens and a minimum resolution of 1*/&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="c"&gt;/*not negates a section of the media type*/&lt;/span&gt;
&lt;span class="k"&gt;@media&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;not&lt;/span&gt; &lt;span class="n"&gt;speech&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;and&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;orientation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;landscape&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c"&gt;/*CSS styles for non-speech devices with landscape orientation*/&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  only
&lt;/h4&gt;

&lt;p&gt;Unlike the other operators we’ve discussed, the only operator is unique since it hides the query for legacy user agents. However, the operator does not affect modern browsers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="c"&gt;/*Older browsers don’t understand the only operator, so they ignore it*/&lt;/span&gt;
&lt;span class="k"&gt;@media&lt;/span&gt; &lt;span class="n"&gt;only&lt;/span&gt; &lt;span class="n"&gt;all&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;min-height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;320px&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;and&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max-height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1280px&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;orientation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;landscape&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c"&gt;/*CSS styles for all devices, except devices with old browsers*/&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Media Query Best Practices
&lt;/h2&gt;

&lt;p&gt;We can follow various best practices when applying media queries for better results.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Employ the mobile-first approach
&lt;/h3&gt;

&lt;p&gt;One valuable best practice when creating media queries is implementing a mobile-first approach. This involves crafting your website to fit small screens first and then scaling up to larger screens. Besides reaching a large audience, a mobile-first approach will ensure your website is accessible on all screen sizes.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Prioritize Logical Breakpoints
&lt;/h3&gt;

&lt;p&gt;Using logical breakpoints based on your website’s content instead of applying fixed breakpoints related to standard screen sizes is another good practice. Unlike logical breakpoints, which are natural, fixed breakpoints can change over time.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Test and Improve
&lt;/h3&gt;

&lt;p&gt;Don’t forget to test and improve your media queries to ensure your website is accessible on different devices and browsers. After all, refining media queries to optimize performance can render your website superior and user-friendly. &lt;/p&gt;

&lt;h2&gt;
  
  
  Bottom Line
&lt;/h2&gt;

&lt;p&gt;Media queries are essential in creating responsive web designs for users to access across different screen sizes, devices, and browsers. For that reason, developers should take advantage of media queries to craft dynamic and user-centric products. Thus, a proper understanding of media queries is fundamental to exploring their full potential in web development. &lt;/p&gt;

</description>
      <category>webdev</category>
      <category>css</category>
      <category>learning</category>
      <category>beginners</category>
    </item>
    <item>
      <title>A Walk Through Client-Side Form Validation in HTML</title>
      <dc:creator>Odhiambo Ouko</dc:creator>
      <pubDate>Thu, 30 May 2024 16:25:25 +0000</pubDate>
      <link>https://dev.to/odhiambo_ouko/a-walk-through-client-side-form-validation-in-html-1l1p</link>
      <guid>https://dev.to/odhiambo_ouko/a-walk-through-client-side-form-validation-in-html-1l1p</guid>
      <description>&lt;h2&gt;
  
  
  What is Form Validation?
&lt;/h2&gt;

&lt;p&gt;Have you ever encountered a form on a website that returns an error message when you provide the wrong details or don’t input some information? The error messages can include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Username not found&lt;/li&gt;
&lt;li&gt;Incorrect password. Try again!&lt;/li&gt;
&lt;li&gt;Please fill in this field&lt;/li&gt;
&lt;li&gt;Your passcode should have eight characters&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When you key in data into a form, the browser or website will check the provided information to see if it matches the required format. The application will accept the input and continue running if the data is correct and in the proper format. On the other hand, the application will cease running and return an error message if the details are incorrect. This process is known as form validation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Types of Form Validation
&lt;/h2&gt;

&lt;p&gt;There are two types of form validation: server-side form validation and client-side form validation.&lt;/p&gt;

&lt;h3&gt;
  
  
  Server-Side Validation
&lt;/h3&gt;

&lt;p&gt;Server-side validation occurs when data is sent to a server from the browser for validation. A perfect scenario of server-side validation is a credit card form that accepts user input and then sends the data to a server to confirm the card is valid and has enough balance to make a particular transaction.&lt;/p&gt;

&lt;h4&gt;
  
  
  Pros of Server-Side Validation
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;Dynamic Validation – Server-side validation is more flexible because it can access databases and conduct complex validations.&lt;/li&gt;
&lt;li&gt;Consistent and Compatible Validation – Unlike client-side validation, server-side validation works properly across different devices, browsers, and sites.&lt;/li&gt;
&lt;li&gt;Increased Security – Server-side validation is more secure because it prevents invalid data or malicious input from entering a database. &lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Cons of Server-Side Validation
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;Reduced Response Time – The Request and response processes between the browser and server reduce the response time in server-side validation.&lt;/li&gt;
&lt;li&gt;Poor User Experience – Poor response time and users’ inability to immediately determine the valid options negatively affect user experience.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Client-Side Validation
&lt;/h3&gt;

&lt;p&gt;On the other hand, client-side validation involves checking data on the browser before sending it to the server. This type of validation is essential in ensuring the data a user submits conforms to all the requirements stipulated by various form controls. A good example is a registration form requiring users to enter their details and a short bio.&lt;/p&gt;

&lt;h4&gt;
  
  
  Pros of Client-Side Validation
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;Quick Feedback – Since client-side validation happens on the user’s computer, it quickly provides feedback to the user without reloading the page.&lt;/li&gt;
&lt;li&gt;Data Accuracy – By detecting wrong inputs and alerting the users, client-side validation ensures the data collected is accurate.&lt;/li&gt;
&lt;li&gt;Improved User Experience – Giving users instant feedback when using a form enhances user experience.&lt;/li&gt;
&lt;li&gt;Reduced server load and bandwidth – Client-side validation only accepts valid data, therefore decreasing a browser’s server load and bandwidth.&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Cons of Client-Side Validation
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;Security Concerns – Client-side form validation is less secure since it can’t process an application from attacks in a database or server.&lt;/li&gt;
&lt;li&gt;Easy to Dodge – Users can bypass client-side validation by disabling JavaScript or manipulating the document object model (DOM).&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  HTML Built-In Form Controls
&lt;/h2&gt;

&lt;p&gt;Client-side form validation can be achieved using HTML or JavaScript. HTML makes use of several form controls to help you implement effective client-side validation for your users. &lt;/p&gt;

&lt;h3&gt;
  
  
  Requiring an Input
&lt;/h3&gt;

&lt;p&gt;When a specific field must be filled before submitting a form, we can use the required attribute in the input element. This attribute doesn’t need a value like other built-in form controls.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;form&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Name&lt;span class="nt"&gt;&amp;lt;label&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt; &lt;span class="na"&gt;required&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Specifying Minimum and Maximum Values
&lt;/h3&gt;

&lt;p&gt;Another built-in form control we can use is the minimum or maximum attributes in a number field. The attributes specify the minimum and maximum numerical values a user can select or enter in a form.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;form&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for =&lt;/span&gt;&lt;span class="s"&gt;"guests"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Number of guests&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"guests"&lt;/span&gt; &lt;span class="na"&gt;min=&lt;/span&gt;&lt;span class="s"&gt;"2"&lt;/span&gt; &lt;span class="na"&gt;max=&lt;/span&gt;&lt;span class="s"&gt;"10"&lt;/span&gt; &lt;span class="na"&gt;required&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt; 
&lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Indicating Type
&lt;/h3&gt;

&lt;p&gt;This form control specifies the data type a user needs to input, whether an email address, a number, a text, or any other type. We use the type attribute and set its value to number, text, email, or password.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;form&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for =&lt;/span&gt;&lt;span class="s"&gt;"age"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Age&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"age"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"number"&lt;/span&gt; &lt;span class="na"&gt;required&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Constraining Text Length
&lt;/h3&gt;

&lt;p&gt;We can use the minlength and maxlength attributes to set the minimum and maximum characters for a text area.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;form&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;textarea&amp;gt;&lt;/span&gt;Introduce yourself in less than 250 characters…&lt;span class="nt"&gt;&amp;lt;/textarea&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;minlength=&lt;/span&gt;&lt;span class="s"&gt;"50"&lt;/span&gt; &lt;span class="na"&gt;maxlength=&lt;/span&gt;&lt;span class="s"&gt;"250"&lt;/span&gt; &lt;span class="na"&gt;required&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Matching Text Pattern
&lt;/h3&gt;

&lt;p&gt;Lastly, we can create a regular expression or regex that users must match when entering data. The form can only pass when the user’s input aligns with the preset regex. In this case, we use the pattern attribute and give it a regex.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;form&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for = &lt;/span&gt;&lt;span class="s"&gt;"telephone"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Enter your phone number&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"telephone"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"number"&lt;/span&gt; &lt;span class="na"&gt;pattern=&lt;/span&gt;&lt;span class="s"&gt;"[0-9] {10}"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Bottom Line
&lt;/h2&gt;

&lt;p&gt;In this article, we exclusively discussed form validation in HTML. We also shed some light on the difference between server-side and client-side validation and outlined their respective pros and cons. Finally, we covered the most common built-in form controls used to validate forms on the client’s side. Applying server-side validation is significant for ensuring data accuracy, improving security, and improving user experience. You must also validate data on the server side.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>An Introduction to Basic JavaScript Loops</title>
      <dc:creator>Odhiambo Ouko</dc:creator>
      <pubDate>Wed, 29 May 2024 08:37:41 +0000</pubDate>
      <link>https://dev.to/odhiambo_ouko/an-introduction-to-basic-javascript-loops-2j8j</link>
      <guid>https://dev.to/odhiambo_ouko/an-introduction-to-basic-javascript-loops-2j8j</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;As a frontend and/or JavaScript developer, there are multiple occasions when we will want our code to conduct repetitive tasks. We can achieve this by writing the code manually a couple of times, but this process can be tedious, especially when we have a handful of code blocks to execute repeatedly. Thankfully, we can use loops in JavaScript to execute a block code several times. In this article, we will discuss the for, do, and while loops, with a focus on their syntax and examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are Loops in JavaScript?
&lt;/h2&gt;

&lt;p&gt;In JavaScript, loops are powerful tools designed to perform repetitive tasks quickly and efficiently. A loop executes a code block several times depending on a specified condition, commonly known as the stopping condition. The code will run over and over again until the condition returns false.&lt;/p&gt;

&lt;h2&gt;
  
  
  Types of Loops
&lt;/h2&gt;

&lt;p&gt;There are three main types of loops in JavaScript: for, while, and do…while loops. While both loops can iterate a code block several times, the loop types work differently and are suitable for different scenarios.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. For Loop
&lt;/h3&gt;

&lt;p&gt;The for loop is a control flow statement that examines a condition and executes a code several times as long as the condition is true. We often use the for loop if we need to run a code block a specific number of times. For this reason, the condition in a for loop is usually a counter indicating how many times the loop should run a code. Besides, the for loop is the most common loop in JavaScript and other high-level programming languages.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmo07kxwr15xzqjepn76k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmo07kxwr15xzqjepn76k.png" alt="Image description" width="800" height="863"&gt;&lt;/a&gt;Figure 1: For loop flowchart&lt;/p&gt;

&lt;h3&gt;
  
  
  For Loop Syntax
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;initialization&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;iteration&lt;/span&gt; &lt;span class="nx"&gt;statement&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c1"&gt;//code block to be executed&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code above represents the syntax of a basic for loop. It starts with the for keyword, then the initialization, condition, and iteration statements enclosed in parentheses, followed by the code to be executed in curly brackets (loop body). Let us dissect each part in the parentheses to understand what they are and their uses. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Initialization: The initialization, commonly abbreviated as

&lt;code&gt;i&lt;/code&gt;

, initializes a loop and determines how the loop iterates through your logic. It's executed first and once in a loop. Since the initialization expression is a variable, we can use the

&lt;code&gt;let&lt;/code&gt;

or

&lt;code&gt;var&lt;/code&gt;

keyword to declare it. &lt;/li&gt;
&lt;/ul&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Condition: A condition is the second recipe in a for-loop statement. The condition is usually a Boolean function that tells the loop how many times it should run a code block. A loop will run if the condition is true and stop when the condition is false.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Iteration Statement: Once the loop has executed a code block, the iteration statement will tell it what to do with the iterator. In most cases, the iteration statement can increase or decrease the iterator. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  For Loop in Action
&lt;/h3&gt;

&lt;p&gt;Example 1&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="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&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;Output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1
2
3
4
5
6
7
8
9
10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example 2&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="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&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;Output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;10
9
8
7
6
5
4
3
2
1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example 3&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;const&lt;/span&gt; &lt;span class="nx"&gt;luxuryBrands&lt;/span&gt; &lt;span class="o"&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;Chanel&lt;/span&gt;&lt;span class="dl"&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;Prada&lt;/span&gt;&lt;span class="dl"&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;Dior&lt;/span&gt;&lt;span class="dl"&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;Rolex&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;luxuryBrands&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;luxuryBrands&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&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;Output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Chanel
Prada
Dior
Rolex
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example 4&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;const&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;totalSum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nx"&gt;totalSum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;totalSum&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;totalSum&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1275
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. While Loop
&lt;/h3&gt;

&lt;p&gt;We can use the while loop if we're unsure how many times we should iterate a line of code. Unlike the for loop, the condition for a while loop can be anything other than a counter. A while loop will execute the code in its body repeatedly, provided the condition is true. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx7o6hj5ee759jbegb2gp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx7o6hj5ee759jbegb2gp.png" alt="Image description" width="800" height="903"&gt;&lt;/a&gt;Figure 2: While loop flowchart&lt;/p&gt;

&lt;h3&gt;
  
  
  While Loop Syntax
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c1"&gt;//code block to be executed&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The standard syntax of a while loop is as shown above. It begins with the while statement followed by the condition in parentheses. The code block we want to run comes after the parentheses. In a while loop, the condition is examined before the loop body. As a result, the loop will not run the code block if the condition is false. It will only execute the code if the condition is initially true and terminate when it becomes false. Thus, the while loop is excellent for scenarios where the condition is true at the outset. &lt;/p&gt;

&lt;h3&gt;
  
  
  While Loop in Action
&lt;/h3&gt;

&lt;p&gt;Example 1&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;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&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;Output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1
2
3
4
5
6
7
8
9
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Do…While Loop
&lt;/h3&gt;

&lt;p&gt;The do…while loop is similar to the while loop since it is effective for running a code if the number of iterations is undefined. Below is a simple syntax of the do…while loop.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftvlqnql82ne1rxltcboe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftvlqnql82ne1rxltcboe.png" alt="Image description" width="638" height="1088"&gt;&lt;/a&gt;Figure 3: Do…While Loop Flowchart&lt;/p&gt;

&lt;h3&gt;
  
  
  Do...While Loop Syntax
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c1"&gt;//code block to be executed&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The loop starts with the do keyword, a code block to be executed within curly brackets, the while keyword, and the condition to be evaluated in parenthesis. Since the do keyword comes before the loop body, the code in the curly brackets must run at least once, even if the condition is false. That's because the do…while loop evaluates the condition after the code block, unlike in the while loop. &lt;/p&gt;

&lt;h3&gt;
  
  
  Do…While Loop in Action
&lt;/h3&gt;

&lt;p&gt;Example 1&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;let&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;10
20
30
40
50
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Closing Thoughts
&lt;/h2&gt;

&lt;p&gt;In this article, we've learned that a loop is a powerful JavaScript tool used for iterating through code blocks. Instead of writing repetitive code manually, the for, while, and do…while loops allow you to iterate through a block of code with less effort. Knowing when to use each type of loop and avoid common pitfalls is crucial in maximizing their potential.&lt;/p&gt;

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