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




