<h1 id="javascript-data-types">JavaScript Data Types</h1>
<p><strong>Summary</strong>: in this tutorial, you will learn about the JavaScript data types and their unique characteristics.</p>
<p>JavaScript has the primitive data types:</p>
<ol>
<li><a href="https://www.javascripttutorial.net/javascript-data-types/#null"><code>null</code></a></li>
<li><a href="https://www.javascripttutorial.net/javascript-data-types/#undefined"><code>undefined</code></a></li>
<li><a href="https://www.javascripttutorial.net/javascript-data-types/#boolean"><code>boolean</code></a></li>
<li><a href="https://www.javascripttutorial.net/javascript-data-types/#number"><code>number</code></a></li>
<li><a href="https://www.javascripttutorial.net/javascript-data-types/#string"><code>string</code></a></li>
<li><a href="https://www.javascripttutorial.net/javascript-data-types/#symbol"><code>symbol</code></a> – available from ES2015</li>
<li><code>bigint</code> – available from ES2020</li>
</ol>
<p>and a complex data type <a href="https://www.javascripttutorial.net/javascript-data-types/#object"><code>object</code></a>.</p>
<p><a href="https://www.javascripttutorial.net/wp-content/uploads/2022/01/JavaScript-data-types.svg"><img src="https://www.javascripttutorial.net/wp-content/uploads/2022/01/JavaScript-data-types.svg" alt="JavaScript data types"></a></p>
<p>JavaScript is a dynamically typed language. It means that a <a href="https://www.javascripttutorial.net/javascript-variables/">variable</a> doesn’t associate with a type. In other words, a variable can hold a value of different types. For example:</p>
<pre><code class="hljs language-js"><span class="hljs-keyword">let</span> counter = <span class="hljs-number">120</span>; <span class="hljs-comment">// counter is a number</span>
counter = <span class="hljs-literal">false</span>; <span class="hljs-comment">// counter is now a boolean</span>
counter = <span class="hljs-string">"foo"</span>; <span class="hljs-comment">// counter is now a stringCode language: JavaScript (javascript)</span>
</code></pre>
<p>To get the current type of the value that the variable stores, you use the <code>typeof</code> operator:</p>
<pre><code class="hljs language-js"><span class="hljs-keyword">let</span> counter = <span class="hljs-number">120</span>;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">typeof</span>(counter)); <span class="hljs-comment">// "number"</span>
counter = <span class="hljs-literal">false</span>;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">typeof</span>(counter)); <span class="hljs-comment">// "boolean"</span>
counter = <span class="hljs-string">"Hi"</span>;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">typeof</span>(counter)); <span class="hljs-comment">// "string"Code language: JavaScript (javascript)</span>
</code></pre>
<p>Output:</p>
<pre><code class="hljs language-js"><span class="hljs-string">"number"</span>
<span class="hljs-string">"boolean"</span>
<span class="hljs-string">"string"</span><span class="hljs-title class_">Code</span> <span class="hljs-attr">language</span>: <span class="hljs-title class_">JSON</span> / <span class="hljs-title class_">JSON</span> <span class="hljs-keyword">with</span> <span class="hljs-title class_">Comments</span> (json)
</code></pre>
<h2 id="the-undefined-type">The undefined type</h2>
<p>The <code>undefined</code> type is a primitive type that has only one value <code>undefined</code>. By default, when a variable is declared but not initialized, it is assigned the value of <code>undefined</code>.</p>
<p>Consider the following example:</p>
<pre><code class="hljs language-js"><span class="hljs-keyword">let</span> counter;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(counter); <span class="hljs-comment">// undefined</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-keyword">typeof</span> counter); <span class="hljs-comment">// undefinedCode language: JavaScript (javascript)</span>
</code></pre>
<p>In this example, the <code>counter</code> is a variable. Since <code>counter</code> hasn’t been initialized, it is assigned the value <code>undefined</code>. The type of <code>counter</code> is also <code>undefined</code>.</p>
<p>It’s important to note that the <code>typeof</code> operator also returns <code>undefined</code> when you call it on a variable that hasn’t been declared:</p>
<pre><code class="hljs language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-keyword">typeof</span> undeclaredVar); <span class="hljs-comment">// undefinedCode language: JavaScript (javascript)</span>
</code></pre>
<h2 id="the-null-type">The null type</h2>
<p>The <code>null</code> type is the second primitive data type that also has only one value <code>null</code>. For example:</p>
<pre><code class="hljs language-js"><span class="hljs-keyword">let</span> obj = <span class="hljs-literal">null</span>;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-keyword">typeof</span> obj); <span class="hljs-comment">// objectCode language: JavaScript (javascript)</span>
</code></pre>
<p>JavaScript defines that <code>null</code> is equal to <code>undefined</code> as follows:</p>
<pre><code class="hljs language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-literal">null</span> == <span class="hljs-literal">undefined</span>); <span class="hljs-comment">// trueCode language: JavaScript (javascript)</span>
</code></pre>
<h2 id="the-number-type">The number type</h2>
<p>JavaScript uses the <code>number</code> type to represent both integer and floating-point numbers.</p>
<p>The following statement declares a variable and initializes its value with an integer:</p>
<pre><code class="hljs language-js"><span class="hljs-keyword">let</span> num = <span class="hljs-number">100</span>;<span class="hljs-title class_">Code</span> <span class="hljs-attr">language</span>: <span class="hljs-title class_">JavaScript</span> (javascript)
</code></pre>
<p>To represent a floating-point number, you include a decimal point followed by at least one number. For example:</p>
<pre><code class="hljs language-js"><span class="hljs-keyword">let</span> price= <span class="hljs-number">12.5</span>;
<span class="hljs-keyword">let</span> discount = <span class="hljs-number">0.05</span>;<span class="hljs-title class_">Code</span> <span class="hljs-attr">language</span>: <span class="hljs-title class_">JavaScript</span> (javascript)
</code></pre>
<p>Note that JavaScript automatically converts a floating-point number into an integer number if the number appears to be a whole number.</p>
<p>The reason is that Javascript always wants to use less memory since a floating-point value uses twice as much memory as an integer value. For example:</p>
<pre><code class="hljs language-js"><span class="hljs-keyword">let</span> price = <span class="hljs-number">200.00</span>; <span class="hljs-comment">// interpreted as an integer 200Code language: JavaScript (javascript)</span>
</code></pre>
<p>To get the range of the number type, you use <code>Number.MIN_VALUE</code> and <code>Number.MAX_VALUE</code>. For example:</p>
<pre><code class="hljs language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">Number</span>.<span class="hljs-property">MAX_VALUE</span>); <span class="hljs-comment">// 1.7976931348623157e+308</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">Number</span>.<span class="hljs-property">MIN_VALUE</span>); <span class="hljs-comment">// 5e-324Code language: JavaScript (javascript)</span>
</code></pre>
<p>Also, you can use <code>Infinity</code> and <code>-Infinity</code> to represent the infinite number. For example:</p>
<pre><code class="hljs language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">Number</span>.<span class="hljs-property">MAX_VALUE</span> + <span class="hljs-title class_">Number</span>.<span class="hljs-property">MAX_VALUE</span>); <span class="hljs-comment">// Infinity</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(-<span class="hljs-title class_">Number</span>.<span class="hljs-property">MAX_VALUE</span> - <span class="hljs-title class_">Number</span>.<span class="hljs-property">MAX_VALUE</span>); <span class="hljs-comment">// -InfinityCode language: JavaScript (javascript)</span>
</code></pre>
<h3 id="nan">NaN</h3>
<p><code>NaN</code> stands for Not a Number. It is a special numeric value that indicates an invalid number. For example, the division of a string by a number returns <code>NaN</code>:.</p>
<pre><code class="hljs language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'a'</span>/<span class="hljs-number">2</span>); <span class="hljs-comment">// NaN;Code language: JavaScript (javascript)</span>
</code></pre>
<p>The <code>NaN</code> has two special characteristics:</p>
<ul>
<li>Any operation with <code>NaN</code> returns <code>NaN</code>.</li>
<li>The <code>NaN</code> does not equal any value, including itself.</li>
</ul>
<p>Here are some examples:</p>
<pre><code class="hljs language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">NaN</span>/<span class="hljs-number">2</span>); <span class="hljs-comment">// NaN</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">NaN</span> == <span class="hljs-title class_">NaN</span>); <span class="hljs-comment">// falseCode language: JavaScript (javascript)</span>
</code></pre>
<h2 id="the-string-type">The string type</h2>
<p>In JavaScript, a string is a sequence of zero or more characters. A string literal begins and ends with either a single quote(<code>'</code>) or a double quote (<code>"</code>).</p>
<p>A string that begins with a double quote must end with a double quote. Likewise, a string that begins with a single quote must also end with a single quote:</p>
<pre><code class="hljs language-js"><span class="hljs-keyword">let</span> greeting = <span class="hljs-string">'Hi'</span>;
<span class="hljs-keyword">let</span> message = <span class="hljs-string">"Bye"</span>;<span class="hljs-title class_">Code</span> <span class="hljs-attr">language</span>: <span class="hljs-title class_">JavaScript</span> (javascript)
</code></pre>
<p>If you want to single quote or double quotes in a literal string, you need to use the backslash to escape it. For example:</p>
<pre><code class="hljs language-js"><span class="hljs-keyword">let</span> message = <span class="hljs-string">'I\'m also a valid string'</span>; <span class="hljs-comment">// use \ to escape the single quote (')Code language: JavaScript (javascript)</span>
</code></pre>
<p>JavaScript strings are immutable. This means that it cannot be modified once created. However, you can create a new string from an existing string. For example:</p>
<pre><code class="hljs language-js"><span class="hljs-keyword">let</span> str = <span class="hljs-string">'JavaScript'</span>;
str = str + <span class="hljs-string">' String'</span>;<span class="hljs-title class_">Code</span> <span class="hljs-attr">language</span>: <span class="hljs-title class_">JavaScript</span> (javascript)
</code></pre>
<p>In this example:</p>
<ul>
<li>First, declare the <code>str</code> variable and initialize it to a string of <code>'JavaScript'</code>.</li>
<li>Second, use the <code>+</code> operator to combine <code>'JavaScript'</code> with <code>' String'</code> to make its value as <code>'Javascript String'</code>.</li>
</ul>
<p>Behind the scene, the JavaScript engine creates a new string that holds the new string <code>'JavaScript String'</code> and destroys the original strings <code>'JavaScript'</code> and <code>' String'</code>.</p>
<h2 id="the-boolean-type">The boolean type</h2>
<p>The <code>boolean</code> type has two literal values: <code>true</code> and <code>false</code> in lowercase. The following example declares two variables that hold the boolean values.</p>
<pre><code class="hljs language-js"><span class="hljs-keyword">let</span> inProgress = <span class="hljs-literal">true</span>;
<span class="hljs-keyword">let</span> completed = <span class="hljs-literal">false</span>;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-keyword">typeof</span> completed); <span class="hljs-comment">// booleanCode language: JavaScript (javascript)</span>
</code></pre>
<p>JavaScript allows values of other types to be converted into boolean values of <code>true</code> or <code>false</code>.</p>
<p>To convert a value of another data type into a boolean value, you use the <a href="https://www.javascripttutorial.net/javascript-boolean/"><code>Boolean()</code></a> function. The following table shows the conversion rules:</p>
<table>
<thead>
<tr>
<th align="left">Type</th>
<th align="left">true</th>
<th align="left">false</th>
</tr>
</thead>
<tbody><tr>
<td align="left">string</td>
<td align="left">non-empty string</td>
<td align="left">empty string</td>
</tr>
<tr>
<td align="left">number</td>
<td align="left">non-zero number and Infinity</td>
<td align="left">0, NaN</td>
</tr>
<tr>
<td align="left">object</td>
<td align="left">non-null object</td>
<td align="left">null</td>
</tr>
<tr>
<td align="left">undefined</td>
<td align="left"></td>
<td align="left">undefined</td>
</tr>
</tbody></table>
<p>For example:</p>
<pre><code class="hljs language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">Boolean</span>(<span class="hljs-string">'Hi'</span>));<span class="hljs-comment">// true</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">Boolean</span>(<span class="hljs-string">''</span>)); <span class="hljs-comment">// false</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">Boolean</span>(<span class="hljs-number">20</span>)); <span class="hljs-comment">// true</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">Boolean</span>(<span class="hljs-title class_">Infinity</span>)); <span class="hljs-comment">// true</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">Boolean</span>(<span class="hljs-number">0</span>)); <span class="hljs-comment">// false</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">Boolean</span>({<span class="hljs-attr">foo</span>: <span class="hljs-number">100</span>})); <span class="hljs-comment">// true on non-empty object</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">Boolean</span>(<span class="hljs-literal">null</span>));<span class="hljs-comment">// falseCode language: JavaScript (javascript)</span>
</code></pre>
<h2 id="the-symbol-type">The symbol type</h2>
<p>JavaScript added a primitive type in ES6: the <code>symbol</code>. Different from other primitive types, the <code>symbol</code> type does not have a literal form.</p>
<p>To create a symbol, you call the <code>Symbol</code> function as follows:</p>
<pre><code class="hljs language-js"><span class="hljs-keyword">let</span> s1 = <span class="hljs-title class_">Symbol</span>();<span class="hljs-title class_">Code</span> <span class="hljs-attr">language</span>: <span class="hljs-title class_">JavaScript</span> (javascript)
</code></pre>
<p>The <code>Symbol</code> function creates a new unique value every time you call it.</p>
<pre><code class="hljs language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">Symbol</span>() == <span class="hljs-title class_">Symbol</span>()); <span class="hljs-comment">// falseCode language: JavaScript (javascript)</span>
</code></pre>
<p>Note that you’ll learn more about symbols in the <a href="https://www.javascripttutorial.net/es6/symbol/">symbol tutorial</a>.</p>
<h2 id="the-bigint-type">The bigint type</h2>
<p>The <code>bigint</code> type represents the whole numbers that are larger than 253 – 1. To form a <code>bigint</code> literal number, you append the letter <code>n</code> at the end of the number:</p>
<pre><code class="hljs language-js"><span class="hljs-keyword">let</span> pageView = <span class="hljs-number">9007199254740991n</span>;
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">typeof</span>(pageView)); <span class="hljs-comment">// 'bigint'Code language: JavaScript (javascript)</span>
</code></pre>
<p>And you’ll learn more about the <a href="https://www.javascripttutorial.net/es-next/javascript-bigint/"><code>bigint</code> type here</a>.</p>
<h2 id="the-object-type">The object type</h2>
<p>In JavaScript, an <a href="https://www.javascripttutorial.net/home/javascript-objects/">object</a> is a collection of <a href="https://www.javascripttutorial.net/home/javascript-object-properties/">properties</a>, where each property is defined as a key-value pair.</p>
<p>The following example defines an empty object using the object literal syntax:</p>
<pre><code class="hljs language-js"><span class="hljs-keyword">let</span> emptyObject = {};<span class="hljs-title class_">Code</span> <span class="hljs-attr">language</span>: <span class="hljs-title class_">JavaScript</span> (javascript)
</code></pre>
<p>The following example defines the <code>person</code> object with two properties: <code>firstName</code> and <code>lastName</code>.</p>
<pre><code class="hljs language-js"><span class="hljs-keyword">let</span> person = {
<span class="hljs-attr">firstName</span>: <span class="hljs-string">'John'</span>,
<span class="hljs-attr">lastName</span>: <span class="hljs-string">'Doe'</span>
};<span class="hljs-title class_">Code</span> <span class="hljs-attr">language</span>: <span class="hljs-title class_">JavaScript</span> (javascript)
</code></pre>
<p>A property name of an object can be any string. You can use quotes around the property name if it is not a valid identifier.</p>
<p>For example, if the person object has a property <code>first-name</code>, you must place it in the quotes such as <code>"first-name"</code>.</p>
<p>A property of an object can hold an object. For example:</p>
<pre><code class="hljs language-js"><span class="hljs-keyword">let</span> contact = {
<span class="hljs-attr">firstName</span>: <span class="hljs-string">'John'</span>,
<span class="hljs-attr">lastName</span>: <span class="hljs-string">'Doe'</span>,
<span class="hljs-attr">email</span>: <span class="hljs-string">'john.doe@example.com'</span>,
<span class="hljs-attr">phone</span>: <span class="hljs-string">'(408)-555-9999'</span>,
<span class="hljs-attr">address</span>: {
<span class="hljs-attr">building</span>: <span class="hljs-string">'4000'</span>,
<span class="hljs-attr">street</span>: <span class="hljs-string">'North 1st street'</span>,
<span class="hljs-attr">city</span>: <span class="hljs-string">'San Jose'</span>,
<span class="hljs-attr">state</span>: <span class="hljs-string">'CA'</span>,
<span class="hljs-attr">country</span>: <span class="hljs-string">'USA'</span>
}
}<span class="hljs-title class_">Code</span> <span class="hljs-attr">language</span>: <span class="hljs-title class_">JavaScript</span> (javascript)
</code></pre>
<p>The <code>contact</code> object has the <code>firstName</code>, <code>lastName</code>, <code>email</code>, <code>phone</code>, and <code>address</code> properties.</p>
<p>The <code>address</code> property itself holds an object that has <code>building</code>, <code>street</code>, <code>city</code>, <code>state</code>, and <code>country</code> properties.</p>
<p>To access a object’s property, you can use</p>
<ul>
<li>The dot notation (<code>.</code>)</li>
<li>The array-like notation (<code>[]</code>).</li>
</ul>
<p>The following example uses the dot notation (<code>.</code>) to access the <code>firstName</code> and <code>lastName</code> properties of the <code>contact</code> object.</p>
<pre><code class="hljs language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(contact.<span class="hljs-property">firstName</span>);
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(contact.<span class="hljs-property">lastName</span>);<span class="hljs-title class_">Code</span> <span class="hljs-attr">language</span>: <span class="hljs-variable constant_">CSS</span> (css)
</code></pre>
<p>If you reference a property that does not exist, you’ll get an <code>undefined</code> value. For example:</p>
<pre><code class="hljs language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(contact.<span class="hljs-property">age</span>); <span class="hljs-comment">// undefinedCode language: JavaScript (javascript)</span>
</code></pre>
<p>The following example uses the array-like notation to access the <code>email</code> and <code>phone</code> properties of the <code>contact</code> object.</p>
<pre><code class="hljs language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(contact[<span class="hljs-string">'phone'</span>]); <span class="hljs-comment">// '(408)-555-9999'</span>
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(contact[<span class="hljs-string">'email'</span>]); <span class="hljs-comment">// 'john.doe@example.com'Code language: JavaScript (javascript)</span>
</code></pre>
<h2 id="summary">Summary</h2>
<ul>
<li>JavaScript has the primitive types: <code>number</code>, <code>string</code>, <code>boolean</code>, <code>null</code>, <code>undefined</code>, <code>symbol</code> and <code>bigint</code> and a complex type: <code>object</code>.</li>
</ul>