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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource


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


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.


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.

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.


                <body onload="basicFunction(), lessBasicFunction('text from a variable'), evenLessBasicFunction(), advancedFunction()">
  <h2>A primer on javascript functions in four steps</h2>
  <p><a href="">( check out the blog post for a more complete walkthrough )</a></p>
  <p>This is some text that includes a value created by a function: "<span id="basicVar">this is the text we're replacing with basicFunction</span>".</p>
  <p>And now the same idea, but we're passing a value to the function: "<span id="lessBasicVar">this is the text we're replacing with lessBasicFunction</span>".</p>
  <p>So far so good, but the value "text from a variable" had to be hardcoded into the page (in the body "onload" section). That's not very helpful and we might as well have written it like the basicFunction above. But we passed it a value as a variable, and that's a start. Let's expand on this idea and create a function that takes two values and outputs them as a total. We'll even put a function inside a function for fun using evenLessBasicFunction and output it to the page here: "<span id="evenLessBasicVar">this is the value we're replacing with evenLessBasicFunction</span>"</p> 
    <p>Let's add a form so the user can enter something and we can then use that to pass it to the function.</p>
  <input id="formValue" class="the-class" placeholder="type something here">
  <button id="submitButton">enter</button>
  <p>So now, thanks to the advancedFunction function (and a "listener"), whatever you put in that field should appear here: "<span id="advancedVar">this is the value we're replacing with advancedFunction</span>".</p>
  <p>Now we have four functions:</p>
    <li>The first function (basicFunction) adds some text to the page</li>
    <li>The second function (lessBasicFunction) also adds text but we get to pass it as a variable</li>
    <li>The third function (evenLessBasicFunction) passes two values and adds them together in a nested function</li>
    <li>The fourth function (advancedFunction) lets us define the value on the page itself</li>
<p>This is all using simple javascript. It's meant to illustrate the logic behind functions and how to pass values to them using variables. There are quicker and better ways to do most of this, like using jquery :). </p>


                /* not the best css here, just quick and dirty for the sake of legibility... */
body {font-family: helvetica, verdana, sans-serif}
span {font-weight: 700;}


                function basicFunction() {
  // set the text
  var theVar = "text generated by js";
  // output the text we just defined to the page by replacing the html content of the element with the ID "basicVar"
  document.getElementById("basicVar").innerHTML = theVar;

function lessBasicFunction(theValue) {
  // no need to define the theValue, it's being passed from where the function is called, all we need to do is output it to the page
  document.getElementById("lessBasicVar").innerHTML = theValue;

function evenLessBasicFunction() {
  // let's define two variables
  var var1 = 100;
  var var2 = 400;
  // now let's create a quick function that takes two values and adds them together
  function addingFunction(value1,value2) {
    return value1 + value2;
  // let's output it to the page
  document.getElementById("evenLessBasicVar").innerHTML = addingFunction(var1,var2);
  // note that we're making this harder than it has to be: we don't have to define our values in var1 and var2, we could just pass them directly to the function when we call it like this: "addingFunction(100,400)"

function advancedFunction() {
  // check if there's a value for the form field (which will be blank when the page loads). If not set the value to "empty"
  if ( document.getElementById("formValue").value ) {
    // grab the value from the form, but we'll need to set a listener (below) since it changes when you click the button
    var theVar = document.getElementById("formValue").value;
  else {
    theVar = "empty";
  // now that we have a value for the form field, let's output it to the field
  document.getElementById("advancedVar").innerHTML = theVar;

// for our advancedFunction we have to add a "listener" to process the function when the "enter" button is clicked, otherwise it will have the initial (empty) value from the first page load. 
// first, set a listener for the "enter" button
document.getElementById("submitButton").addEventListener("click", advancedFunction);
// now add one for anyone who hits the "return/enter" key instead of the button
document.getElementById("formValue").addEventListener('keypress', function (e) {
    if (e.key === 'Enter') {