Let’s begin with Lecture 31 of the HTML5 & CSS3 series. JavaScript Basics โ Variables, Data Types & Conditionals. This lecture is for absolute beginners; even kids can follow along and understand.
๐ฏ Objective:
By the end of this lecture, you will:
- Understand what variables are and how to use them
- Know the basic data types in JavaScript (strings, numbers, booleans)
- Be able to use conditionals (
if
,else if
,else
) to make decisions - Create simple logic that responds to user input or page state
- Practice with real examples like age checkers , password validators , and theme switchers
๐ง What Is JavaScript?
JavaScript is a programming language that runs in your browser.
It lets you make your website interactive โ like showing messages, checking answers, or changing colors when someone clicks.
Think of it like this:
If HTML is the body and CSS is the clothes, then JavaScript is the brain that makes decisions and does things!
๐ค Variables โ Store Information
Variables are like labeled boxes where you can store values.
Syntax:
let name = "Alex";
let age = 12;
let isStudent = true;
Use let
or const
to declare variables:
- Use
let
for values that might change - Use
const
for values that wonโt change
๐ฆ JavaScript Data Types
TYPE | EXAMPLE |
---|---|
String | "Hello" โ text inside quotes |
Number | 25 , 3.14 โ whole or decimal numbers |
Boolean | true , false โ yes/no values |
Undefined | A variable without value |
Null | Represents no value |
๐งฎ Basic Operators
OPERATOR | MEANING |
---|---|
+ | Addition or string concatenation |
- , * , / | Subtraction, multiplication, division |
== , === | Equality checks |
!= , !== | Not equal |
< , > | Less than, greater than |
&& , ` |
๐งญ Conditionals โ Make Decisions
Conditionals let your code make choices based on conditions.
Basic Syntax:
if (condition) {
// Do something
} else if (another condition) {
// Do something else
} else {
// Default action
}
๐ป Try This: Build a Simple Age Checker
Letโs create a script that checks if someone is old enough to play a game.
Step 1: Add HTML
In your portfolio.html
or a new file named age-checker.html
:
<h2>Age Checker</h2>
<label for="userAge">Enter your age:</label>
<input type="number" id="userAge">
<button onclick="checkAge()">Check Age</button>
<p id="ageResult"></p>
Step 2: Add JavaScript
Add this before </body>
or in an external script:
<script>
function checkAge() {
let age = document.getElementById("userAge").value;
if (age >= 18) {
document.getElementById("ageResult").innerHTML = "You're old enough to play!";
} else if (age > 0 && age < 18) {
document.getElementById("ageResult").innerHTML = "Sorry, you're too young to play.";
} else {
document.getElementById("ageResult").innerHTML = "Please enter a valid age.";
}
}
</script>
๐ Now users can enter their age and get a response instantly!

๐ก Bonus: Password Validator
Letโs add another interactive example โ a password checker.
Add HTML:
<h2>Password Validator</h2>
<label for="userPass">Enter password:</label>
<input type="password" id="userPass">
<button onclick="checkPassword()">Submit</button>
<p id="passResult"></p>
Add JavaScript:
<script>
function checkPassword() {
let password = document.getElementById("userPass").value;
if (password === "secret") {
document.getElementById("passResult").innerHTML = "โ
Access granted!";
} else {
document.getElementById("passResult").innerHTML = "โ Wrong password!";
}
}
</script>
Now users can try typing in the correct password and see feedback instantly!
๐งช Try It Yourself!
- Add a color guesser โ ask users to guess a color and show success or error.
- Create a simple calculator that adds two numbers from inputs and shows result.
- Write a script that changes greeting based on time of day using
Date()
object. - Make a rock-paper-scissors game using
prompt()
andalert()
.
โ Final Tips
CONCEPT | CODE EXAMPLE |
---|---|
Variable | let name = "Alex"; |
String | "This is text" |
Number | 25 , 3.14 |
Boolean | true , false |
If Statement | if (age > 18) { ... } |
Change HTML | document.getElementById("result").innerHTML = "New Text"; |
๐ Next Lecture Preview:
In Lecture 32 , weโll learn about functions and events in JavaScript โ so you can write reusable blocks of code and respond to actions like clicks, hovers, and form submissions!
Stay Updated
If you found this information useful, donโt forget to bookmark this page and Share.
HTML5 and CSS3 Compleate Series