{"id":3737,"date":"2020-05-16T11:35:24","date_gmt":"2020-05-16T11:35:24","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=3737"},"modified":"2020-05-18T14:30:05","modified_gmt":"2020-05-18T14:30:05","slug":"interview-questions-2","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/interview-questions-2\/","title":{"rendered":"Interview questions"},"content":{"rendered":"<ol>\n<li><strong>What is difference between let and var while declaring variable?<\/strong><br \/>\n<em>var is Function Scoped and let is block scoped<\/em><br \/>\n<strong>Using var<\/strong><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar foo = 123;\r\nif (true) {\r\n    var foo = 456;\r\n}\r\n\r\nconsole.log(foo); \/\/ 456\r\n<\/pre>\n<p><strong>Using let<\/strong><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nlet foo = 123;\r\nif (true) {\r\n    let foo = 456;\r\n\r\n}\r\nconsole.log(foo); \/\/ 123\r\n<\/pre>\n<\/li>\n<li><strong>What would be the value of the below variables?<\/strong>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\"> \r\n \/\/Value is Undefined\r\n var foo:string;\r\n \r\n \/\/You can explicitly define value as undefined \r\n var age = undefined;\r\n\r\n \/\/any is a valid value which is default type until the variable assumes some data type by type inference\r\n var location:any;\r\n\r\n \/\/undefined and null could be assigned to any of the datatype\r\n var pincode:any=undefined;\r\n var state:any=null;\r\n<\/pre>\n<\/li>\n<li><strong>Why the below is not possible in interface?<\/strong><br \/>\nI am having a interface and class like one below which results in compile time exception<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\ninterface test1 {\r\n}\r\n\r\nclass test2 implements test1\r\n{\r\n    public foo;\r\n}\r\n\r\nlet test: test1 = new test2();\r\n\r\ntest.foo = 'test';\r\n<\/pre>\n<p>The reason for the compilation error is <em>when you reference a variable with specific interface in TypeScript, you can only use the properties and methods declared in the interface of the variable<\/em><br \/>\nYou are assigning test1 as a type of test variable, test1 interface doesn&#8217;t have foo property in it. so that&#8217;s why you are getting this error. If you change the type to let test: test2: new test2();. it won&#8217;t throw any error like one below<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nlet test: test2 = new test2();\r\ntest.foo = 'test';\r\n<\/pre>\n<p>There are two workaround for this. One is not to define the type while initializing variable like one below<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\ninterface test1 { }\r\nclass test2 implements test1{\r\n    public foo;\r\n}\r\nlet test = new test2();\r\ntest.foo = 'test';\r\n<\/pre>\n<p>Other is to use any while initializing variable<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\ninterface test1 { }\r\nclass test2 implements test1{\r\n    public foo;\r\n}\r\nlet test: test1 | any = new test2();\r\ntest.foo = 'test';\r\n<\/pre>\n<\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>What is difference between let and var while declaring variable? var is Function Scoped and let is block scoped Using var var foo = 123; if (true) { var foo = 456; } console.log(foo); \/\/ 456 Using let let foo = 123; if (true) { let foo = 456; } console.log(foo); \/\/ 123 What would&hellip; <a href=\"https:\/\/codethataint.com\/blog\/interview-questions-2\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[304],"tags":[],"class_list":["post-3737","post","type-post","status-publish","format-standard","hentry","category-interview-questions-typescript"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3737","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=3737"}],"version-history":[{"count":5,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3737\/revisions"}],"predecessor-version":[{"id":3757,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3737\/revisions\/3757"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=3737"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=3737"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=3737"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}