⚡ JavaScript Basics
Welcome to JavaScript!
From visual blocks to real code - let's write programs that power the web!
What is JavaScript?
JavaScript is a real programming language used to make websites interactive! Every website you visit - Shopee, YouTube, Instagram - uses JavaScript to respond to your clicks, show animations, and update content.
Remember Scratch blocks? JavaScript does the same things, but with text-based code that professional programmers use!
From Scratch to JavaScript
Let's see how Scratch blocks translate to JavaScript code:
🧩 Scratch Block
say "Hello, Malaysia!" for 2 seconds
⚡ JavaScript Code
Key Concepts
Syntax
JavaScript has specific rules for writing code - like grammar in language!
console.log()
Displays messages in the browser console - your programming output window!
Variables
Containers that store data - like naming your sprite in Scratch!
Operators
Symbols like +, -, *, / for math and logic operations.
Semicolons
End each statement with ; - like a period in a sentence!
Browser Console
Built-in tool to test JavaScript code and see output.
Your First JavaScript Code
🎯 Hello World Program
What this does: Prints three messages to the console - one in English, Malay, and Chinese!
Variables - Storing Information
Variables let us store and reuse data. Think of them as labeled boxes!
💰 Calculating Roti Canai Pricing
Output: Price: RM4.5
Explanation:
letcreates a new variable=assigns a value to the variable*multiplies the values+joins text and numbers together
Basic Operators
🧮 Math in JavaScript
Note: Everything after // is a comment - notes for humans, ignored by the
computer!
Opening the Browser Console
- Windows/Linux: Press F12 or Ctrl + Shift + J
- Mac: Press Cmd + Option + J
- Or right-click any webpage → Inspect → Console tab
Try typing: console.log("Hello!") and press Enter!
Common Mistakes to Avoid
⚠️ Watch Out For:
- Missing semicolons: End each line with ;
- Wrong quotes: Use matching "quotes" or 'quotes'
- Case sensitivity: console.log works, Console.Log doesn't!
- Spelling errors: JavaScript won't understand typos
Summary
You've learned:
- ✅ JavaScript is a real programming language for the web
- ✅ console.log() displays output in the browser console
- ✅ Variables store data using the let keyword
- ✅ Operators (+, -, *, /) perform calculations
- ✅ Every statement ends with a semicolon (;)
- ✅ How to open and use the browser console
🎮 Interactive Challenge: Write JavaScript!
Open your browser console (F12 or Cmd+Option+J) and try these exercises:
📝 Challenge 1: Your First Messages
Type these commands in the console, one at a time:
Goal: See your messages appear in the console!
🧮 Challenge 2: Calculator Fun
Calculate these in the console:
💰 Challenge 3: Nasi Lemak Stall
Create a pricing calculator:
Try: Change the prices and recalculate!
🎯 Offline Activity: Convert Scratch to JavaScript
What You'll Do:
Take your favorite Scratch programs and convert them to JavaScript code!
Instructions:
- Open one of your Scratch projects (or create a simple one)
- Look at the blocks you used
- Write down what each block does in plain language
- Try to write similar code in JavaScript using console.log() and variables
- Test your code in the browser console!
Example Conversion:
Scratch Program
When clicked:
Set score to 0
Change score by 10
Say score
JavaScript Code
💪 Practice Challenges
Challenge 1: Message Master
Write JavaScript code that displays these messages in order:
- Your name and age
- Your favorite Malaysian food
- Your dream job
- Why you want to learn programming
Hint: Use 4 console.log() statements!
Challenge 2: School Store Calculator
You're shopping at the school bookstore. Calculate the total:
Expected output: Total: RM5.30
Challenge 3: Mamak Restaurant Bill
Calculate the total bill for this order:
- 2 Roti Canai @ RM1.50 each
- 3 Teh Tarik @ RM2.50 each
- 1 Nasi Goreng @ RM6.00
Write the JavaScript code with variables for each item!
Challenge 4: Temperature Converter
Create a program that converts 30°C to Fahrenheit:
Answer should be: 86°F
Challenge 5: Debug the Code!
Find and fix the errors in this code:
Errors to fix: Missing semicolons, wrong capitalization!