Site icon Bernard Aybout's Blog – MiltonMarketing.com

Learn more about JavaScript Variables

Learn more about JavaScript Variables

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.

Exit mobile version