cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <h1>The FUN thing about Functions in JavaScript</h1>
<blockquote>What the Heck is a JavaScript Function Anyway? Even old programmers are mystified at times by the liberty JavaScript takes with functions. However, this approach is often what makes JavaScript so appealing to some. Here is a step-by-step explanation of what I mean.</blockquote>
<blockquote>You may know that JavaScript functions can be written in variable structure "var test = function(x){...}" or a standard named "declaration notation" function call. We'll be utilizing the declaration notarion for this example to illustrate how function logic and usage has developed.</blockquote>
<br>
<div id="result1"></div>
<div id="result2"></div>
<div id="result3"></div>
<div id="result4"></div>
<div id="result5"></div>
<blockquote>Thank you for viewing this code demo! ~ Dan  :-]></blockquote>
            
          
!
            
              body{
  font-family:arial; 
}
.highlight{
  color:red;
  background-color:yellow;
  font-weight:bold;
  font-size:large;
  padding:3px;
}
            
          
!
            
              
//Ex. 1: A standard function:
var message = "<strong>EXAMPLE 1.) A Standard Function:</strong> This is a standard function call.<blockquote><p> function f1(msg) {<br>&nbsp;&nbsp;&nbsp;&nbsp;   document.getElementById('result1').innerHTML=msg;<br>}<br>f1(message);</p><blockquote>";

function f1(msg) {
   document.getElementById("result1").innerHTML=msg;  
}
f1(message);

//Ex. 2: Standard function, grouped
var message = "<strong>EXAMPLE 2.) \"Call Wrapping\" the Function Call: </strong> Here is the same type of function. However, since function calls are objects, they can be grouped. Putting a parenthesis around the call still works! See the next example for why that is important.<blockquote><p> function f2(msg) {<br>&nbsp;&nbsp;&nbsp;&nbsp;   document.getElementById('result2').innerHTML=msg; <br>}<br> <span class='highlight'>(</span>f2<span class='highlight'>)</span>(message);</blockquote>";

function f2(msg) {
  document.getElementById("result2").innerHTML=msg; 
}
(f2)(message);

//Ex. 3. Calling a Function without a named call
var message = "<strong>EXAMPLE 3.) Wrapping the entire function as a call.</strong> Since a call is the function object, the function call can be used without the named call at all. This is all fascilitated by the parenthesis now wrapping the entire function. <blockquote><p> <span class='highlight'>(</span>function foo(msg) {<br>&nbsp;&nbsp;&nbsp;&nbsp;     document.getElementById('result3').innerHTML=msg;<br> }<span class='highlight'>)</span>(message);</p></blockquote>";
(function foo(msg) {
    document.getElementById("result3").innerHTML=msg;
})(message); 

//Ex. 4. An Anonymouse Function Call
var message = "<strong>EXAMPLE 4.) An Anonymouse Function Call:</strong> Because of this, you don't need the function name either!<blockquote><p>(function<span class='highlight'>&nbsp;&nbsp;</span>(msg) { <br>&nbsp;&nbsp;&nbsp;&nbsp;document.getElementById('result4').innerHTML=msg;<br>})(message);</p></blockquote>";
(function (msg) { 
     document.getElementById("result4").innerHTML=msg;
})(message);

// Ex. 5. EC6 Shorthand - We don't need the keyword function either with EC6, use => instead.
var message = "<strong>EXAMPLE 5.) An Anonymouse Call Without the 'Function' Keyword:</strong> Nore do you need the function keyword itself!<blockquote><p>(<span class='highlight'>&nbsp;</span>(param)=>{<br>&nbsp;&nbsp;&nbsp;&nbsp;document.getElementById('result5').innerHTML=msg;<br>})(message);</p></blockquote><blockquote>With ES6 that can use only a parameter and a structure, we are practically left with just an equation!:<p> ((param)=>{ //Do something })(param));</p></blockquote>";
((msg) => { 
   document.getElementById("result5").innerHTML=msg;
})(message);

            
          
!
999px
Loading ..................

Console