<?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: David Okpare</title>
    <description>The latest articles on DEV Community by David Okpare (@iandavid).</description>
    <link>https://dev.to/iandavid</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%2F69150%2Facc39eab-3de7-4b9a-9117-b96550aa19cd.png</url>
      <title>DEV Community: David Okpare</title>
      <link>https://dev.to/iandavid</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/iandavid"/>
    <language>en</language>
    <item>
      <title>Ternary Operators In JavaScript</title>
      <dc:creator>David Okpare</dc:creator>
      <pubDate>Mon, 27 Aug 2018 17:06:29 +0000</pubDate>
      <link>https://dev.to/iandavid/ternary-operators-in-javascript-4inl</link>
      <guid>https://dev.to/iandavid/ternary-operators-in-javascript-4inl</guid>
      <description>&lt;p&gt;&lt;em&gt;Are you tired of repeating yourself while coding? Do you still use the traditional ‘if…else’ blocks? What if I told you, you could write 15 lines of code in 4 lines? Call 1–800-TERNARY-OPERATORS now.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In computer programming, &lt;strong&gt;?:&lt;/strong&gt; is a ternary operator that is part of the syntax for basic conditional expressions in several programming languages.&lt;/p&gt;

&lt;p&gt;Ternary Operators are the shorthand version of if…else statements. It is the only conditional operator in JavaScript that takes three operands.&lt;/p&gt;

&lt;p&gt;The basic syntax for ternary operators is &lt;strong&gt;condition ? expression1 : expression2&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;where the condition is the value to be tested/evaluated,&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;expression1&lt;/strong&gt; can be value(s) of any type to be executed &lt;strong&gt;if the condition is true&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;expression2&lt;/strong&gt; can be value(s) of any type to be executed &lt;strong&gt;if expression1 is false&lt;/strong&gt; i.e fallback value commonly know as ‘else’&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;“ ? ”&lt;/strong&gt; means &lt;strong&gt;“IF”&lt;/strong&gt;, and &lt;strong&gt;“ : “&lt;/strong&gt; means &lt;strong&gt;“else”&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s look at an example&lt;/p&gt;

&lt;p&gt;If we were to determine if one is allowed to drive by their ages, using the if…else statements — it looks something like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var age = 18;

if (age &amp;gt;= 16) {
   alert("You're allowed to drive!");
}

else {
   alert("You should be 16 to drive!");
}

// "You're allowed to drive!"

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

&lt;/div&gt;



&lt;p&gt;Using ternary operators, the same code will look like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var age = 18;

alert (age &amp;gt;= 16) ? "You're allowed to drive!" : "You should be 16 to drive!"

// You're allowed to drive!

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

&lt;/div&gt;



&lt;p&gt;Voila! This block of code will translate to &lt;strong&gt;IF variable ‘age’ is greater or equal to 16, the browser should alert ‘You’re allowed to drive!’, ELSE ‘You should be 16 to drive!’ should be alerted!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Another reason to adopt ternary, is it’s flexibility and miniature size which could fit anywhere in your code. For example, if you want to attach the the result of your string directly to a string, you can easily do that without having to declare your condition separately.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var isMember = true;
'The fee is ' + (isMember ? '$2.00' : '$10.00'); // The fee is $2.00
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;IF isMember is true , ‘The fee is $2.00’, ELSE ‘The fee is $10.00’.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Guess what?! You can also assign the results of the ternary operations to variables. Let’s use the same driving example we used earlier.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var age = 18;
var canDrive = (age &amp;gt;= 16) ? "You're allowed to drive!" : "You should be 16 to drive!";
console.log(canDrive); // "You're allowed to drive!"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we saved the result in a variable and later displayed it in the console.&lt;/p&gt;

&lt;p&gt;There’s just as much possible in ternary operators as in the traditional if…else block. Multiple ternary operators can be chained together to form what we call ‘IF…ELSE IF…ELSE” block.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var year = prompt('Which year was the 2018 World Cup?', '');
alert(year &amp;lt; 2018) ? 'Too early' : (year &amp;gt; 2018) ? 'Too late' : 'Exactly!'; 

// Exactly!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The block of code above will translate if the year is less than 2018, the browser should alert ‘Too early’, or else, if the year is greater than 2018, ‘Too late’ will be displayed, else if it’s not greater or lesser than i.e equal to, then ‘Exactly’ will be displayed.&lt;/p&gt;

&lt;p&gt;You can also have nested if statements:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var statement1 = true;
var statement2 = true;

var check = statement1 ? (statement2 ? "True, Yes!" : "True, False!") : 'False';

console.log(check); // True, Yes!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our eyes scans codes vertically, which indentation and spaces play great parts in helping us read the codes easily. They are not excluded in ternary, and free spaces don’t affect your code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var stop = false, age = 23;

age &amp;gt; 18 ? (
    alert('OK, you can go.')
) : (
    alert('Sorry, you are much too young!')
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code above will work just as well as any ternary evaluation or any if…else operation.&lt;/p&gt;

&lt;p&gt;It is also possible to have multiple operations per case, and separate them with a comma.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var age = 16;

var auth = age &amp;gt; 18 ? (
    alert('OK, you can go.'), 
    // alert returns "undefined", but it will be ignored because
    // isn't the last comma-separated value of the parenthesis
    'APPROVED' // the value to be assigned if age &amp;gt; 18
) : (
    alert('You are much too young!'),
    alert('Sorry :-('),
    // etc. etc.
    'DISAPPROVE' // the value to be assigned if !(age &amp;gt; 18)
);

location.assign(auth); // "DISAPPROVE"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Last but not the least, ternary operations can be used to return values in functions. For example, if we were to write a function to return a value to determine if Chris Pratt is a member of the Marvel Universe or not;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var marvel = true;

function chrisPratt(marvel) {
  if (marvel === true){
return "I am Star-Lord!";
}
else {
return "Have you watched Guardians of the Galaxy?";
}

}
var output = chrisPratt(marvel);
console.log(output); // "I am Star-Lord!"

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

&lt;/div&gt;



&lt;p&gt;Notice we used return multiple times and had to write the if…else statement and embed their values in curly brackets etc, which is great. But it can be shortened using ternary operators. The same code above will be evaluated as such in ternary operation;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var marvel = true;

function chrisPratt(marvel) {
  return (marvel === true) ? "I am Star-Lord!" : "Have you watched Guardians of the Galaxy?";
}
var output = chrisPratt(marvel);
console.log(output); // "I am Star-Lord!"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note: All variables used in a ternary operation should be defined before the operation is created.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;One more thing, it is recommended not to nest ternary operators, because it may be difficult to understand.&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Good programmers write code that humans can understand. So it will be great not to overuse these ternaries expressions, but use them in specific instances. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Ternary Operators may seem obscure or ambiguous for most developer and most people who don’t know, but it’s a great way to refactor your code. And with enough practice, you will be able to understand it whenever you see it in your code or other developer codes.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Explain CSS Positioning Like I'm Five </title>
      <dc:creator>David Okpare</dc:creator>
      <pubDate>Mon, 18 Jun 2018 16:00:56 +0000</pubDate>
      <link>https://dev.to/iandavid/explain-css-positioning-like-im-five--3g77</link>
      <guid>https://dev.to/iandavid/explain-css-positioning-like-im-five--3g77</guid>
      <description>&lt;p&gt;&lt;strong&gt;Please explain how to position elements in CSS like I'm Five&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>explainlikeimfive</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How do web developers/freelancers build websites in time?</title>
      <dc:creator>David Okpare</dc:creator>
      <pubDate>Wed, 25 Apr 2018 10:47:16 +0000</pubDate>
      <link>https://dev.to/iandavid/how-do-web-developersfreelancers-build-websites-in-time-34op</link>
      <guid>https://dev.to/iandavid/how-do-web-developersfreelancers-build-websites-in-time-34op</guid>
      <description>&lt;p&gt;How do web developers or freelancers build websites nowadays in time? &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;From scratch (Pure HTML,CSS, JS etc) or Website Templates or CMS or Website builders?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Please Mention the tools you use or how you build websites :)&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>career</category>
      <category>discuss</category>
    </item>
    <item>
      <title>How much do you need to know or learn before calling yourself a developer?</title>
      <dc:creator>David Okpare</dc:creator>
      <pubDate>Tue, 24 Apr 2018 13:03:58 +0000</pubDate>
      <link>https://dev.to/iandavid/how-much-do-you-need-to-know-or-learn-before-calling-yourself-a-developer-3h11</link>
      <guid>https://dev.to/iandavid/how-much-do-you-need-to-know-or-learn-before-calling-yourself-a-developer-3h11</guid>
      <description>&lt;p&gt;&lt;strong&gt;Can anyone tell me what I need to know or learn before I can call myself a front end developer or a back end developer, maybe even a full stack?&lt;/strong&gt; And is it possible to become one of this developer in a year or two? Any path or resources to learn from will be great :) Thanks!&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>discuss</category>
      <category>webdev</category>
      <category>career</category>
    </item>
  </channel>
</rss>
