Chapter 2: Variables and Data Types in JavaScript
By Ahmed Thaer
What is a Variable?
Think of a variable like a labeled box where you can store information. You can put things in the
box, take them out, or replace them. In JavaScript, variables allow you to save data—like
numbers, text, or even more complex things—so you can use them later in your program.
Declaring Variables
When I started with JavaScript, I learned there are three main ways to create (declare) a
variable: var, let, and const. Here’s what I use and why:
● let: My go-to for most cases. I can change (update) the value later.
● const: I use this if I don’t want the value to ever change.
● var: An old-school way; I rarely use it because let and const are better for modern
code.
Examples:
javascript
CopyEdit
let age = 26;
const name = 'Ahmed';
var city = 'Dallas';
Naming Variables
There are some simple rules for naming variables in JavaScript:
● Use letters, numbers, _ or $
● Don’t start with a number
● Be descriptive! (For example: userAge is better than x)
● JavaScript is case-sensitive (Name is different from name)
Data Types
Variables can store different kinds of data. Here are the most common types I use:
1. String: Text, written inside quotes
let greeting = 'Hello, world!';
2. Number: Any number, with or without decimals
let score = 95;
let price = 12.5;
3. Boolean: True or false (great for logic!)
let isOnline = true;
let isAdmin = false;
Undefined: If you declare a variable but don’t give it a value, it’s undefined
javascript
CopyEdit
let notSet;
console.log(notSet); // undefined
4.
5. Null: Intentionally means “no value”
let selectedUser = null;
6. Object: Used to store collections of data
let user = { name: 'Ahmed', age: 26 };
7. Array: A list of values
let colors = ['red', 'green', 'blue'];
Working with Variables
You can change the value stored in a variable if you used let or var:
javascript
CopyEdit
let mood = 'happy';
console.log(mood); // happy
mood = 'excited';
console.log(mood); // excited
But if you used const, you can’t change it:
javascript
CopyEdit
const birthYear = 1999;
// birthYear = 2000; // This would cause an error!
Type Checking
To check the type of a variable, use typeof:
javascript
CopyEdit
let amount = 20;
console.log(typeof amount); // "number"
Quick Practice
Try this out in your browser console:
javascript
CopyEdit
let myName = 'Ahmed';
let myAge = 26;
let isStudent = false;
console.log(myName, myAge, isStudent);
console.log(typeof myName); // string
console.log(typeof myAge); // number
console.log(typeof isStudent); // boolean
Summary
In this chapter, I covered:
● What variables are and how to create them
● Different ways to declare variables (let, const, var)
● Common data types in JavaScript
● How to check a variable’s type
Getting comfortable with variables and data types is the foundation for everything you’ll do in
JavaScript. In the next chapter, I’ll dive into basic operations and expressions—how to use
variables to do math, combine strings, and more.