Studio X Help Guide
JavaScript Basics
Make your profile react, change and move.
JavaScript makes your profile do things. You write it inside a <script> block. Put your scripts at the bottom of Your Content, after the HTML they work with, so that HTML already exists when the script runs.
Changing what's on the page
Give an element an id, find it with document.getElementById, then change it. textContent is its text; style changes its CSS.
var el = ... creates a variable: a name for something so you can use it again. Lines end with a semicolon. Text goes in quotes.
Reacting to clicks
Set an element's onclick to a function: a bundle of instructions that runs every time it's clicked.
Showing and hiding things
classList.toggle adds a class if it's missing and removes it if it's there. Pair it with CSS for show/hide, dark mode, and more.
Lists of things: arrays and loops
An array is a list in square brackets. A for loop goes through it one item at a time; here it builds an HTML list for you.
Random picks
Math.random() gives a random number between 0 and 1. Multiply by the list length and round down to pick a random item.
Making decisions: if and else
Show a different greeting depending on the visitor's time of day:
Timers
setInterval runs something over and over (every 1000 milliseconds = every second). setTimeout runs something once, after a delay.
Dates and counting days
Working with many elements at once
document.querySelectorAll finds everything matching a CSS selector. Then forEach does something to each one.
Seeing what your script is doing
console.log("hello") prints a message to your browser's developer console, a hidden panel that also shows any errors. Open it with F12 (or Cmd+Option+J on a Mac in Chrome, Cmd+Option+C in Safari once developer tools are turned on). If a script does nothing at all, the console almost always says why.
<script>
console.log("My script is running!");
</script>
Learning more
JavaScript is huge, and everything you learn elsewhere works here too, within the sandbox's limits. The next chapter explains those limits, which are few but important.
