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

              
                <main>
  
  <h1>🤷 What is JavaScript?</h1>
  <section>
    <p>JavaScript is a programming language that can be used to increase the complexity of user interaction on a web page.</p>
    <p>One reason that it has grown in popularity over the last few years is that it can be used across different operating systems (Windows, Linux, MacOS) and also different browsers (Chrome, Firefox, Internet Explorer).</p>
  </section>
  
  <h2>🕺 Top Tip!</h2>
  <section>
    <p>JavaScript and Java are not the same thing! They are two different programming languages.</p>
  </section>
  
  <h2>🐍 Let's play with JavaScript!</h2>

  <h3>🚨 Step 1: The Alert Method</h3>
  <section>
    <p>In JavaScript, a programmer has access to built in methods which can assist her in completing tasks. One of these built in methods is called a window alert.</p>
    <p>The alert method displays an alert box with a specified message and an 'OK' button which a user can use to close the box. It is used when you want to make sure info is sent to the user in a way that they cannot ignore! 👀</p>
    <p>To try it, type <span class="javascript">alert("Your message here.")</span> in the Javascript box of this Codepen.</p>
    <p>The alert should fire when the document saves. Since we don't want it to keep alerting us, we can type <span class="javascript">//</span> at the beginning of the line to comment it out of our script.</p>
  </section>
  
  <h3>🎁 Step 2: Understanding a variable</h3>
  <section>
    <p>A JavaScript variable can store a value or an expression. If I wanted to store my name as a value in a variable, it might look something like this: <span class="javascript">var name = 'Kristina';</span>. Note that in this example, 'var' is how JavaScript knows that I'm referencing a variable, 'name' is the title that I've given the variable and "'Kristina'" is the string of characters that I have assigned to that variable.</p> 
    <p>An example of an expression stored in a variable could be:</p>
    <p class="javascript">var difference = a - b;</p>
    <p>To make a variable in JavaScript, you must declare that it exists. This might look like:</p>
    <p class="javascript">var name;</p>
    <p>Now that the variable is declared, we can now assign it a value.</p>
    <p class="javascript">name = 'Kristina';</p>
    <p>We can also declare and assign a variable simultaneously, by writing something like this:</p>    
    <p class="javascript">var name = 'Kristina';</p>
    <p>Try creating a variable with your name in the JavaScript box of this file. This will not yet show anywhere!
  </p>
  </section>
  
  <h3>🖨 Step 3: Print a variable to your document</h3>
  <section>
  <p>At the moment, your variable has been created in the Javascript, but it is not yet doing anything. If you want to 'print' it to your document, you must tell JavaScript to do so. To do that, try typing this below where you have declared and assigned your variable:</p>
    <p class="javascript">document.write(nameOfYourVariable);</p>
    <p>Your variable will now be printed at the bottom of this document! Go check it out! </p>
  </section>
  
  <h3>➕ Step 4: Print your variable in a sentence.</h3>
  <section>
    <p>Variables are useful for many reasons. One of these is that they can be used dynamically within static data. Let's create an example! Create a new variable and print it to your document using this syntax:</p>
    <p class="javascript">var myName = 'Kristina';</p>
    <p class="javascript">document.write('My name is ' + myName + '.');</p>
    <p>In this example, we are creating a string of words and inserting our variable (also a string) into the middle of it. The <span class="javascript">+</span> sign allows us add all of the strings together. Note that we need to include spaces where we want them!
  </p>
  </section>
  
  <h3>📝 Step 5: Arrays</h3>
  <section>
  <p>In JavaScript, there are different types types of data. We have already spoken a little bit about 'strings'. A string is is text values. It can be a word, a sentence or syntax. Strings are always wrapped in single or double quotes. All of the following are strings:</p> 
    <p class="javascript">"This is a string!!"</p>
    <p class="javascript">'12343556456'</p>
    <p class="javascript">"$%$^$%&$%$#@$%^"</p>
    <p>Another data type is an array. An array is a list of values. It starts and ends in square bracket <span class="javascript">[ ]</span>. Here is an example of an array of strings:</p>
    <p class="javascript">var names = ['Kristina', 'Joe', 'Laura'];</p>
   <p>Create a variable in your JavaScript file and store an array of names. What happens if you print that array to the page by using the variable name?</p>
  </section>
  
  <h3>✂️ Step 6: Access a single element in an array</h3>
  <section>
    <p>In the step above, you printed the entire array of values to the document. What happens if you only want to use one of those values? With arrays, you can use the position of the element in the list to specify that you only want to use that element in the array.</p>
    <p>One tricky thing to note is that JavaScript (and many other programming languages) uses a zero based number system. That means that instead of starting a count at 1, we would start at 0. What this means is that the first item in an array has a position of 0. If I wanted to print out the first item in my array, it would look like this:</p>
    <p class="javascript">document.write(nameOfYourArray[0]);</p>
    <p>Can you print out the second item in your array to your document?</p>
  </section>
  
  <h3>💎 Step 7: Array.length</h3>
  <section>
  <p>Javascript has many built in methods which a programmer can call upon for more information about that array. One of these methods is Array.length. Array.length tells us how many items are in an array. To see it in action, try typing this in your JavaScript file:</p>
    <p class="javascript">document.write(nameOfYourArray.length);</p>
    <p>Note that Array.length does not use a 0 based system!</p>
  </section>
  
  <h3>➰ Bonus: For Loops</h3>
  <section>
    <p>Some times we might want to use all the values in an array. We could manually write out something like this:</p>
    <p class="javascript">var friends = ["Kate", "Maya", "Chad"];</p>
    <p class="javascript">document.write('I like ' + friends[0] + '.');</p>
    <p class="javascript">document.write('I like ' + friends[1] + '.');</p>
    <p class="javascript">document.write('I like ' + friends[2] + '.');</p>
   <p>But a quicker way might be to use a built in Javascript function called a for loop. A for loop looks like this. Can you figure out how it works? This uses the <span class='javascript'>friends</span> variable referenced above.</p>
    <p class="javascript"> </br>
      for (var i = 0; i < friends.length; i++) {</br>
      &emsp; document.write('I like ' + friends[i] + '.');</br>
      };
    </p>
  </section>

</main>
              
            
!

CSS

              
                body {
  font-family: Arial Black;
  margin: 5vh 10vw;
  color: red;
  word-wrap: break-word
}

h1, h2, h3 {
  color: black;
  letter-spacing: 0.2rem;
}

h3 {
  color: black;
  font-family: Arial;
}

section {
  border: 2px dashed black;
  padding: 1rem 2rem;
}

p {
  color: black;
  font-family: Arial;
}

.javascript {
  background-color: black;
  color: white;
  font-family: Courier New;
  font-size: 1;
  padding: 0 0.5rem;
}

              
            
!

JS

              
                // Step 1:
// Create an alert
// After you see it, comment it out

// Step 2:
// A. Declare a variable
// B. Assign a value to your variable
// Bonus: Declare and assign your variable in one line

// Step 3:
// Print your variable to the document

// Step 4:
// Print that variable to the document within a sentence
// document.write('Hi ' + name + '!');
// document.write('You are ' + number + '% cool.')

// Step 5:
// Create a variable that is stored in an array.
// Using document.write, write your array to the page. 

// Step 6:
// Print out the second item in your array using its zero based position

// Step 7:
// Write out the length of your array to your document

// Bonus:
// Loop through your array and print out each element within a sentence.
              
            
!
999px

Console