HTML and CSS cannot count, cannot compare, cannot remember. JavaScript can. It is a real programming language, it is built into every browser, and it is what makes a page do something instead of just sitting there.
It goes inside a <script> tag:
<p>Look in the box below.</p>
<pre id="__out"></pre>
<script>
console.log("Hello from JavaScript!");
</script>Try it โ this is live
console.log(...) is JavaScript's print(). It writes a line out where a programmer can read it.
๐ฆ Variables
Same idea as Python, with one extra word at the front:
let score = 100;
const name = "Maya";
let โ a box whose contents can change later.
const โ a box that is set once and never changed.
let score = 10;
console.log(score);
score = score + 5;
console.log(score);
const name = "Maya";
console.log(name);Try it โ this is live
๐ Reach for const first
Use const unless you know the value has to change. A box that cannot change is one less thing that can go wrong โ and when you read the code later, const promises you it is still what it was.
๐ If you know Pythonโฆ
Python
JavaScript
score = 10
let score = 10;
say let or const
print(x)
console.log(x);
same job
# a note
// a note
two slashes
True / False
true / false
lower case
None
null
nothing at all
indentation
{ }
braces group things
The ideas are identical. Variables, text, numbers, decisions, loops โ you are not learning to program twice, you are learning one thing in a second accent.
๐ข Numbers and text
let a = 7;
let b = 2;
console.log(a + b);
console.log(a - b);
console.log(a * b);
console.log(a / b);
console.log(a % b);Try it โ this is live
Note 7 / 2 gives 3.5 โ JavaScript has only one kind of number, so no whole-number division surprise.
Joining text together is done with + as well, but there is a much nicer way โ backticks with ${...} holes in them:
const name = "Maya";
const age = 11;
console.log("Hi " + name + ", you are " + age);
console.log(`Hi ${name}, you are ${age}`);Try it โ this is live
โ ๏ธ + means two different things
2 + 3 is 5. But "2" + 3 is "23", because a piece of text next to a + turns the whole thing into joining. This catches everybody once.
๐งฐ A few things you can ask of text
const word = "dragon";
console.log(word.length);
console.log(word.toUpperCase());
console.log(word[0]);
console.log(word.includes("ago"));Try it โ this is live
๐ length has no brackets
word.length is a fact about the text, so no brackets. word.toUpperCase() is something you ask it to do, so it gets brackets. That difference runs all the way through the language.
โ What you learned
JavaScript lives in <script> tags and runs in the browser.
console.log() prints. // starts a comment.
let for a box that changes, const for one that does not.
Backticks and ${ } beat gluing text with +.
Lines usually end with a semicolon.
๐ฎ Time to Practice!
These tasks have no page to look at โ the output box below each one is where your console.log lines appear.
๐
Task 1: Hello, Console
Easy
Print the exact words Hello, JavaScript!
console.log("Hello, JavaScript!");
๐ฆ
Task 2: Two Boxes
Easy
Make a const called pet holding "Buddy" and a let called age holding 7. Print each one on its own line โ the values, not the words.
const pet = "Buddy";
let age = 7;
console.log(pet);
console.log(age);
๐งฎ
Task 3: A Little Maths
Medium
Two variables, a = 12 and b = 5. Print their sum, then their difference, then their product โ three lines, in that order.
const a = 12;
const b = 5;
console.log(a + b);
console.log(a - b);
console.log(a * b);
๐๏ธ Extra Practice โ Drill Time!
This is where it sticks. Try every one โ and if you get stuck, read your own earlier answers before you peek.
๐ฌ
Task 4: Use a Template
Medium
Make const name = "Sam" and const age = 10, then print exactly Sam is 10 years old using backticks and ${ } โ not +.
const name = "Sam";
const age = 10;
console.log(`${name} is ${age} years old`);
๐
Task 5: Change the Box
Easy
Make let score = 10. Print it. Add 5 to it and print it again. Double it and print it a third time.
Make const word = "elephant". Print its length, then the whole word in capitals, then its first letter โ three lines.
const word = "elephant";
console.log(word.length);
console.log(word.toUpperCase());
console.log(word[0]);
๐ชค
Task 7: The Plus Trap
Hard
Print three lines that show the trap. Line 1: 2 + 3 as numbers. Line 2: "2" + 3 with the 2 in quotes. Line 3: the word different. Your output should read 5, then 23, then different.
const item = "Cake", const price = 4, const many = 3. Print exactly two lines: Cake x3 Total: 12 Work the total out with * โ do not type 12 yourself.