HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
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.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<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>
  document.write('I like ' + friends[i] + '.');</br>
};
</p>
</section>
</main>
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;
}
// 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.
Also see: Tab Triggers