Learn JavaScript VARIABLES in 13 minutes! (12:45)
Variables are containers that store values in JavaScript. Learn how to declare and assign variables, understand different data types (numbers, strings, booleans), and display values in both the console and on web pages.
- Variable declaration and assignment
- The
letkeyword - Three basic data types: numbers, strings, and booleans
- Using
typeofto check data types - Template literals for displaying variables
- Displaying variables in HTML elements
- Variable naming best practices
A variable is a container that stores a value. The variable behaves as if it were the value it contains.
Use the let keyword to declare a variable:
let x;
let y;Important: Each variable name must be unique!
let x;
let x; // ❌ Error: identifier 'x' has already been declaredAssign a value to your variable using =:
let x;
x = 100;
console.log(x); // Output: 100// Both steps at once
let x = 123;
console.log(x); // Output: 123Numbers can be whole numbers or decimals.
// Age
let age = 25;
console.log(age); // 25
// Price
let price = 10.99;
console.log(price); // 10.99
// GPA (Grade Point Average)
let gpa = 2.1;
console.log(gpa); // 2.1Use backticks ` and ${} to insert variables into strings:
let age = 25;
let price = 10.99;
let gpa = 2.1;
console.log(`You are ${age} years old`);
// Output: You are 25 years old
console.log(`The price is $${price}`);
// Output: The price is $10.99
console.log(`Your GPA is: ${gpa}`);
// Output: Your GPA is: 2.1Use typeof to check a variable's type:
let age = 25;
console.log(typeof age); // number
let price = 10.99;
console.log(typeof price); // numberA string is a series of characters. Use double quotes " or single quotes '.
// First name
let firstName = "Bro";
console.log(typeof firstName); // string
console.log(firstName); // Bro
// Favorite food
let favoriteFood = "pizza";
console.log(`You like ${favoriteFood}`);
// Output: You like pizza
// Email
let email = "bro123@gmail.com";
console.log(`Your email is ${email}`);
// Output: Your email is bro123@gmail.comlet username = "bro123";
console.log(username); // bro123
console.log(typeof username); // stringImportant: Numbers inside strings are text, not numbers for math!
let age = "25"; // String, not number
console.log(age + 5); // "255" (concatenation, not addition)
let realAge = 25; // Number
console.log(realAge + 5); // 30 (actual addition)A boolean is either true or false. Typically used as flags in programs.
// Is someone online?
let online = true;
console.log(typeof online); // boolean
console.log(`Bro is online: ${online}`);
// Output: Bro is online: true
// Is item for sale?
let forSale = true;
console.log(`Is this car for sale: ${forSale}`);
// Output: Is this car for sale: true
// Is student enrolled?
let isStudent = false;
console.log(`Enrolled: ${isStudent}`);
// Output: Enrolled: falseBooleans are typically used with if statements (covered later):
let online = true;
if (online) {
console.log("User is online");
} else {
console.log("User is offline");
}<p id="p1"></p>
<p id="p2"></p>
<p id="p3"></p>let fullName = "Bro Code";
let age = 25;
let isStudent = false;
// Simple assignment
document.getElementById("p1").textContent = fullName;
document.getElementById("p2").textContent = age;
document.getElementById("p3").textContent = isStudent;Output on webpage:
Bro Code
25
false
let fullName = "Bro Code";
let age = 25;
let isStudent = false;
document.getElementById("p1").textContent = `Your name is ${fullName}`;
document.getElementById("p2").textContent = `You are ${age} years old`;
document.getElementById("p3").textContent = `Enrolled: ${isStudent}`;Output on webpage:
Your name is Bro Code
You are 25 years old
Enrolled: false
// Numbers
let age = 25;
let price = 10.99;
let gpa = 2.1;
// Strings
let firstName = "Bro";
let favoriteFood = "pizza";
let email = "bro@gmail.com";
// Booleans
let online = true;
let forSale = false;
let isStudent = true;
// Console output with template literals
console.log(`You are ${age} years old`);
console.log(`The price is $${price}`);
console.log(`Your GPA is: ${gpa}`);
console.log(`Your name is ${firstName}`);
console.log(`You like ${favoriteFood}`);
console.log(`Your email is ${email}`);
console.log(`Bro is online: ${online}`);
console.log(`Is this car for sale: ${forSale}`);
console.log(`Enrolled: ${isStudent}`);let age = 25;
let name = "Bro";
let online = true;
console.log(typeof age); // number
console.log(typeof name); // string
console.log(typeof online); // booleanCreate variables for your personal information and display them:
// Declare and assign variables
let fullName = ""; // Your full name
let age = 0; // Your age
let city = ""; // Your city
let isStudent = false; // Are you a student?
// Display in console with template literals
// Your code hereCreate variables for a product:
let productName = "";
let price = 0;
let inStock = false;
// Display: "Product: [name], Price: $[price], Available: [true/false]"Identify the data type:
let a = 42;
let b = "42";
let c = true;
let d = "Hello";
let e = 3.14;
// Use typeof to check each oneCreate variables and display them on a webpage:
let userName = "Your Name";
let userAge = 0;
let isPremium = false;
// Display each in its own paragraph elementlet age = 25;
let firstName = "John";
let isStudent = true;
let totalPrice = 99.99;
let userEmail = "user@example.com";let a = 25; // Not descriptive
let x123 = "John"; // Meaningless
let thing = true; // Too vague
let $ = 99.99; // Confusing- ✅ Use camelCase:
firstName,totalPrice - ✅ Start with a letter,
_, or$ - ✅ Can contain letters, numbers,
_,$ - ❌ Cannot start with a number
- ❌ Cannot use reserved keywords (
let,if,true, etc.) - ❌ Cannot contain spaces or special characters
| Type | Example | Use Case | typeof |
|---|---|---|---|
| Number | 25, 10.99, -5 |
Math, calculations, age, price | "number" |
| String | "Hello", "Bro123" |
Text, names, messages, emails | "string" |
| Boolean | true, false |
Flags, conditions, status | "boolean" |
let age = 25;
let age = 30; // ❌ Error: 'age' has already been declared
// ✅ Correct: Just reassign
let age = 25;
age = 30; // Works!let age = "25"; // String!
console.log(age + 5); // "255" (not 30!)
// ✅ Use number
let age = 25;
console.log(age + 5); // 30let name = Bro; // ❌ Error: Bro is not defined
// ✅ Correct
let name = "Bro";Creating a variable with let:
let age;Giving the variable a value:
age = 25;let age = 25;Embedding variables in strings:
console.log(`I am ${age} years old`);- Variables are containers that store values
- Use
letto declare variables - Three basic data types:
- Number: 25, 10.99, -5
- String: "text", "hello", "123"
- Boolean: true, false
- Declaration:
let x; - Assignment:
x = 10; - Both:
let x = 10; - Template literals use backticks and
${variable} - Use
typeofto check data type - Variable names must be unique
- Use camelCase for multi-word names
- Variables behave as if they were the value they contain
JavaScript ARITHMETIC OPERATORS
Duration: 12:45
Difficulty: Beginner
Category: Fundamentals