Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <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>
              
            
!

CSS

              
                body{
  font-family:arial; 
}
.highlight{
  color:red;
  background-color:yellow;
  font-weight:bold;
  font-size:large;
  padding:3px;
}
              
            
!

JS

              
                
//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>(msg)=>{<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

Console