create account

JavaScript Functions & Tricks By albro by albro

View this thread on: hive.blogpeakd.comecency.com
· @albro ·
$2.58
JavaScript Functions & Tricks By albro
<center>![JavaScript functions](https://files.peakd.com/file/peakd-hive/albro/23uFuC1qr3TmehzZos1PFhpFDpB7Dc8uExM2s3Tz31L6xvwPxKR2fp19mKexct8kkXGab.png)</center>

<p>
    In this post, I want to talk about the types of functions (Self-invoking functions, Declaration functions, Expression functions, Arrow functions, etc.) and how to define them, and we will also give some points about these functions.
</p>
<h3>
    JavaScript functions
</h3>
<p>
    Functions in JavaScript are defined with the <code>function</code> keyword and have two main types:
</p>
<ul>
    <li>
        Declaration Function
    </li>
    <li>
        Expression Function&nbsp;
    </li>
</ul>
<h3 style="text-align:justify;">
    Declaration Function
</h3>
<p style="text-align:justify;">
    Functions are declared with the following structure:
</p>
<pre><code class="language-javascript">function functionName(parameters) {
  // The code you want to run in the function
}</code></pre>
<p>
    These functions are not executed automatically, but we have to call them or invoke them in a more casual way. Their nature is such that they are stored for future use. Example:
</p>
<pre><code class="language-javascript">&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;body&gt;
&lt;h2&gt;JavaScript Functions&lt;/h2&gt;
&lt;p&gt;This example contains a statement that calls a function and then performs a calculation and returns a value. This function is of the first type (declarative):&lt;/p&gt;
&lt;p id="demo"&gt;&lt;/p&gt;
&lt;script&gt;
var x = myFunction(4, 3);
document.getElementById("demo").innerHTML = x;
function myFunction(a, b) {
  return a * b;
}
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;</code></pre>

<center>![Declaration Function](https://files.peakd.com/file/peakd-hive/albro/23tcP5UHFKspwGbcoVzhPF9ums8HPhmLpUokHy2c38CXVoMXq1KgeCEzx4ajSCVByKzzx.png)</center>

<p>
    <i><strong>Note:</strong> semicolons are used to separate executable commands in JavaScript, and since declaring is not an executable function (I said that these types of functions are not executed at the same time until they are called), there is no need to put There is no semicolon in their definition (after the closing bracket).</i>
</p>
<h3 style="text-align:justify;">
    Expression Function
</h3>
<p style="text-align:justify;">
    Another way to define JavaScript functions is to define them through expression. These types of functions are stored in a variable:
</p>
<pre><code class="language-javascript">&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;body&gt;
&lt;p&gt;Storing functions of the second type inside a variable:&lt;/p&gt;
&lt;p id="demo"&gt;&lt;/p&gt;
&lt;script&gt;
var x = function (a, b) {return a * b};
document.getElementById("demo").innerHTML = x;
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;</code></pre>
<p style="text-align:justify;">
    After you define this type of function in a variable, you can use that variable as a function. The following example clearly shows this:
</p>
<pre><code class="language-javascript">&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;body&gt;
&lt;p&gt;After you define this type of function in a variable, you can use that variable as a function:
&lt;/p&gt;
&lt;p id="demo"&gt;&lt;/p&gt;
&lt;script&gt;
var x = function (a, b) {return a * b};
document.getElementById("demo").innerHTML = x(4, 3);
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;</code></pre>

<center>![Expression Function](https://files.peakd.com/file/peakd-hive/albro/23tmmjqrmBBgAAjzRwRdc5wkmqGMfaa1ub27qUwi6aiAzYqffKCrYgAQpTQScY49BuhCR.png)</center>

<p style="text-align:justify;">
    The function that you see in the above example is called <i><strong>anonymous function</strong></i> because it has no name. These types of functions are always called with the desired variable name.
</p>
<p style="text-align:justify;">
    <i><strong>Note:</strong></i> If you look carefully, I have used a semicolon in the above function because it is part of an executive statement.
</p>
<h3 style="text-align:justify;">
    Function() Constructor
</h3>
<p style="text-align:justify;">
    You have seen from the previous examples that JavaScript functions are defined with the <code>function</code> keyword, but there is another way to define functions in JavaScript. We can use a constructor: <code>Function()</code>
</p>
<p style="text-align:justify;">
    Consider the following example:
</p>
<pre><code class="language-javascript">&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;body&gt;
&lt;p&gt;JavaScript has an built-in function constructor.&lt;/p&gt;
&lt;p id="demo"&gt;&lt;/p&gt;
&lt;script&gt;
var myFunction = new Function("a", "b", "return a * b");
document.getElementById("demo").innerHTML = myFunction(4, 3);
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;</code></pre>

<center>![Function() Constructor](https://files.peakd.com/file/peakd-hive/albro/23uRKjMYAstAVjtHjUseiRwJ5RvKkMBH4gHKgjTRbbhBtVfYy5HBjBrsk5jSSKZP2dYRf.png)</center>

<p style="text-align:justify;">
    Do we have special functions that we need to use the constructor to define? No! This is just one of several methods of defining JavaScript functions. You can simply write the previous example as follows:
</p>
<pre><code class="language-javascript">&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;body&gt;
&lt;p id="demo"&gt;&lt;/p&gt;
&lt;script&gt;
var myFunction = function (a, b) {return a * b};
document.getElementById("demo").innerHTML = myFunction(4, 3);
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;</code></pre>
<h3 style="text-align:justify;">
    Hoisting in JavaScript functions
</h3>
<p style="text-align:justify;">
    JavaScript's default behavior is to move declarations to the top of the current scope. This is true for variables and functions, so we can call JavaScript functions before we define them:
</p>
<pre><code class="language-javascript">myFunction(5);
function myFunction(y) {
  return y * y;
}</code></pre>
<p style="text-align:justify;">
    But if we define the functions with expressions, they will no longer be hoisted and we cannot do this.
</p>
<h3 style="text-align:justify;">
    Self-invoking functions
</h3>
<p style="text-align:justify;">
    Self-invoking functions means that they call themselves and do not need to be called and called separately. You can Self-invoking them by placing parentheses in front of Function expressions.
</p>
<p style="text-align:justify;">
    Note: function declaration cannot be Self-invoking.
</p>
<p style="text-align:justify;">
    Consider the following example:
</p>
<pre><code class="language-javascript">&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;body&gt;
&lt;p&gt;Functions can be invoked automatically without being called:&lt;/p&gt;
&lt;p id="demo"&gt;&lt;/p&gt;
&lt;script&gt;
(function () {
  document.getElementById("demo").innerHTML = "Hello! I called myself";
})();
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;</code></pre>

<center>![Self-invoking functions](https://files.peakd.com/file/peakd-hive/albro/23u6YQcP7tszSyasjv3GMC1ei2YzvBSYavVMhht9kC2qzjdZvnnUheivAGhQ9t1i6m1yr.png)</center>

<p style="text-align:justify;">
    As you can see, in order to read the functions, you must first put the entire function in a pair of parentheses and then put another pair of parentheses in front of it!
</p>
<p style="text-align:justify;">
    It is interesting to know that the above example function is called <strong>anonymous self-invoking function</strong>.
</p>
<p style="text-align:justify;">
    Note: You can use functions as a single value:
</p>
<pre><code class="language-javascript">&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;body&gt;
&lt;p id="demo"&gt;&lt;/p&gt;
&lt;script&gt;
function myFunction(a, b) {
  return a * b;
}
var x = myFunction(4, 3) * 2;
document.getElementById("demo").innerHTML = x;
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;</code></pre>
<p style="text-align:justify;">
    As you can see, I was able to use a function as a normal value and insert it into my calculations (in the example above, I multiplied the result of the function by 2).
</p>
<h3 style="text-align:justify;">
    Arrow Function
</h3>
<p style="text-align:justify;">
    This class of functions allows you to define functions more concisely:
</p>
<pre><code class="language-javascript">// Example in ES5
var x = function(x, y) {
  return x * y;
}
// Example in ES6
const x = (x, y) =&gt; x * y;</code></pre>
<p style="text-align:justify;">
    &nbsp;
</p>
<p style="text-align:justify;">
    [Hive: @albro]
</p>
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 244 others
properties (23)
authoralbro
permlinkjavascript-functions-and-tricks-by-albro
categoryhive-169321
json_metadata"{"app":"peakd/2023.11.3","format":"markdown","description":"I want to talk about the types of functions (Self-invoking functions, Declaration functions, Expression functions & etc)","tags":["programming","development","tricks","hive-engine","neoxian","stem","chessbrothers","lassecash","hive","leofinance"],"users":["albro"],"image":["https://files.peakd.com/file/peakd-hive/albro/23uFuC1qr3TmehzZos1PFhpFDpB7Dc8uExM2s3Tz31L6xvwPxKR2fp19mKexct8kkXGab.png","https://files.peakd.com/file/peakd-hive/albro/23tcP5UHFKspwGbcoVzhPF9ums8HPhmLpUokHy2c38CXVoMXq1KgeCEzx4ajSCVByKzzx.png","https://files.peakd.com/file/peakd-hive/albro/23tmmjqrmBBgAAjzRwRdc5wkmqGMfaa1ub27qUwi6aiAzYqffKCrYgAQpTQScY49BuhCR.png","https://files.peakd.com/file/peakd-hive/albro/23uRKjMYAstAVjtHjUseiRwJ5RvKkMBH4gHKgjTRbbhBtVfYy5HBjBrsk5jSSKZP2dYRf.png","https://files.peakd.com/file/peakd-hive/albro/23u6YQcP7tszSyasjv3GMC1ei2YzvBSYavVMhht9kC2qzjdZvnnUheivAGhQ9t1i6m1yr.png"]}"
created2023-12-24 09:16:15
last_update2023-12-24 09:16:15
depth0
children3
last_payout2023-12-31 09:16:15
cashout_time1969-12-31 23:59:59
total_payout_value1.302 HBD
curator_payout_value1.274 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length8,411
author_reputation30,477,419,385,789
root_title"JavaScript Functions & Tricks By albro"
beneficiaries
0.
accounthive-169321
weight200
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id129,945,152
net_rshares5,545,546,224,887
author_curate_reward""
vote details (308)
@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/http://hivebuzz.me/@albro/comments.png?202312241631"></td><td>You made more than 100 comments.<br>Your next target is to reach 200 comments.</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>


To support your work, I also upvoted your post!


**Check out our last posts:**
<table><tr><td><a href="/hivebuzz/@hivebuzz/christmas-2023"><img src="https://images.hive.blog/64x128/https://files.peakd.com/file/peakd-hive/hivebuzz/48PxLszsgqS1gaev6FeBYEsrt9sKNhKtddSS32hKnvM3Xam6KCMs3rza5BPR7AQNaF.png"></a></td><td><a href="/hivebuzz/@hivebuzz/christmas-2023">It's the Christmas season: give your friends a gift</a></td></tr></table>
properties (22)
authorhivebuzz
permlinknotify-albro-20231224t163805
categoryhive-169321
json_metadata{"image":["http://hivebuzz.me/notify.t6.png"]}
created2023-12-24 16:38:06
last_update2023-12-24 16:38:06
depth1
children0
last_payout2023-12-31 16:38:06
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,050
author_reputation369,202,597,441,110
root_title"JavaScript Functions & Tricks By albro"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id129,952,839
net_rshares0
@poshthreads ·
https://inleo.io/threads/albro/re-leothreads-snaqytc
<sub> The rewards earned on this comment will go directly to the people ( albro ) sharing the post on LeoThreads,LikeTu,dBuzz.</sub>
properties (22)
authorposhthreads
permlinkre-albro-javascript-functions-and-tricks-by-albro-1524
categoryhive-169321
json_metadata"{"app":"Poshtoken 0.0.2","payoutToUser":["albro"]}"
created2023-12-24 09:29:27
last_update2023-12-24 09:29:27
depth1
children0
last_payout2023-12-31 09:29: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_length186
author_reputation415,471,585,053,248
root_title"JavaScript Functions & Tricks By albro"
beneficiaries
0.
accountnomnomnomnom
weight10,000
max_accepted_payout1,000,000.000 HBD
percent_hbd0
post_id129,945,319
net_rshares0
@stemsocial ·
re-albro-javascript-functions-and-tricks-by-albro-20231225t075750301z
<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-javascript-functions-and-tricks-by-albro-20231225t075750301z
categoryhive-169321
json_metadata{"app":"STEMsocial"}
created2023-12-25 07:57:51
last_update2023-12-25 07:57:51
depth1
children0
last_payout2024-01-01 07:57:51
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,903,676,462,363
root_title"JavaScript Functions & Tricks By albro"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id129,969,466
net_rshares0