create account

Dynamic Type & Static Type By albro by albro

View this thread on: hive.blogpeakd.comecency.com
· @albro ·
$2.20
Dynamic Type & Static Type By albro
<center>![Dynamic Type & Static Type](https://images.ecency.com/DQmNy1VCRtEVybKt1yHXXhsMVWgQsN9jaCTbasRkx8hjaMn/12.jpg)
</center>

<p>
    The typescript language provides us with all kinds of types. Of course, the JavaScript language itself has several data types, but TypeScript adds many more to it, to the extent that it allows you to define your own types and use them. In this post, I want to talk about some of the main types that are also known by JavaScript (we call them core types). Then I will examine the difference between "Knowing Type" from JavaScript and "Using Type" from TypeScript.
</p>
<p>
    Our first main type is <code>number</code>. In both JavaScript and TypeScript languages, there is only one type of number, that is, unlike some other languages ​​(for example, Java), there are different types for integers or decimal numbers (float, double, etc.) does not exist Therefore, 10, 5, 6, and 6 are all numbers and there is no difference between them.
</p>
<p>
    Our next type is <code>string</code>, which is usually defined in three different ways:
</p>
<ul>
    <li>
        Using the single quote mark (i.e. <code>'</code>) like <code>'Hi'</code>
    </li>
    <li>
        Using double quote (i.e. <code>"</code>) like <code>"Hi"</code>
    </li>
    <li>
        Using template literal (that is, backtick sign or <code>`</code>) like <code>`Hi`</code>
    </li>
</ul>
<p>
    The third one is a newer method in JavaScript, and by using it, instead of gluing variables and strings together with the <code>+</code> operator, we can use them inside the string itself.
</p>
<p>
    The third type is <code>Boolean</code>, which contains only <code>true</code> and <code>false</code> values. Also, as you know, the values ​​are usually converted to <code>true</code>, except for a special group such as zero, which is always <code>false</code>. <code>Boolean</code> values ​​do not take anything other than these two values ​​and even if you use a <code>number</code> or a <code>string</code> as a <code>Boolean</code>, it will eventually become <code>true</code> or <code>false</code>.
</p>
<p>
    For now, I will start with these three types and as I go further, I will introduce more types. In this post, I want to focus on the difference between "understanding type" and "using type" and explain the concept of <code>dynamic types</code> and <code>static types</code>. To start testing these types, create an HTML file and put the following content in it:
</p>
<pre><code class="language-html">&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
  &lt;meta charset="UTF-8"&gt;
  &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
  &lt;meta http-equiv="X-UA-Compatible" content="ie=edge"&gt;
  &lt;title&gt;Understanding TypeScript&lt;/title&gt;
  &lt;script src="app.js" defer&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;/body&gt;
&lt;/html&gt;</code></pre>
<p>
    Now create a file called <code>app.ts</code> to write the typescript codes in it. I have written the following simple code for you which is actually pure javascript and does not have typescript code yet. Put this code in the <code>app.ts</code> file:
</p>
<pre><code class="language-javascript">function add(n1, n2) {
  return n1 + n2;
}
const number1 = 5;
const number2 = 2.8;
const result = add(number1, number2);
console.log(result);</code></pre>
<p>
    As you can see, we have a simple function that adds two numbers together. If we get our javascript file with the <code>tsc app.ts</code> command and open the browser, we will get the number <code>7.8</code> in the browser, so there is no problem until this part. Now, if we want to have a problem with the program like in the previous part, we can define the number <code>5</code> as a <code>string</code>:
</p>
<pre><code class="language-javascript">const number1 = '5';
const number2 = 2.8;</code></pre>
<p>
    By doing this and running the <code>tsc app.ts</code> command again in the browser, we get the number <code>52.8</code>. Exactly the same problem we had with <code>input1.value</code>. In fact, JavaScript tells itself that my first value is a <code>string</code> and is going to be added to the second value. This second value must also be a <code>string</code>, because adding math and calculations between a <code>string</code> and a <code>number</code> does not make sense, so I convert the second value to a <code>string</code>.
</p>
<p>
    To solve this problem, we need to specify the type. To do this, we use a colon plus the name of that particular type. For example, in this function we say:
</p>
<pre><code class="language-typescript">function add(n1: number, n2: number) {
  return n1 + n2;
}</code></pre>
<p>
    Of course, what type is depends on you and your program. For example, if you want this function to receive only string parameters, you can say:
</p>
<pre><code class="language-typescript">function add(n1: string, n2: string) {
  return n1 + n2;
}</code></pre>
<p>
    Now it gives us an error about the number 5 being a string, so we need to remove it from <code>string</code> mode.
</p>
<p>
    We know that the <code>number</code> type is also available in JavaScript and is not specific to TypeScript. In JavaScript itself, we use <code>typeof</code> operator to display type. For example:
</p>
<pre><code class="language-typescript">function add(n1: number, n2: number) {
  console.log(typeof number1);
  return n1 + n2;
}</code></pre>
<p>
    Now we run the command <code>tsc app.ts</code> to update the JavaScript file (<code>app.js</code>), then we go to the browser. In the console section of the browser, we see the word <code>number</code>. If we don't want to use TypeScript and solve the above problem only with JavaScript, we should use the same <code>typeof</code> operator:
</p>
<pre><code class="language-typescript">function add(n1: number, n2: number) {
  if (typeof n1 !== 'number' || typeof n2 !== 'number') {
    throw new Error('Incorrect input!');
  }
  return n1 + n2;
}</code></pre>
<p>
    In this code, if the type of parameter <code>n1</code> or <code>n2</code> is not equal to <code>number</code>, display an error to the user. Throwing errors is one of the issues related to JavaScript and is considered a prerequisite, so I won't go into that issue, but if you don't know what <code>throw new Error</code> does, I must say that it sends an error to the user's browser.
</p>
<p>
    Why should we write such a code in JavaScript? Because JavaScript has the ability to understand and recognize types, but it cannot actively use them like TypeScript. The above code gives us an error at run-time, while using typescript we can find our own error during development and coding. In fact, <i><strong>JavaScript uses dynamic types</strong></i>, which means that types can change instantly. For example, let's convert a numeric variable into a string variable, exactly like the case above, where the second parameter of the function was converted from a number to a string because of the first parameter, while <i><strong>Typescript uses static types</strong></i>, which means that you are allowed to change types. You don't have and it will always be fixed during development.
</p>
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 235 others
properties (23)
authoralbro
permlinkdynamic-type-and-static-type
categoryhive-169321
json_metadata"{"app":"ecency/3.2.0-vision","tags":["hive-169321","development","programming","chessbrothers","hive-engine","neoxag","stem","tricks","proofofbrain","leofinance","ecency"],"format":"markdown+html","image":["https://images.ecency.com/DQmNy1VCRtEVybKt1yHXXhsMVWgQsN9jaCTbasRkx8hjaMn/12.jpg"],"thumbnails":["https://images.ecency.com/DQmNy1VCRtEVybKt1yHXXhsMVWgQsN9jaCTbasRkx8hjaMn/12.jpg"],"description":"In this post, I want to talk about some of the main types that are also known by JavaScript (we call them core types).","image_ratios":["1.5383"]}"
created2024-08-22 07:10:39
last_update2024-08-22 07:10:39
depth0
children7
last_payout2024-08-29 07:10:39
cashout_time1969-12-31 23:59:59
total_payout_value1.112 HBD
curator_payout_value1.087 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length7,241
author_reputation30,477,419,385,789
root_title"Dynamic Type & Static Type By albro"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id136,451,444
net_rshares8,163,472,225,854
author_curate_reward""
vote details (299)
@chessbrotherspro ·
<h3>Congratulations!</h3><hr /><div class="pull-right"><img src="https://images.hive.blog/DQmQLssYuuJP2neoTVUbMRzvAu4Ptg7Vwt92aTM7Z3gNovg/cb-logo-150.png" alt="You have obtained a vote from CHESS BROTHERS PROJECT"/></div><div class="text-justify"><h3>✅ Good job. Your post has been appreciated and has received support from <a href="/@chessbrotherspro"><b>CHESS BROTHERS</b></a> ♔ 💪</h3><p><br>♟ We invite you to use our hashtag <b>#chessbrothers</b> and learn more <a href="/@chessbrotherspro/introducing-chess-brothers-project-the-most-innovative-community-combining-chess-fitness-and-more"><b>about us</b></a>.</p><p>♟♟ You can also reach us on our <a href="https://discord.gg/73sK9ZTGqJ" rel="noopener" title="This is going to take you to the Discord of Chess Brothers"><b>Discord server</b></a>  and promote your posts there.</p><p>♟♟♟  Consider <a href="/@chessbrotherspro/teamwork-is-worthwhile-join-the-chess-brothers-healing-trail-supporting-the-work-being-done-and-earning-rewards"><b>joining our curation trail</b></a> so we work as a team and you get rewards automatically.</p><p>♞♟ Check out our <a href="/@chessbrotherspro"><b>@chessbrotherspro</b></a> account to learn about the curation process carried out daily by our team.</p><p><br>🏅 If you want to earn profits with your HP delegation and support our project, we invite you to join the <i>Master Investor</i> plan. <a href='/@chessbrotherspro/master-investor-plan-or-programa'>Here you can learn how to do it.</a></p></div><div class="text-center"><p><br>Kindly</p><p><strong><em>The CHESS BROTHERS team</em></strong></p></div>
👍  
properties (23)
authorchessbrotherspro
permlinkre-dynamic-type-and-static-type
categoryhive-169321
json_metadata""
created2024-08-22 13:41:12
last_update2024-08-22 13:41:12
depth1
children0
last_payout2024-08-29 13:41:12
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,598
author_reputation78,492,844,132,988
root_title"Dynamic Type & Static Type By albro"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id136,456,877
net_rshares0
author_curate_reward""
vote details (1)
@cleanplanet ·
Hello,<br/>this Comment has been upvoted with 100%, thanks to @albro who burned 1000 PLANET<br/>With this burn @albro is actively participating in the CLEAN PLANET reward protocol.<br/>@albro is helping @cleanplanet to grow with the curation.<br/>Thanks for your help<br/>@cleanplanet
properties (22)
authorcleanplanet
permlinkre-albro-20240825t152503087z
categoryhive-169321
json_metadata"{"tags":["cleanplanet"],"app":"hivegadgets/1.0.0","format":"markdown+html","description":"Upvote for burned Planet Token"}"
created2024-08-25 15:25:03
last_update2024-08-25 15:25:03
depth1
children0
last_payout2024-09-01 15:25:03
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length284
author_reputation85,786,252,576,268
root_title"Dynamic Type & Static Type By albro"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id136,525,610
net_rshares0
@godofwar35 ·
It's realy Good post😘👍
!LUV
👍  
properties (23)
authorgodofwar35
permlinkre-albro-2024823t63315794z
categoryhive-169321
json_metadata{"tags":["hive-169321","development","programming","chessbrothers","hive-engine","neoxag","stem","tricks","proofofbrain","leofinance","ecency"],"app":"ecency/3.2.0-vision","format":"markdown+html"}
created2024-08-23 03:03:15
last_update2024-08-23 03:03:15
depth1
children0
last_payout2024-08-30 03:03:15
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length27
author_reputation-6,774,839,269
root_title"Dynamic Type & Static Type By albro"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id136,472,165
net_rshares0
author_curate_reward""
vote details (1)
@hivebuzz ·
Congratulations @albro! You have completed the following achievement on the Hive blockchain And have been rewarded with New badge(s)

<table><tr><td><img src="https://images.hive.blog/60x70/https://hivebuzz.me/@albro/upvoted.png?202408230900"></td><td>You received more than 25000 upvotes.<br>Your next target is to reach 30000 upvotes.</td></tr>
</table>

<sub>_You can view your badges on [your board](https://hivebuzz.me/@albro) and compare yourself to others in the [Ranking](https://hivebuzz.me/ranking)_</sub>
<sub>_If you no longer want to receive notifications, reply to this comment with the word_ `STOP`</sub>

properties (22)
authorhivebuzz
permlinknotify-1724404068
categoryhive-169321
json_metadata{"image":["https://hivebuzz.me/notify.t6.png"]}
created2024-08-23 09:07:48
last_update2024-08-23 09:07:48
depth1
children0
last_payout2024-08-30 09:07:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length621
author_reputation370,484,397,320,392
root_title"Dynamic Type & Static Type By albro"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id136,476,196
net_rshares0
@pars.team ·
!LUV
properties (22)
authorpars.team
permlinkre-albro-2024823t64148450z
categoryhive-169321
json_metadata{"tags":["hive-169321","development","programming","chessbrothers","hive-engine","neoxag","stem","tricks","proofofbrain","leofinance","ecency"],"app":"ecency/3.2.0-vision","format":"markdown+html"}
created2024-08-23 03:11:48
last_update2024-08-23 03:11:48
depth1
children0
last_payout2024-08-30 03:11:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length4
author_reputation1,044,473,769,173
root_title"Dynamic Type & Static Type By albro"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id136,472,267
net_rshares0
@pibara ·
Hive Archeology comment
This is a [hive-archeology](https://github.com/pibara/hive-archeology) proxy comment meant as a proxy for upvoting good content that is past it's initial pay-out window.

![image.png](https://files.peakd.com/file/peakd-hive/pibara/EppQ5vutzcx8r5YZ2LiXjW7EnMtgkcam6KpByUfiojAYYXLBmKFTC1jom5Kig1H9Z1w.png)

<sub><sup>Pay-out for this comment is configured as followed:</sup></sub>

| <sub><sup>role</sup></sub> | <sub><sup>account</sup></sub> | <sub><sup>percentage</sup></sub> | <sub><sup>note</sup></sub>|
| --- | --- | --- | --- |
| <sub><sup>curator</sup></sub> | <sub><sup>-</sup></sub> | <sub><sup>0.0%</sup></sub> | <sub><sup>curation rewards disabled</sup></sub> |
| <sub><sup>author</sup></sub> | <sub><sup>@albro</sup></sub> | <sub><sup>95.0%</sup></sub> | <sub><sup></sup></sub> |
| <sub><sup>dev</sup></sub> | <sub><sup>@croupierbot</sup></sub> | <sub><sup>2.5%</sup></sub> | <sub><sup>author of hive-archology</sup></sub> |
| <sub><sup>dev</sup></sub> | <sub><sup>@emrebeyler</sup></sub> | <sub><sup>2.5%</sup></sub> | <sub><sup>author of lighthive</sup></sub> |
👍  
properties (23)
authorpibara
permlinkalbro-dynamic-type-and-static-type
categoryhive-169321
json_metadata"{"tags": ["hivearcheology"], "app": "HiveArcheology 0.1.7"}"
created2024-09-02 23:02:18
last_update2024-09-02 23:02:18
depth1
children0
last_payout2024-09-09 23:02:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,074
author_reputation60,469,629,952,622
root_title"Dynamic Type & Static Type By albro"
beneficiaries
0.
accountalbro
weight9,500
1.
accountcroupierbot
weight250
2.
accountemrebeyler
weight250
max_accepted_payout1,000.000 HBD
percent_hbd0
post_id136,793,694
net_rshares266,729,431,320
author_curate_reward""
vote details (1)
@stemsocial ·
re-albro-dynamic-type-and-static-type-20240823t034427941z
<div class='text-justify'> <div class='pull-left'>
 <img src='https://stem.openhive.network/images/stemsocialsupport7.png'> </div>

Thanks for your contribution to the <a href='/trending/hive-196387'>STEMsocial community</a>. Feel free to join us on <a href='https://discord.gg/9c7pKVD'>discord</a> to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support.&nbsp;<br />&nbsp;<br />
</div>
properties (22)
authorstemsocial
permlinkre-albro-dynamic-type-and-static-type-20240823t034427941z
categoryhive-169321
json_metadata{"app":"STEMsocial"}
created2024-08-23 03:44:27
last_update2024-08-23 03:44:27
depth1
children0
last_payout2024-08-30 03:44:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length565
author_reputation22,927,823,584,327
root_title"Dynamic Type & Static Type By albro"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id136,472,735
net_rshares0