JavaScript Vs TypeScript
Tags: javascript, javascriptframework, developer, webdev
Published: 2021-10-31 14:22:00

<p>Before we dive into the differences let's have a little background on both languages.</p>
<p><strong>JavaScript</strong> (often shortened as JS) is a lightweight, interpreted, cross-platform, object-oriented scripting language that conforms to the ECMAScript specification and is used to make interactive web pages. JavaScript is used on both client-side and server-side that allows programmers to make web pages.</p>
<p><strong>TypeScript</strong> (Often shortened as TS) is a programming language developed and maintained by Microsoft. It is a strongly typed superset of Javascript, cross-platform, object-oriented, compiles to plain javascript language, and is used to make interactive web pages.</p>
<p><strong>When we have JavaScript as a client-side and server-side technology, then why we needed TypeScript?</strong></p>
<p>As we know by the development of Nodejs, JavaScript has become more popular than it was before due to server-side and client-side web development. However, when JavaScript was growing then the code getting more complex and making it difficult to maintain the reusability of code. It was not able to fulfill the necessity of Object-Oriented programming. These flaws hinder JavaScript from succeeding at the enterprise level as a server-side technology. Then TypeScript comes into the picture to fill the gap.</p>
<p><strong>Difference between JavaScript and TypeScript</strong></p>
<p><em>TypeScript is an Object-oriented programming language whereas JavaScript is an Object-oriented scripting language </em></p>
<ul>
<li><p>Object-oriented programming languages are based on objects whereas scripting languages are made up of scripts to automate any function/action.</p>
</li>
<li><p>Scripting languages don't need a compilation step and mostly get interpreted.</p>
</li>
<li><p>TypeScript always calls attention to errors during the development period but JavaScript highlights at the runtime.</p>
</li>
</ul>
<p><em>TypeScript support static typing but javaScript has dynamic typing</em></p>
<ul>
<li><p>JavaScript assigns the type to any variable at runtime(dynamically typed)</p>
</li>
<li><p>TypeScript gives the option for static typing, the type of a variable is declared while writing the code. For example</p>
</li>
</ul>
<p>in JavaScript:</p>
<pre><code><span class="hljs-keyword">let</span> name_ =<span class="hljs-string">"JavaScript"</span>;
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">"name variable is infered as string"</span>,names);
name_=<span class="hljs-number">45</span>;
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">"name variable is infered as number "</span>,names);
</code></pre><p>in TypeScript:</p>
<pre><code>let name_ = <span class="hljs-string">"TypeScript"</span>;
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">"name variable is infered as string"</span>,name_);
name_=<span class="hljs-number">45</span>;    <span class="hljs-regexp">//</span> <span class="hljs-keyword">throw</span> error <span class="hljs-string">"Type 'number' is not assignable to type 'string' "</span>
</code></pre><ul>
<li>When a language is not statically typed, it is more prone to errors in runtime.</li>
</ul>
<p><em>TypeScript support interface but Javascript does not.</em></p>
<ul>
<li>Being an object-oriented programming language TypeScript s supports  OOPs 
concepts like interfaces, classes, inheritances, generics, etc.</li>
</ul>
<p>There are many benefits TypeScript provides over JavaScript like it helps to make code management easier, increases team performance by the explicitly defined data structure, and variables type annotation make code by far easier to comprehend the decisions made by the other engineers who were working originally on your codebase.
TypeScript is getting popular and adopted by the Industry giants.</p>
<p> <a target="_blank" href="https://www.npmtrends.com/typescript">NPM downloads in the past 2 years</a> :</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1631464058934/gKzhw7CcA.png" alt="image.png" /></p>
<p> <a target="_blank" href="https://insights.stackoverflow.com/survey/2020#technology-most-loved-dreaded-and-wanted-languages-loved">Stackoverflow survey on most loved language</a> </p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1631464807404/uUvgNCcQI.png" alt="Screenshot (5).png" /></p>
<p>TypeScript is an awesome tool for JavaScript developers. It makes large codebases easy to understand and gives a fine code-writing toolkit as well.</p>
<p>That's it for now, thank you for reading.</p>
<p>-sneha</p>
</br><p style="color:#808080;">This blog is posted using <a href="www.blogtiple.com">Blogtiple</a></p></br><p style="color:#808080;">This blog is posted using <a href="www.blogtiple.com">Blogtiple</a></p></br><p style="color:#808080;">This blog is posted using <a href="www.blogtiple.com">Blogtiple</a></p>