{"id":424,"date":"2022-10-26T15:12:36","date_gmt":"2022-10-26T07:12:36","guid":{"rendered":"https:\/\/islandtropicaman.com\/wp\/?p=424"},"modified":"2022-10-26T15:14:55","modified_gmt":"2022-10-26T07:14:55","slug":"nullish-coalescing-in-typescript","status":"publish","type":"post","link":"https:\/\/islandtropicaman.com\/wp\/2022\/10\/26\/nullish-coalescing-in-typescript\/","title":{"rendered":"Nullish coalescing in TypeScript"},"content":{"rendered":"<p>In the below program, the function will return a string indicating that that object is not a number using TypeScript nullish coalescing which will provide a default value for that variable if it is either null or undefined.  <\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nfunction num(obj : any) : number {\r\n    return obj?? &quot;Not a number&quot;\r\n}\r\n\r\nconsole.log(num(undefined)) \/* Not a number *\/\r\nconsole.log(num(3)) \/* 3 *\/\r\n<\/pre>\n<p>The ?? is nullish coalescing operator which will provide the default value on the left hand side of that operator.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the below program, the function will return a string indicating that that object is not a number using TypeScript nullish coalescing which will provide a default value for that variable if it is either null or undefined. function num(obj : any) : number { return obj?? &quot;Not a number&quot; } console.log(num(undefined)) \/* Not a&#8230;<\/p>\n<p class=\"more-link-wrap\"><a href=\"https:\/\/islandtropicaman.com\/wp\/2022\/10\/26\/nullish-coalescing-in-typescript\/\" class=\"more-link\">Read More<span class=\"screen-reader-text\"> &ldquo;Nullish coalescing in TypeScript&rdquo;<\/span> &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38],"tags":[128,40],"class_list":["post-424","post","type-post","status-publish","format-standard","hentry","category-type-script","tag-nullish-coalescing","tag-typescript"],"blog_post_layout_featured_media_urls":{"thumbnail":"","full":""},"categories_names":{"38":{"name":"Type-Script","link":"https:\/\/islandtropicaman.com\/wp\/category\/type-script\/"}},"tags_names":{"128":{"name":"Nullish coalescing","link":"https:\/\/islandtropicaman.com\/wp\/tag\/nullish-coalescing\/"},"40":{"name":"typescript","link":"https:\/\/islandtropicaman.com\/wp\/tag\/typescript\/"}},"comments_number":"0","_links":{"self":[{"href":"https:\/\/islandtropicaman.com\/wp\/wp-json\/wp\/v2\/posts\/424","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/islandtropicaman.com\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/islandtropicaman.com\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/islandtropicaman.com\/wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/islandtropicaman.com\/wp\/wp-json\/wp\/v2\/comments?post=424"}],"version-history":[{"count":2,"href":"https:\/\/islandtropicaman.com\/wp\/wp-json\/wp\/v2\/posts\/424\/revisions"}],"predecessor-version":[{"id":426,"href":"https:\/\/islandtropicaman.com\/wp\/wp-json\/wp\/v2\/posts\/424\/revisions\/426"}],"wp:attachment":[{"href":"https:\/\/islandtropicaman.com\/wp\/wp-json\/wp\/v2\/media?parent=424"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/islandtropicaman.com\/wp\/wp-json\/wp\/v2\/categories?post=424"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/islandtropicaman.com\/wp\/wp-json\/wp\/v2\/tags?post=424"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}