<?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: Pcoch</title>
    <description>The latest articles on DEV Community by Pcoch (@pcoch).</description>
    <link>https://dev.to/pcoch</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%2F788125%2F87fdbb02-0fa6-4f22-a1eb-674447edf6a3.jpeg</url>
      <title>DEV Community: Pcoch</title>
      <link>https://dev.to/pcoch</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pcoch"/>
    <language>en</language>
    <item>
      <title>4 Ways to create an object in JS</title>
      <dc:creator>Pcoch</dc:creator>
      <pubDate>Wed, 04 Jan 2023 05:35:31 +0000</pubDate>
      <link>https://dev.to/pcoch/4-ways-to-create-an-object-in-js-5b38</link>
      <guid>https://dev.to/pcoch/4-ways-to-create-an-object-in-js-5b38</guid>
      <description>&lt;p&gt;I often get tripped up learning something new when there are many ways to achieve the same outcome.&lt;/p&gt;

&lt;p&gt;A recent example of this I experienced was learning how to create Objects in JS.&lt;/p&gt;

&lt;p&gt;There are several ways to create objects in JavaScript, and here are 4. My personal preference is to use the Object constructor.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using the Object constructor:
&lt;/h2&gt;

&lt;p&gt;You can use the Object constructor to create an empty object and then add properties to it. For example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9v6kn5opyzr34mqxv0s4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9v6kn5opyzr34mqxv0s4.png" alt="object constructor" width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Using object literals
&lt;/h2&gt;

&lt;p&gt;You can use object literals to create objects with properties directly. For example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqu42be7f0ce7c4jxfqtu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqu42be7f0ce7c4jxfqtu.png" alt="object literal" width="800" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Using the Object.create method
&lt;/h2&gt;

&lt;p&gt;You can use the Object.create method to create an object that has a specified prototype. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let obj = Object.create(Object.prototype)
obj.name = 'John'
obj.age = 30
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Using class syntax
&lt;/h2&gt;

&lt;p&gt;You can use class syntax to define a class and then create objects from that class using the new operator. For example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6tmcqyfy4dayhgbe0abx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6tmcqyfy4dayhgbe0abx.png" alt="class syntax" width="800" height="552"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;A good way to learn is to play around in the console or something like RunJS to get a feel for each approach.&lt;/p&gt;

</description>
      <category>emptystring</category>
    </item>
  </channel>
</rss>
