{"componentChunkName":"component---src-templates-blog-post-js","path":"/blog/exciting-new-features-in-javascript","result":{"data":{"markdownRemark":{"excerpt":"Javascript community is going very well with the TC39 (Technical Committee 39), and it's proposal system. And javascript is now evolving…","html":"<p>Javascript community is going very well with the TC39 (Technical Committee 39), and it's proposal system. And javascript is now evolving fast, and we can expect some amazing features. I will talk about some of the most useful and exciting features which will make our life easy.</p>\n<h2 id=\"1-Optional-Chaining-\" style=\"position:relative;\"><a href=\"#1-Optional-Chaining-\" aria-label=\"1 Optional Chaining  permalink\" class=\"gatsby-remark-autolink before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>1. Optional Chaining ?</h2>\n<p>I'm sure you faced an error like this.</p>\n<p><code class=\"language-text\">cannot read property 'amazing' of undefined.</code></p>\n<p>and these errors are pervasive when accessing dynamic object with deep nesting, we can combat this in javascript by using the <code class=\"language-text\">&amp;&amp;</code> operator to check, but it can get quite messy sometimes, here's the old and new way</p>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">const</span> js <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span>\n  can<span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">do</span><span class=\"token operator\">:</span> <span class=\"token string\">\"anything\"</span><span class=\"token punctuation\">,</span>\n    will<span class=\"token operator\">:</span> <span class=\"token string\">\"do anything\"</span><span class=\"token punctuation\">,</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token comment\">// old way</span>\n<span class=\"token keyword\">const</span> canDoAnything <span class=\"token operator\">=</span> js <span class=\"token operator\">&amp;&amp;</span> js<span class=\"token punctuation\">.</span>can <span class=\"token operator\">&amp;&amp;</span> js<span class=\"token punctuation\">.</span>can<span class=\"token punctuation\">.</span>do\n\n<span class=\"token comment\">// new way</span>\n<span class=\"token keyword\">const</span> canDoAnythingNew <span class=\"token operator\">=</span> js<span class=\"token operator\">?.</span>can<span class=\"token operator\">?.</span>do</code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<h2 id=\"3-Private-fields-\" style=\"position:relative;\"><a href=\"#3-Private-fields-\" aria-label=\"3 Private fields  permalink\" class=\"gatsby-remark-autolink before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>3. Private fields <em>#</em></h2>\n<blockquote>\n<p>Partially Available in Chrome &#x26; NodeJS 12</p>\n</blockquote>\n<p>Finally, we can avoid _underscore naming conventions and use actual private variables in classes. Yes, the syntax is a bit weird and controversial at first, but hey, first time for everything! the new version of chrome supports class <code class=\"language-text\">#fields</code> only, but the class <code class=\"language-text\">#methods()</code> is coming soon</p>\n<blockquote>\n<p>Check the <a href=\"https://github.com/tc39/proposal-class-fields\">TC39 proposal for class fields</a></p>\n</blockquote>\n<blockquote>\n<p>Check the <a href=\"https://github.com/tc39/proposal-private-methods\">TC39 proposal for private methods</a></p>\n</blockquote>\n<p>here's how it looks</p>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token comment\">// private fields must start with '#'</span>\n<span class=\"token comment\">// and they arent accessible outside the class block</span>\n\n<span class=\"token keyword\">class</span> <span class=\"token class-name\">Ship</span> <span class=\"token punctuation\">{</span>\n  #x<span class=\"token punctuation\">;</span>\n  #y<span class=\"token punctuation\">;</span>\n\n  <span class=\"token function\">constructor</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">x<span class=\"token punctuation\">,</span> y</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>#x <span class=\"token operator\">=</span> x<span class=\"token punctuation\">;</span>\n    <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>#y <span class=\"token operator\">=</span> y<span class=\"token punctuation\">;</span>\n  <span class=\"token punctuation\">}</span>\n\n  <span class=\"token comment\">// private methods are comming soon</span>\n  <span class=\"token function\">#boost</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>#x<span class=\"token operator\">++</span><span class=\"token punctuation\">;</span>\n    <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>#y<span class=\"token operator\">++</span><span class=\"token punctuation\">;</span>\n  <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token keyword\">const</span> ship <span class=\"token operator\">=</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">Ship</span><span class=\"token punctuation\">(</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">1</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>ship<span class=\"token punctuation\">.</span>#x<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// -> Error</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<h2 id=\"4-Top-Level-await\" style=\"position:relative;\"><a href=\"#4-Top-Level-await\" aria-label=\"4 Top Level await permalink\" class=\"gatsby-remark-autolink before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>4. Top Level await</h2>\n<p>This one is interesting because <code class=\"language-text\">top level await</code> allows you to use \"await\" without an outer async function, which means you don't have to wrap an anonymous async function for every async operation.</p>\n<p>\"Top Level await' is very handy while debugging async stuff (like fetch) in console without wrapping it in an async function.</p>\n<blockquote>\n<p>Check the <a href=\"https://github.com/tc39/proposal-top-level-await\">TC39 proposal for Top Level Await</a></p>\n</blockquote>\n<p>and there are many <a href=\"https://github.com/tc39/proposal-top-level-await#use-cases\">use cases</a> which are mentioned in the tc39 repo</p>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token comment\">// top level window</span>\n<span class=\"token keyword\">await</span> <span class=\"token function\">fetch</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"https://somesite.com/api/data\"</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">// -> Response {..}</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span></span></pre></div>\n<h2 id=\"5-BigInt\" style=\"position:relative;\"><a href=\"#5-BigInt\" aria-label=\"5 BigInt permalink\" class=\"gatsby-remark-autolink before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>5. BigInt</h2>\n<blockquote>\n<p>Available in Chrome &#x26; NodeJS 12</p>\n</blockquote>\n<p>Javascript always has been tribble at Math because we cannot reliably store numbers larger than 2 ^ 53, which limits its capabilities in many regions, for example, graphics and data processing. </p>\n<blockquote>\n<p>Check the <a href=\"https://github.com/tc39/proposal-bigint\">TC39 proposal for BigInt</a></p>\n</blockquote>\n<p>here's how the syntax looks</p>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token comment\">// can define BigInt by appending 'n' to a number literal</span>\n<span class=\"token keyword\">const</span> theBiggestInt <span class=\"token operator\">=</span> <span class=\"token number\">9997199254748991n</span>\n\n<span class=\"token comment\">// using the constructor with a literal</span>\n<span class=\"token keyword\">const</span> alsoHuge <span class=\"token operator\">=</span> <span class=\"token function\">BigInt</span><span class=\"token punctuation\">(</span><span class=\"token number\">9997199254748991n</span><span class=\"token punctuation\">)</span>\n\n<span class=\"token comment\">// or with a string</span>\n<span class=\"token keyword\">const</span> hugeButString <span class=\"token operator\">=</span> <span class=\"token function\">BigInt</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"9997199254748991n\"</span><span class=\"token punctuation\">)</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>You can also do the same <code class=\"language-text\">+, -, /, *, %</code> operations on BigInt as you would expect from regular numbers, but you can't mix BigInt with numbers in most operations. Comparing Number and BigInt works, but not adding them.</p>\n<h2 id=\"6-globalThis\" style=\"position:relative;\"><a href=\"#6-globalThis\" aria-label=\"6 globalThis permalink\" class=\"gatsby-remark-autolink before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>6. globalThis</h2>\n<p>I know this one might not be too much exciting for you, but trust me <code class=\"language-text\">globalThis</code> is essential for standardizing the <code class=\"language-text\">global object</code> in javascript.</p>\n<p>let's say we need to get the <code class=\"language-text\">globalObject</code> for browser, node, all other alien stuff.</p>\n<blockquote>\n<p>Check the <a href=\"https://github.com/tc39/proposal-global\">TC39 proposal for globalThis</a></p>\n</blockquote>\n<p>Before <code class=\"language-text\">globalThis</code>, the only reliable cross-platform way to get the global object for an environment was <code class=\"language-text\">Function('return this')()</code>. However, this causes CSP violations in some settings, so es6-shim uses a check like this, for example:</p>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token keyword\">var</span> <span class=\"token function-variable function\">getGlobal</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token comment\">// in node, it's named 'global'. If we're in a shell, 'this' might work.</span>\n  <span class=\"token comment\">// some times also self. and in browsers its window</span>\n  <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">typeof</span> self <span class=\"token operator\">!==</span> <span class=\"token string\">\"undefined\"</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">return</span> self\n  <span class=\"token punctuation\">}</span>\n  <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">typeof</span> window <span class=\"token operator\">!==</span> <span class=\"token string\">\"undefined\"</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">return</span> window\n  <span class=\"token punctuation\">}</span>\n  <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">typeof</span> global <span class=\"token operator\">!==</span> <span class=\"token string\">\"undefined\"</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">return</span> global\n  <span class=\"token punctuation\">}</span>\n  <span class=\"token keyword\">throw</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">Error</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"unable to locate global object\"</span><span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">}</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>Now, this can get quite messy because we have to check for every environment and find the actual <code class=\"language-text\">global</code>. You might think that its not a big issue, but it is a massive issue because the problem here is about standardization.</p>\n<p>And for that reason, <code class=\"language-text\">globalThis</code> is introduced. the new syntax will look like this</p>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-js line-numbers\"><code class=\"language-js\"><span class=\"token comment\">// taken from mdn</span>\n<span class=\"token keyword\">function</span> <span class=\"token function\">canMakeHTTPRequest</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">return</span> <span class=\"token keyword\">typeof</span> globalThis<span class=\"token punctuation\">.</span>XMLHttpRequest <span class=\"token operator\">===</span> <span class=\"token string\">\"function\"</span>\n<span class=\"token punctuation\">}</span>\n\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token function\">canMakeHTTPRequest</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\n<span class=\"token comment\">// expected output (in a browser): true</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<hr>\n<p>That's all, folks!</p>\n<p>Now, some of them are supported in the latest browsers and nodejs 12, but some of them are <code class=\"language-text\">stage 3</code>, so maybe we can see some changes.</p>\n<p>if you have any questions or if I missed some cool features, comment down below. &#x3C;3</p>","timeToRead":4,"id":"a4c47775-2bd4-536b-a5dd-4fe53a3dab40","frontmatter":{"date":"August 07, 2019","title":"Exciting New Features In Javascript"}}},"pageContext":{"slug":"/blog/exciting-new-features-in-javascript"}},"staticQueryHashes":["1033876704","3435786681","550521386"]}