<?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: weilaiqishi</title>
    <description>The latest articles on DEV Community by weilaiqishi (@kimeng).</description>
    <link>https://dev.to/kimeng</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F999533%2F26ed9421-49b5-4513-a522-77bb2252980b.jpeg</url>
      <title>DEV Community: weilaiqishi</title>
      <link>https://dev.to/kimeng</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kimeng"/>
    <language>en</language>
    <item>
      <title>DATA TYPES IN JAVASCRIPT</title>
      <dc:creator>weilaiqishi</dc:creator>
      <pubDate>Tue, 12 Sep 2023 15:38:11 +0000</pubDate>
      <link>https://dev.to/kimeng/data-types-in-javascript-7em</link>
      <guid>https://dev.to/kimeng/data-types-in-javascript-7em</guid>
      <description>&lt;h2&gt;
  
  
  DATA TYPES
&lt;/h2&gt;

&lt;p&gt;There are six &lt;strong&gt;simple data types&lt;/strong&gt; (also called &lt;strong&gt;primitive types&lt;/strong&gt;) in ECMAScript: Undefined, Null, Boolean, Number, String, and Symbol. There is also one &lt;strong&gt;complex data type&lt;/strong&gt; called Object, which is an unordered list of name–value pairs. ECMAScript’s data types have dynamic aspects that make each single data type behave like several.&lt;/p&gt;

&lt;h3&gt;
  
  
  Undefined
&lt;/h3&gt;

&lt;p&gt;The Undefined type has only one value, which is the special value undefined. When a variable is declared using var or let but not initialized, it is assigned the value of undefined as follows:&lt;br&gt;
&lt;/p&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;message&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the variable message is declared without initializing it. When compared with the &lt;strong&gt;literal value&lt;/strong&gt; of &lt;code&gt;undefined&lt;/code&gt;, the two are equal. This example is identical to the following:&lt;br&gt;
&lt;/p&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;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;undefined&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here the variable message is explicitly initialized to be undefined. This is unnecessary because, by default, any uninitialized variable gets the value of &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Null
&lt;/h3&gt;

&lt;p&gt;The Null type is the second data type that has only one value: the special value &lt;code&gt;null&lt;/code&gt;. Logically, a &lt;code&gt;null&lt;/code&gt; value is an empty object pointer, which is why typeof returns "object" when it’s passed a &lt;code&gt;null&lt;/code&gt; value in the following example:&lt;br&gt;
&lt;/p&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;car&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;car&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "object"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When defining a variable that is meant to later hold an object, it is advisable to initialize the variable &lt;br&gt;
to &lt;code&gt;null&lt;/code&gt; as opposed to anything else. That way, you can explicitly check for the value &lt;code&gt;null&lt;/code&gt; to determine if the variable has been filled with an object reference at a later time, such as in this example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;car&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="c1"&gt;// do something with car&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even though &lt;code&gt;null&lt;/code&gt; and &lt;code&gt;undefined&lt;/code&gt; are related, they have very different uses. As mentioned previously, you should never explicitly set the value of a variable to &lt;code&gt;undefined&lt;/code&gt;, but the same does not hold true for &lt;code&gt;null&lt;/code&gt;. Any time an object is expected but is not available, &lt;code&gt;null&lt;/code&gt; should be used in its place. This helps to keep the paradigm of &lt;code&gt;null&lt;/code&gt; as an empty object pointer and further differentiates it from &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

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