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
visitorName
and assign your name to it. -
Create a variable called
welcomeMessage
and store this message in it:"Welcome back, "
followed by the visitorβs name. -
Print the message to the console.
-
Create a variable named
loginAttempts
and set it to3
. -
Print this sentence using a variable:
"You have X login attempts remaining."
(whereX
is 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