Skip to content

JavaScript Basics Data Types | Lecture 31

JavaScript Basics

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

TYPEEXAMPLE
String"Hello" โ€“ text inside quotes
Number25, 3.14 โ€“ whole or decimal numbers
Booleantrue, false โ€“ yes/no values
UndefinedA variable without value
NullRepresents no value
๐Ÿงฎ Basic Operators
OPERATORMEANING
+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!

JavaScript Basics
JavaScript Basics

๐Ÿ’ก 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!

  1. Add a color guesser โ€“ ask users to guess a color and show success or error.
  2. Create a simple calculator that adds two numbers from inputs and shows result.
  3. Write a script that changes greeting based on time of day using Date() object.
  4. Make a rock-paper-scissors game using prompt() and alert().

โœ… Final Tips

CONCEPTCODE EXAMPLE
Variablelet name = "Alex";
String"This is text"
Number25, 3.14
Booleantrue, false
If Statementif (age > 18) { ... }
Change HTMLdocument.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

Najeeb Alam

Najeeb Alam

Technical writer specializes in developer, Blogging and Online Journalism. I have been working in this field for the last 20 years.