HTML is structure. CSS is style. JavaScript is behavior: the thing that reacts, calculates, remembers, and updates the page.
This is where the calculator stops being a picture and starts doing something. But first, the basics.
Where JavaScript goes
You can put JavaScript in a <script> tag:
<script src="script.js" defer></script>
Put it in the <head> with defer, or just before </body>. Either way, the script waits until the HTML has been parsed, so your code can find the elements it needs.
You can also run JavaScript in the browser’s DevTools console. Open DevTools (F12 or right-click → Inspect), click Console, and type. It’s the fastest way to test a line.
Variables
A variable is a named box that holds a value.
const name = "Chicken and rice";
let calories = 550;
calories = 600; // fine: let can change
// name = "Rice"; // error: const can't be reassigned
- Use
constby default. It means “this name won’t point to a different value.” - Use
letwhen the value needs to change. - Avoid
var. It’s the old way and has confusing scoping rules.
Types
JavaScript has a small set of basic types:
| Type | Examples | What it’s for |
|---|---|---|
string |
"hello", 'hello' |
Text |
number |
42, 3.14, -7 |
Numbers (no separate int/float) |
boolean |
true, false |
Yes/no |
null |
null |
“Nothing, on purpose” |
undefined |
undefined |
“Nothing, not set yet” |
object |
{ name: "Rice" }, arrays |
Collections of values |
typeof tells you what you’re looking at:
console.log(typeof "hello"); // "string"
console.log(typeof 42); // "number"
console.log(typeof true); // "boolean"
console.log(typeof undefined); // "undefined"
console.log(typeof null); // "object" (a famous JavaScript quirk)
Strings
const food = "Chicken and rice";
const calories = 550;
console.log(food.length); // 16
console.log(food.toUpperCase()); // "CHICKEN AND RICE"
console.log(food.includes("rice")); // true
console.log(" padded ".trim()); // "padded"
Template literals make it easy to build sentences:
const message = `${food} has ${calories} kcal.`;
console.log(message);
The backticks let you drop values into a string with ${...}. It’s cleaner than gluing strings together with +.
Try it
The playground below has no HTML or CSS — just JavaScript and the console. Hit Run, then change the values and run it again.
Console
Console is quiet. Run the code to see output.
Edit the code, then hit Run (or Ctrl + Enter) and watch the console. This is a safe sandbox — you can't break the page.
Your first variables
the plan's verify stepCreate variables for a meal: a name, a number of calories, and whether you enjoyed it. Log each one, then log a sentence like 'Oatmeal has 320 kcal.' using a template literal. Finally, log the typeof each variable.
If you see undefined, the variable exists but has no value yet. If you see a ReferenceError, the name doesn't exist.
The mistakes everyone makes
- Reassigning a
const. Useletwhen the value changes. - Using
varout of habit. Useconstorlet. - Forgetting quotes around text.
const food = Chicken;looks for a variable namedChicken. - Confusing
nullandundefined.undefinedmeans “not set”;nullmeans “set to nothing on purpose.” - Adding a string and a number.
"5" + 1is"51", not6. Convert first. - Typo in a variable name. JavaScript is case-sensitive:
foodandFoodare different.
JavaScript is the behavior layer: it reacts, calculates, and updates.
Use const by default, let when the value changes, and avoid var.
The basic types are string, number, boolean, null, undefined, and object.
Template literals use backticks and ${...} to insert values.
console.log is your first debugging tool.
Next: conditions and functions. The calculator needs to make decisions and reuse logic.
Up next
Logic: conditions, comparisons, and functions
Make the code decide: if this, then that. Then wrap the decisions in functions you can reuse — the building blocks of the calculator's math.