<?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: Vipin Sharma</title>
    <description>The latest articles on DEV Community by Vipin Sharma (@js_dev_e47fea29f1be16c02e).</description>
    <link>https://dev.to/js_dev_e47fea29f1be16c02e</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%2F2725416%2F40a82578-0b58-4e16-a77c-aa50148c9859.png</url>
      <title>DEV Community: Vipin Sharma</title>
      <link>https://dev.to/js_dev_e47fea29f1be16c02e</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/js_dev_e47fea29f1be16c02e"/>
    <language>en</language>
    <item>
      <title>JavaScript Objects: A Quirky Bunch of Code Characters 🎭</title>
      <dc:creator>Vipin Sharma</dc:creator>
      <pubDate>Thu, 30 Jan 2025 12:11:26 +0000</pubDate>
      <link>https://dev.to/js_dev_e47fea29f1be16c02e/javascript-objects-a-quirky-bunch-of-code-characters-57fp</link>
      <guid>https://dev.to/js_dev_e47fea29f1be16c02e/javascript-objects-a-quirky-bunch-of-code-characters-57fp</guid>
      <description>&lt;p&gt;Welcome to the chaotic yet fascinating universe of &lt;strong&gt;JavaScript objects&lt;/strong&gt;! If you think of them as simple data containers, think again. &lt;strong&gt;JavaScript objects&lt;/strong&gt; are more like an unpredictable group of roommates—sometimes helpful, sometimes messy, and always full of surprises. Let’s explore their quirks with some fun and practical &lt;strong&gt;JavaScript coding examples&lt;/strong&gt;!&lt;/p&gt;

&lt;h2&gt;
  
  
  1. The Basic JavaScript Object – The Obvious Roommate 😴
&lt;/h2&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;predictablePerson&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bob&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;favoriteFood&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Plain toast&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;hobby&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Filing taxes on time&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="nx"&gt;predictablePerson&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: Bob&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ah, Bob. The kind of guy who follows the rules, never forgets a deadline, and thinks "fun" is reading the entire terms and conditions document.&lt;/p&gt;

&lt;h3&gt;
  
  
  ✅ DO:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Use objects to logically group related data.&lt;/li&gt;
&lt;li&gt;Declare objects with &lt;code&gt;const&lt;/code&gt; to avoid accidental changes.&lt;/li&gt;
&lt;li&gt;Keep property names clear and meaningful.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  ❌ DON'T:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Don’t mutate objects unnecessarily—JavaScript has a long memory.&lt;/li&gt;
&lt;li&gt;Don’t create excessively complex objects unless absolutely needed.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  2. JavaScript Object with Methods – The Entertainer 🤹‍♂️
&lt;/h2&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;funnyPerson&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Eddie&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;favoritePrank&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Whoopee cushion&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nf"&gt;pullPrank&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`Haha! Gotcha with a &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;favoritePrank&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;!`&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="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;funnyPerson&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pullPrank&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// Output: Haha! Gotcha with a Whoopee cushion!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Objects with methods are like that one friend who always has a new trick up their sleeve. 🎭&lt;/p&gt;

&lt;h3&gt;
  
  
  ✅ DO:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Use methods to encapsulate behavior inside objects.&lt;/li&gt;
&lt;li&gt;Use the shorthand syntax for cleaner function definitions.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  ❌ DON'T:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Don’t define functions outside an object if they belong inside it.&lt;/li&gt;
&lt;li&gt;Don’t forget how &lt;code&gt;this&lt;/code&gt; works—it can be unpredictable if misused.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  3. JavaScript Objects Are Reference Types – The Evil Twin 👿
&lt;/h2&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;original&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;mood&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;chill&lt;/span&gt;&lt;span class="dl"&gt;"&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;duplicate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;original&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;duplicate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mood&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;stressed&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="nx"&gt;original&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mood&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: stressed&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Surprise! &lt;strong&gt;JavaScript objects&lt;/strong&gt; are &lt;strong&gt;reference types&lt;/strong&gt;, meaning if you update &lt;code&gt;duplicate&lt;/code&gt;, &lt;code&gt;original&lt;/code&gt; changes too. It's like your evil twin taking over your social media account and posting weird selfies.&lt;/p&gt;

&lt;h3&gt;
  
  
  ✅ DO:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;{ ...object }&lt;/code&gt; (spread operator) or &lt;code&gt;Object.assign({}, object)&lt;/code&gt; to create independent copies.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;structuredClone(object)&lt;/code&gt; for deep copying.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  ❌ DON'T:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Don’t assume &lt;code&gt;let duplicate = original;&lt;/code&gt; makes a fresh copy—it’s just another name for the same object.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  4. The Mystery of &lt;code&gt;this&lt;/code&gt; in JavaScript Objects – The Lost Tourist 🧭
&lt;/h2&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;lostPerson&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Chris&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nf"&gt;askWhereAmI&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="k"&gt;this&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="nx"&gt;lostPerson&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;askWhereAmI&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Output: { name: "Chris", askWhereAmI: [Function] }&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;misplacedFunction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;lostPerson&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;askWhereAmI&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nf"&gt;misplacedFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Output: Window (or undefined in strict mode)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;JavaScript’s &lt;code&gt;this&lt;/code&gt; is like a tourist who forgot their map—it doesn’t always know where it belongs.&lt;/p&gt;

&lt;h3&gt;
  
  
  ✅ DO:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;.bind(this)&lt;/code&gt; when passing functions around.&lt;/li&gt;
&lt;li&gt;Use arrow functions to avoid losing &lt;code&gt;this&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  ❌ DON'T:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Don’t assume &lt;code&gt;this&lt;/code&gt; always points to the object—it changes depending on how the function is called.&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Understanding Hoisting in JavaScript</title>
      <dc:creator>Vipin Sharma</dc:creator>
      <pubDate>Thu, 16 Jan 2025 19:39:31 +0000</pubDate>
      <link>https://dev.to/js_dev_e47fea29f1be16c02e/understanding-hoisting-in-javascript-4dld</link>
      <guid>https://dev.to/js_dev_e47fea29f1be16c02e/understanding-hoisting-in-javascript-4dld</guid>
      <description>&lt;p&gt;JavaScript is a flexible and powerful language with many unique features, and one of the most intriguing is hoisting. Understanding how hoisting works is crucial for writing clean, bug-free JavaScript code. In this article, we’ll dive into what hoisting is, how it works, and its implications for variable and function declarations.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Hoisting?
&lt;/h2&gt;

&lt;p&gt;Hoisting is a behavior in JavaScript where variable and function declarations are moved to the top of their scope during the compilation phase, before the code is executed. This means you can use variables and functions before they are declared in the code.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Key Point: Only the declarations are hoisted, not the initializations.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  How Hoisting Works
&lt;/h2&gt;

&lt;p&gt;To better understand hoisting, let’s look at some examples:&lt;/p&gt;

&lt;h2&gt;
  
  
  Variable Hoisting
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(myVar); // Output: undefined
var myVar = 10;
console.log(myVar); // Output: 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Behind the scenes, the code is interpreted as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var myVar; // Declaration is hoisted to the top
console.log(myVar); // undefined (variable is declared but not initialized)
myVar = 10; // Initialization happens in place
console.log(myVar); // 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Function Hoisting
&lt;/h2&gt;

&lt;p&gt;Function declarations are fully hoisted, meaning you can call the function before it’s defined:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;greet(); // Output: Hello, World!
function greet() {
  console.log('Hello, World!');
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This works because the entire function is hoisted to the top of its scope.&lt;/p&gt;

&lt;h2&gt;
  
  
  Hoisting with var, let, and const
&lt;/h2&gt;

&lt;h2&gt;
  
  
  var Hoisting
&lt;/h2&gt;

&lt;p&gt;Variables declared with var are hoisted and initialized with undefined:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(a); // undefined
var a = 5;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  let and const Hoisting
&lt;/h2&gt;

&lt;p&gt;Variables declared with let and const are also hoisted, but they are not initialized. They remain in a "temporal dead zone" (TDZ) until the code execution reaches the declaration.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(b); // ReferenceError: Cannot access 'b' before initialization
let b = 10;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This behavior prevents the use of let and const variables before they are declared, reducing bugs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Function Declarations vs. Function Expressions
&lt;/h2&gt;

&lt;p&gt;Function Declarations&lt;br&gt;
Function declarations are hoisted entirely, meaning you can call them before their definition:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;greet(); // Output: Hello!
function greet() {
  console.log('Hello!');
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Function Expressions
&lt;/h2&gt;

&lt;p&gt;Function expressions, including arrow functions, are treated as variables. Only the variable declaration is hoisted, not the function assignment:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sayHello(); // TypeError: sayHello is not a function

var sayHello = function () {
  console.log('Hello!');
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Best Practices for Hoisting
&lt;/h2&gt;

&lt;p&gt;To write clean and predictable code, consider the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
Declare Variables at the Top: Avoid confusion by declaring variables at the beginning of their scope.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var name;
console.log(name);
name = 'Alice';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
Use let and const: Prefer let and const over var to avoid unexpected hoisting behaviors.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let age = 25;
const pi = 3.14;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
Define Functions Before Use: While function declarations are hoisted, defining functions before calling them improves code readability.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Hoisting is an integral part of JavaScript that impacts how your code is interpreted. By understanding its nuances and adhering to best practices, you can avoid unexpected behavior and write more robust, maintainable code. Remember that while hoisting can seem like a quirky feature, leveraging let, const, and clear function declarations will keep your code predictable and clean.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
