<?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: Prem Jethwa</title>
    <description>The latest articles on DEV Community by Prem Jethwa (@premjethwa).</description>
    <link>https://dev.to/premjethwa</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%2F751334%2Ff949e1b2-7f3c-4ac1-a73d-e6dfddc734b2.png</url>
      <title>DEV Community: Prem Jethwa</title>
      <link>https://dev.to/premjethwa</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/premjethwa"/>
    <language>en</language>
    <item>
      <title>3 ways to add conditional properties to an object [...]</title>
      <dc:creator>Prem Jethwa</dc:creator>
      <pubDate>Thu, 13 Jan 2022 08:21:58 +0000</pubDate>
      <link>https://dev.to/premjethwa/3-ways-to-add-conditional-properties-to-an-object--91n</link>
      <guid>https://dev.to/premjethwa/3-ways-to-add-conditional-properties-to-an-object--91n</guid>
      <description>&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%2Ftqhtavku44brnccvksmr.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%2Ftqhtavku44brnccvksmr.png" alt="3 ways to add conditional properties to an object" width="279" height="181"&gt;&lt;/a&gt;&lt;br&gt;
1 - Using Spread Operator&lt;/p&gt;

&lt;p&gt;What is Spread Operator?&lt;br&gt;
source: &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax"&gt;MDN&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Spread syntax (...)  allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected.&lt;/p&gt;

&lt;p&gt;Example:-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
   ...(someCondition &amp;amp;&amp;amp; {a: “hi”})
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const greed = {
  ...(true) &amp;amp;&amp;amp; {a: “hi”},
  ...(false) &amp;amp;&amp;amp; {b: "bye"},
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;2 - Using Object.assign&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
Object.assign(a, conditionB ? { b: 1 } : null,
                 conditionC ? { c: 2 } : null,
                 conditionD ? { d: 3 } : null);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Object.assign modifies the first argument in-place while returning the updated object: so you can use it in a bigger formula to further manipulate the object.&lt;/p&gt;

&lt;p&gt;You can pass undefined or {} instead of null, with the same result. &lt;/p&gt;

&lt;p&gt;Number has no own enumerable properties, so you could even provide 0 instead since primitive values are wrapped.&lt;/p&gt;

&lt;p&gt;For jQuery Developers&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var a = $.extend({}, {
    b: conditionB ? 5 : undefined,
    c: conditionC ? 5 : undefined,
    // and so on...
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;3 - To remove Undefined Values form Object not remove other falsely values like “”, 0 or null&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const me = {
  name: “prem”,
  age: undefined ,
  height: null
}

const cleanup = JSON.parse(JSON.stringify(me)); // { name: “prem”, height: null }

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

&lt;/div&gt;



&lt;p&gt;Shot Tip:-&lt;/p&gt;

&lt;p&gt;Use !!value to get result in Boolean values if its truthy value the will return true otherwise False.&lt;/p&gt;

&lt;p&gt;Eg:-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let isTruthy = “hello” 

console.log(!!isTruthy) // true

isTruthy = “”; //can be 0 or undefined or null

Console.log(!!isTruthy) // false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>3 steps for setting a typescript project? [typescript setup]</title>
      <dc:creator>Prem Jethwa</dc:creator>
      <pubDate>Wed, 12 Jan 2022 07:19:33 +0000</pubDate>
      <link>https://dev.to/premjethwa/how-to-setup-typescript-57db</link>
      <guid>https://dev.to/premjethwa/how-to-setup-typescript-57db</guid>
      <description>&lt;p&gt;&lt;a href="https://dev.tourl"&gt;&lt;/a&gt;&lt;br&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%2Fogh3bcux6zsbqpsiq01f.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%2Fogh3bcux6zsbqpsiq01f.png" alt="How to setup typescript" width="310" height="163"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is typescript?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A free and open source programming language created and maintained by Microsoft, TypeScript (&lt;a href="https://www.typescriptlang.org"&gt;https://www.typescriptlang.org&lt;/a&gt;). &lt;/p&gt;

&lt;p&gt;It adds types and more to JavaScript.Types can be added gradually. Specifying more types allows TypeScript to detect more errors.&lt;/p&gt;

&lt;p&gt;1 - &lt;strong&gt;Install: -&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Node.js&lt;/em&gt;&lt;br&gt;
&lt;em&gt;Code Editor&lt;/em&gt; (VS code recommended)&lt;br&gt;
&lt;em&gt;Install TypeScript&lt;/em&gt; (install -g typescript)&lt;/p&gt;

&lt;p&gt;2 - &lt;strong&gt;Initialize the project: -&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Create Project folder&lt;/em&gt;&lt;br&gt;
&lt;em&gt;Init&lt;/em&gt; ( npx tsc –init )&lt;/p&gt;

&lt;p&gt;3 - &lt;strong&gt;ProjectName(root directory)/tsconfig.json&lt;/strong&gt; &lt;br&gt;
-- &lt;em&gt;Open &amp;amp; Setup&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
   "compilerOptions":{
      "target":"es5",
      "module":"commonjs",
      "strict":true
   }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There will be many commented out default configurations. This configuration defines the version of JavaScript your TypeScript will be compiled into and the module system for your compiled program. When &lt;code&gt;"strict"&lt;/code&gt; is set to &lt;code&gt;true&lt;/code&gt;, a wide range of type-checking rules are enabled. As a result, your TypeScript program will have fewer errors.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "strict": true,
    _**"outDir": "dist",
    "sourceMap": true**_
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the value of&lt;code&gt;"outDir"&lt;/code&gt;is set to &lt;code&gt;"dist"&lt;/code&gt;, a directory named dist will be created. The compiled JavaScript file will be placed in the dist file when you use &lt;u&gt;npx tsc&lt;/u&gt; to compile your TypeScript file.&lt;/p&gt;

&lt;p&gt;Setting "sourceMap" to true will allow you to quickly solve errors in the original TypeScript file.&lt;/p&gt;

&lt;p&gt;Make sure you save file with .ts extention&lt;br&gt;
Ready to write typescript Code !&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>typescript</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
