{"id":252,"date":"2020-08-14T20:54:47","date_gmt":"2020-08-14T20:54:47","guid":{"rendered":"https:\/\/datatype.co.in\/blog\/?p=252"},"modified":"2020-08-16T15:55:21","modified_gmt":"2020-08-16T15:55:21","slug":"data-types-in-javascript","status":"publish","type":"post","link":"https:\/\/datatype.co.in\/blog\/data-types-in-javascript\/","title":{"rendered":"Data Types in JavaScript"},"content":{"rendered":"\n<p><strong>What is Data Type?<\/strong><\/p>\n\n\n\n<p>In programming the term <strong>Data Type<\/strong> refers to type or classification of data that a variable can hold and operations that can be performed on it. It hints the interpreter or compiler how the data is intended to be used by programmer. <\/p>\n\n\n\n<p>In JavaScript, as per latest ECMAScript standard there are nine different Data Types which can be grouped into three broad categories &#8211;<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>Primitive or Primary &#8211;<\/strong> <code>undefined<\/code>, <code>Boolean<\/code>, <code>Number<\/code>, <code>String<\/code>, <code>BigInt<\/code> and <code>Symbol<\/code><\/li><li><strong>Non-data Structure or Composite &#8211;<\/strong> <code>Object<\/code> and <code>Function<\/code><\/li><li><strong>Special &#8211; <\/strong><code>null<\/code><\/li><\/ol>\n\n\n\n<p>Let&#8217;s understand the Data Types in detail.<\/p>\n\n\n\n<p><strong>undefined:<\/strong><\/p>\n\n\n\n<p> A variable that has not been assigned a value has the value&nbsp;<code>undefined<\/code><\/p>\n\n\n\n<p><strong>Boolean:<\/strong><\/p>\n\n\n\n<p>Boolean Data Type represents logical entity that holds two values &#8211; <code>true <\/code>and <code>false<\/code><\/p>\n\n\n\n<p><strong>Number:<\/strong><\/p>\n\n\n\n<p> A Number type variable can have values: (numbers between -(2<sup>53<\/sup>&nbsp;\u2212&nbsp;1) and 2<sup>53<\/sup>&nbsp;\u2212&nbsp;1). In addition to representing floating-point numbers, the number type has three symbolic values:&nbsp;<code>+Infinity<\/code>,&nbsp;<code>-Infinity<\/code>, and <code>NaN<\/code> (<strong>N<\/strong>ot a&nbsp;<strong>N<\/strong>umber). <\/p>\n\n\n\n<p><strong>String:<\/strong><\/p>\n\n\n\n<p>It is used to represent textual data.<\/p>\n\n\n\n<p><strong>BigInt:<\/strong><\/p>\n\n\n\n<p> It is a numeric primitive which can have integer values larger than the range supported by the&nbsp;<code>Number<\/code>&nbsp;data type with arbitrary precision. BigInt make it possible to correctly perform integer arithmetic without overflowing issues. A&nbsp;<code>BigInt<\/code>&nbsp;is created by appending&nbsp;<code>n<\/code>&nbsp;to the end of an integer or by calling the constructor. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1234567890123456789n * 12345n; \/\/ Result: 15240740603574074060205n<\/code><\/pre>\n\n\n\n<p><strong>Symbol:<\/strong><\/p>\n\n\n\n<p> A Symbol is a&nbsp;<strong>unique<\/strong>&nbsp;and&nbsp;<strong>immutable<\/strong>&nbsp;primitive value and may be used as an identifier for object properties. Symbol variables are created by using <code>Symbol()<\/code> with optional String argument as it&#8217;s description (without <em>new <\/em>keyword). <code>Symbol()<\/code> returns unique value every time it is called. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let sym1 = Symbol()\nlet sym2 = Symbol('foo')\nlet sym3 = Symbol('foo')\n\nlet sym4 = new Symbol(); \/\/ Throws TypeError\nSymbol('foo') === Symbol('foo')  \/\/ false<\/code><\/pre>\n\n\n\n<p><strong>null:<\/strong><\/p>\n\n\n\n<p><code>null<\/code> is a primitive data type that has only single value <em>null<\/em>. Null means nothing or something that doesn&#8217;t exist.<\/p>\n\n\n\n<p>\n\nPrimitive Data Types can hold only a single value whereas Composite Data Types can hold multiple values. \n\n<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var name = \"Sam\"; \/\/ String\nvar age = 25; \/\/ Number\nvar isDeveloper = false; \/\/ Boolean\n\n\/\/ Function\nvar a;\nalert(a); \/\/ Output : undefined\n\nvar a = null;\nalert(a); \/\/ Output: null\n\n\/\/ Object\nvar obj = {\n   \"name\": \"Tom\", \n   \"age\": 25,\n   \"isDeveloper\": false\n} <\/code><\/pre>\n\n\n\n<p><strong>Difference between <code>null <\/code>and <code>undefined<\/code>:<\/strong><\/p>\n\n\n\n<p>It is important to note that <em>undefined <\/em>and <em>null<\/em> are two distinct types &#8211; <em>undefined&nbsp;<\/em>is a type itself (undefined) while <em>null&nbsp;<\/em>is an object. Using the <code>typeof<\/code> operator you can easily determine type of a variable. Checkout the following examples:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var x;\nalert(x); \/\/shows undefined\nalert(typeof x); \/\/shows undefined  \n\nvar x = null;\nalert(x); \/\/shows null\nalert(typeof x); \/\/shows Object<\/code><\/pre>\n\n\n\n<p> <strong>In JavaScript <\/strong><code><strong>Array<\/strong><\/code><strong> belongs to <\/strong><code><strong>Object<\/strong><\/code><strong> Data Type<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var fruits = [\"Apple\", \"Orange\", \"Banana\"];\nalert(typeof fruits); \/\/shows Object<\/code><\/pre>\n\n\n\n<p>Now, here are few <strong>JS<\/strong> <strong>coding interview questions<\/strong> for you. What will be the output of the following code in JavaScript? <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>null === undefined\nnull == undefined\nnull === null\n+0 === -0\n25 \/ +0\n25 \/ -0 <\/code><\/pre>\n\n\n\n<p>Post your answers in the comment box below.<\/p>\n\n\n\n<p><strong>Dynamic Typing in JavaScript:<\/strong><\/p>\n\n\n\n<p>Unlike other programming languages,  JavaScript is a&nbsp;<em>loosely typed&nbsp;<\/em>and&nbsp;<em>dynamic<\/em>&nbsp;language. Variables in JavaScript are not directly associated with any particular value type, and any variable can be assigned (and re-assigned) values of all types: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var foo = \"bar\"; \/\/ foo is string\nfoo = 15; \/\/ foo is now a number\nfoo = false; \/\/ foo is now a boolean\n\nvar x; \/\/ variable x can hold any value<\/code><\/pre>\n\n\n\n<p>JavaScript is strange, isn&#8217;t it? In the upcoming articles, I will cover many more interesting topics. Stay tuned.<\/p>\n\n\n\n<p>You can try all above examples here: <a href=\"https:\/\/datatype.co.in\/code-editor\">Datatype Code Editor<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is Data Type? In programming the term Data Type refers to type or classification of data that a variable can hold and operations that can be performed on it.&nbsp;[ &hellip; ]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[134,129,132,131,130,140,138,100,139,141,137,135],"class_list":["post-252","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-composite-data-types","tag-data-type","tag-data-types-in-javascript","tag-data-types-in-js","tag-datatype","tag-dynamic-typing-in-js","tag-js-bigint-example","tag-js-interview-question","tag-js-symbol-example","tag-js-typeof-example","tag-typeof-null","tag-undefined-and-null-in-js","list-style-post"],"_links":{"self":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/252","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/comments?post=252"}],"version-history":[{"count":45,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/252\/revisions"}],"predecessor-version":[{"id":315,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/252\/revisions\/315"}],"wp:attachment":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/media?parent=252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/categories?post=252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/tags?post=252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}