Make Your Code Think
HTML builds the page. CSS styles it. JavaScript can ask questions, remember answers, do maths and make decisions.
Put <script> near the bottom
For now, put your <script> just before the closing </body> tag.
<body>
<h1>My Game</h1>
<button>PLAY</button>
<script>
// JavaScript goes here
</script>
</body>
1. Your First JavaScript
JavaScript goes inside a <script> tag. Try the smallest useful JavaScript command: alert().
Hello! to your own message. Then add a second alert().
2. Let JavaScript Remember Something
let creates a variable. Think of a variable as a named box that stores something.
"Daniel" is text. playerName is a variable name, so it has no quotes.
3. Ask the Player a Question
prompt() asks a question and gives JavaScript the answer.
+ can join text together.
"Hello " + name joins the words Hello with whatever the player typed.
"Welcome to the game, " + name + "!"
4. Store the Question Too
Variables can store the question, the answer, or almost anything else we need later.
5. JavaScript Can Do Maths
Use +, -, * and / for maths.
+ add - subtract * multiply / divide
6. Maths with Answers
We can use answers from prompt() in simple maths. For now, multiplication is especially easy.
7. Make a Decision with if
if means: only run this code when something is true.
8. if ... else
else gives JavaScript something different to do when the answer is not correct.
9. Comparisons
Comparisons ask questions that can be true or false.
==equal to!=not equal to>greater than<less than>=greater than or equal<=less than or equalscore > 100age >= 10answer != "no"
10. Put It Together
These are small, but they use real programming: input, variables, maths and decisions.
Personal Greeting
Ask the player's name and welcome them to the game.
Maths Calculator
Ask for two numbers and show the product, area or difference.
Guessing Game
Ask for a secret number and use if/else to say whether the guess is correct.
JavaScript 1 Quick Reference
<script>Where JavaScript goes.alert("Hi")Show a message.prompt("Question?")Ask the user.let score = 5Store a value."Hello"A string: text in quotes."Hi " + nameJoin text and values.+ - * /Math operators.if (...) { }Run code when true.else { }Otherwise do this.== !=Equal / not equal.< <= > >=Compare numbers.Ready to make some mini programs?
Start with quick checks, then build a calculator and guessing game.