📘 JavaScript Lesson: Understanding Variables
🧠 Lesson Objective
By the end of this lesson, you’ll be able to:
-
Define and declare variables in JavaScript.
-
Differentiate between variable keywords, names, and values.
-
Store different types of data in variables.
-
Write interactive code that uses variables.
🔍 What Are Variables in JavaScript?
A variable is like a box where you can store information to use later in your program. You might store a name, a number, or even a password. Think of it like writing something down so your browser doesn’t forget.
Without variables, a browser can’t remember anything. Every time you refresh a page, everything resets unless you’ve stored the data somewhere—like in a variable.
🧱 Basic Syntax of a JavaScript Variable
Here’s a simple variable:
Let’s break that down:
-
var→ This is the keyword. It tells JavaScript, “Hey! I’m creating a variable.” -
userName→ This is the name of the variable. It’s how we refer to this piece of stored data. -
=→ This is the assignment operator. It means “give the value on the right to the variable on the left.” -
"Bob"→ This is the value being stored (a string in this case). -
;→ This ends the statement. It’s like a period in English.
🧪 More Examples
🟢 Example 1: Store a user’s age
➡️ We stored the number 30 in a variable called userAge.
🟢 Example 2: Store a website title
➡️ Here, "MiltonMarketing" is a string stored in the variable siteTitle.
🟢 Example 3: Use a variable in a sentence
➡️ Output: Hello, Alice!
This combines two strings together using the + operator.
🧠 Rules for Naming Variables
When creating a variable name:
✅ It must begin with a letter (a–z, A–Z), an underscore _, or a dollar sign $.
❌ It can’t begin with a number (e.g., 1user is invalid).
✅ Use camelCase for multiple words (e.g., userPassword or currentScore).
❌ You can’t use spaces or symbols like - in a variable name.
| Type | Example | Description |
|---|---|---|
| String | “Hello” | Text, enclosed in double or single quotes |
| Number | 42 | Numeric value (integer or decimal) |
| Boolean | true / false | Logical value, either true or false |
| Null | null | Represents a deliberate non-value |
| Undefined | undefined | A variable that has been declared but not assigned a value |
🔐 Example: Store a Password
Let’s say we want to store the correct password for a login screen:
Later in the program, we might use this to compare with what the user typed:
💡 Pro Tip: Use let and const in Modern JavaScript
Although this lesson uses var (which works), modern JavaScript often uses:
-
let→ for variables that may change. -
const→ for variables that will not change.
Example:
Use var only when needed for compatibility or learning basics.
📝 Final Practice Assignment
✍️ Instructions:
Write JavaScript code to do the following:
-
Create a variable named
visitorNameand assign your name to it. -
Create a variable called
welcomeMessageand store this message in it:"Welcome back, "followed by the visitor’s name. -
Print the message to the console.
-
Create a variable named
loginAttemptsand set it to3. -
Print this sentence using a variable:
"You have X login attempts remaining."
(whereXis replaced by the value fromloginAttempts)
✅ Answer Key
📚 Summary
-
Variables store information in JavaScript.
-
You define a variable using a keyword (
var,let, orconst), a name, and a value. -
Strings must be inside double quotes.
-
Variable names must follow specific rules.
-
You can use variables to build dynamic, interactive websites.
Related Videos:
Related Posts:
Online JavaScript Compiler. Code on the go.
Storing and Modifying Information (CRUD)
JavaScript Glossary – Variables
Object-Oriented Programming (OOP)
Building a web page with HTML tags
Defining What an Application Is
Comparing Python to other languages
Introduction to JavaScript – Create a Variable: const
Python Essentials: A Beginner’s Guide to Coding with Python
Introduction to JavaScript – Create a Variable: let
Learn more about JavaScript Operators
Introduction to JavaScript – Libraries
How do I start a WordPress blog? (hosting wordpress)
Helping humans speak to the computer
Learn Code Introspection Python Programming
Introduction to JavaScript – Variables: Review
Introduction to JavaScript – Variables: String Interpolation
Redirect New Registered Users to a Specific Page – WordPress
