{"id":838,"date":"2019-04-05T10:37:00","date_gmt":"2019-04-05T10:37:00","guid":{"rendered":"https:\/\/staging.infragistics.com\/blogs\/?p=838"},"modified":"2025-02-26T08:43:19","modified_gmt":"2025-02-26T08:43:19","slug":"undeclared-variable-in-javascript","status":"publish","type":"post","link":"https:\/\/www.infragistics.com\/blogs\/undeclared-variable-in-javascript","title":{"rendered":"Type of Undeclared Variable in JavaScript: What is it?"},"content":{"rendered":"\n<p>Have you ever thought, what is <em>type of undeclared variable<\/em> in JavaScript? I know, the first thing that might come to mind is: how can an undeclared variable have a type? Yes, in JavaScript it is possible.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/static.infragistics.com\/marketing\/Blogs\/Migration\/00\/00\/00\/09\/43\/6175.pastedimage1554461223622v1.png\" alt=\" undeclared variable in JavaScript\" title=\"undeclared variable in JavaScript\"\/><\/figure>\n\n\n\n<p>To understand it, let us start with understanding types in JavaScript. There are seven built in types in JavaScript. They are as follows:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>null<\/li>\n\n\n\n<li>undefined<\/li>\n\n\n\n<li>boolean<\/li>\n\n\n\n<li>number<\/li>\n\n\n\n<li>string<\/li>\n\n\n\n<li>object<\/li>\n\n\n\n<li>symbol (added on ES6)<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/static.infragistics.com\/marketing\/Blogs\/Migration\/00\/00\/00\/09\/43\/6175.pastedimage1554461271525v2.png\" alt=\" \"\/><\/figure>\n\n\n\n<p>Each variable with assigned value has a type. Let us consider the code listed below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">var\u00a0foo\u00a0=\u00a09;\nconsole.log(typeof\u00a0(foo));\u00a0\/\/\u00a0number\nvar\u00a0koo;\nconsole.log(typeof\u00a0(koo));\u00a0\/\/\u00a0undefined\nvar\u00a0too\u00a0=\u00a0'Infragistics';\nconsole.log(typeof\u00a0(too));\u00a0\/\/\u00a0string<\/pre>\n\n\n\n<p>As you can see in the above snippet, if there is no value assigned then type of variable is <strong>undefined<\/strong>. &nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/static.infragistics.com\/marketing\/Blogs\/Migration\/00\/00\/00\/09\/43\/0880.pastedimage1554461515361v3.png\" alt=\" As you can see in the above snippet, if there is no value assigned then type of variable is undefined.\" title=\"As you can see in the above snippet, if there is no value assigned then type of variable is undefined.\"\/><\/figure>\n\n\n\n<p>So far so good, we saw that variable with no assigned value is undefined.&nbsp; Let us consider the next code snippet:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">var\u00a0koo;\nconsole.log(koo);\u00a0\/\/\u00a0undefiend\nconsole.log(typeof\u00a0(koo));\u00a0\/\/\u00a0undefined<\/pre>\n\n\n\n<p>We have created a variable koo and have not assigned any value to it.&nbsp; Now both value and type of koo are set to undefined.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/static.infragistics.com\/marketing\/Blogs\/Migration\/00\/00\/00\/09\/43\/0880.pastedimage1554461600420v4.png\" alt=\" We have created a variable koo and have not assigned any value to it.\u00a0 Now both value and type of koo are set to undefined.\" title=\"We have created a variable koo and have not assigned any value to it.\u00a0 Now both value and type of koo are set to undefined.\"\/><\/figure>\n\n\n\n<p>Now that you understand type and value associated with undefined, let\u2019s move on to null. In JavaScript, null is a primitive type.&nbsp; However, type of null value is object. Consider code listed below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">var\u00a0foo\u00a0=\u00a0null;\nconsole.log(foo);\u00a0\/\/\u00a0null\nconsole.log(typeof\u00a0(foo));\u00a0\/\/\u00a0object<\/pre>\n\n\n\n<p>You may consider it as a legacy bug that type of null value is object in JavaScript.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/static.infragistics.com\/marketing\/Blogs\/Migration\/00\/00\/00\/09\/43\/0880.pastedimage1554461683949v5.png\" alt=\" You may consider it as a legacy bug that type of null value is object in JavaScript.\" title=\"You may consider it as a legacy bug that type of null value is object in JavaScript.\"\/><\/figure>\n\n\n\n<p>Finally, yet equally as important, in JavaScript a variable which is not declared also has a type. The declared variable type is undefined.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">console.log(foo);\/\/\u00a0error\u00a0foo\u00a0is\u00a0not\u00a0defined\nconsole.log(typeof\u00a0(foo));\u00a0\/\/\u00a0undefined\u00a0<\/pre>\n\n\n\n<p>When you read the value of variable, which is&nbsp; not declared, JavaScript will&nbsp; return an error \u201cnot defined\u201d, and will return its type as undefined<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/static.infragistics.com\/marketing\/Blogs\/Migration\/00\/00\/00\/09\/43\/3513.pastedimage1554461931866v6.png\" alt=\" When you read the value of variable, which is\u00a0 not declared, JavaScript will\u00a0 return an error \u201cnot defined\u201d, and will return its type as undefined\" title=\"When you read the value of variable, which is\u00a0 not declared, JavaScript will\u00a0 return an error \u201cnot defined\u201d, and will return its type as undefined\"\/><\/figure>\n\n\n\n<p>In addition, keep in mind that in world of JavaScript not defined is not the same as undefined. &nbsp;I hope now you understand various primitive types in JavaScript.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Have you ever thought, what is type of undeclared variable in JavaScript? I know, the first thing that might come to mind is: how can an undeclared variable have a type? Yes, in JavaScript it is possible.<\/p>\n","protected":false},"author":65,"featured_media":2210,"comment_status":"publish","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-838","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-how-to"],"_links":{"self":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts\/838","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/users\/65"}],"replies":[{"embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/comments?post=838"}],"version-history":[{"count":1,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts\/838\/revisions"}],"predecessor-version":[{"id":2247,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts\/838\/revisions\/2247"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/media\/2210"}],"wp:attachment":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/media?parent=838"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/categories?post=838"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/tags?post=838"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}