⚡ Rocket.net – Managed WordPress Hosting

MiltonMarketing.com  Powered by Rocket.net – Managed WordPress Hosting

Bernard Aybouts - Blog - MiltonMarketing.com

Approx. read time: 3.6 min.

Post: Learn more about JavaScript Variables

📘 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:

var userName = "Bob";

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

var userAge = 30;

➡️ We stored the number 30 in a variable called userAge.


🟢 Example 2: Store a website title

var siteTitle = "MiltonMarketing";

➡️ Here, "MiltonMarketing" is a string stored in the variable siteTitle.


🟢 Example 3: Use a variable in a sentence

Copy to Clipboard

➡️ 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:

var correctPassword = "letMeIn123";

Later in the program, we might use this to compare with what the user typed:

Copy to Clipboard

💡 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:

let score = 100;
const pi = 3.14;

Use var only when needed for compatibility or learning basics.


📝 Final Practice Assignment

✍️ Instructions:

Write JavaScript code to do the following:

  1. Create a variable named visitorName and assign your name to it.

  2. Create a variable called welcomeMessage and store this message in it:
    "Welcome back, " followed by the visitor’s name.

  3. Print the message to the console.

  4. Create a variable named loginAttempts and set it to 3.

  5. Print this sentence using a variable:
    "You have X login attempts remaining."
    (where X is replaced by the value from loginAttempts)


✅ Answer Key

Copy to Clipboard

📚 Summary

  • Variables store information in JavaScript.

  • You define a variable using a keyword (var, let, or const), 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.

About the Author: Bernard Aybout (Virii8)

Avatar of Bernard Aybout (Virii8)
I am a dedicated technology enthusiast with over 45 years of life experience, passionate about computers, AI, emerging technologies, and their real-world impact. As the founder of my personal blog, MiltonMarketing.com, I explore how AI, health tech, engineering, finance, and other advanced fields leverage innovation—not as a replacement for human expertise, but as a tool to enhance it. My focus is on bridging the gap between cutting-edge technology and practical applications, ensuring ethical, responsible, and transformative use across industries. MiltonMarketing.com is more than just a tech blog—it's a growing platform for expert insights. We welcome qualified writers and industry professionals from IT, AI, healthcare, engineering, HVAC, automotive, finance, and beyond to contribute their knowledge. If you have expertise to share in how AI and technology shape industries while complementing human skills, join us in driving meaningful conversations about the future of innovation. 🚀