<?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: Mahmoud Awd</title>
    <description>The latest articles on DEV Community by Mahmoud Awd (@mahmoudawd).</description>
    <link>https://dev.to/mahmoudawd</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%2F1055887%2F2e9b0258-4c65-4e4a-8c18-536636d61938.png</url>
      <title>DEV Community: Mahmoud Awd</title>
      <link>https://dev.to/mahmoudawd</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mahmoudawd"/>
    <language>en</language>
    <item>
      <title>Introduction to Symbols Using Type Script</title>
      <dc:creator>Mahmoud Awd</dc:creator>
      <pubDate>Fri, 31 Mar 2023 02:32:46 +0000</pubDate>
      <link>https://dev.to/mahmoudawd/introduction-to-symbols-using-type-script-1727</link>
      <guid>https://dev.to/mahmoudawd/introduction-to-symbols-using-type-script-1727</guid>
      <description>&lt;p&gt;*&lt;em&gt;&lt;em&gt;Introduction to Symbols Using Type Script&lt;/em&gt; *&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Symbols are a unique data type in TypeScript that allows for the creation of unambiguous and immutable values. Unlike other primitive data types such as strings, numbers, and &lt;code&gt;booleans&lt;/code&gt;, symbols are not &lt;code&gt;coercible&lt;/code&gt; and cannot be converted to other types. They are often used to create private members of classes and to define constants that cannot be changed.&lt;/p&gt;

&lt;p&gt;Creating a symbol in Type Script is simple, using the &lt;code&gt;Symbol()&lt;/code&gt; function. The function takes an optional string parameter that serves as a description of the symbol, but this parameter has no impact on the value of the symbol itself.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mySymbol = Symbol();

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

&lt;/div&gt;



&lt;p&gt;Symbols can be used as property keys in objects, using square bracket notation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const myObject = {

};

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

&lt;/div&gt;



&lt;p&gt;This allows for the creation of private members in classes, as symbols cannot be accessed from outside the class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class MyClass {
  private [mySymbol] = 'secret';

  public getSecret(): string {
    return this[mySymbol];
  }
}

const myClassInstance = new MyClass();

console.log(myClassInstance.getSecret()); // 'secret'
console.log(myClassInstance[mySymbol]); // ERROR: Property 'Symbol()' does not exist on type 'MyClass'

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

&lt;/div&gt;



&lt;p&gt;Symbols can also be used to define constants, to ensure that the value cannot be changed or overwritten.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const MY_CONSTANT = Symbol();

function myFunction(input: symbol) {
  if (input === MY_CONSTANT) {
    console.log('This is my constant');
  } else {
    console.log('This is not my constant');
  }
}

myFunction(MY_CONSTANT); // 'This is my constant'
myFunction(Symbol()); // 'This is not my constant'

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

&lt;/div&gt;



&lt;p&gt;In conclusion, symbols are a powerful feature of TypeScript that can be used to create unambiguous and immutable values, and to define private members and constants. They are particularly useful in object-oriented programming, where they can be used to ensure encapsulation and protect sensitive data.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>symbols</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
