Variables
A variable is a named box in memory where your program keeps a value, like a name, an age or a price.
Creating a Variable
Imagine a label on a jar in a kitchen: the label is the variable's name, and what is inside is its value.
In Python you just write the name, an equals sign =, and the value:
Example
student = "Ali"
age = 17
print(student)
print(age)Ali 17
In JavaScript you start with the word let:
Example
let student = "Ali";
let age = 17;
console.log(student);
console.log(age);Ali 17
The = sign does not mean "equals" like in maths. It means "put this value into this box".
Changing a Variable
The value in a variable can change. That is why it is called a *variable*.
Example
price = 300
print(price)
price = 350
print(price)300 350
Example
let price = 300;
console.log(price);
price = 350;
console.log(price);300 350
In JavaScript you use let only the first time. After that, you just use the name.
In JavaScript, use const instead of let for a value that should never change, for example const shopName = "Karachi Mart";. Trying to change a const gives an error.
Types of Values
Values come in different kinds, called data types. The most common ones are:
| Type | Python example | JavaScript example | Meaning |
|---|---|---|---|
| Text (string) | "Ayesha" | "Ayesha" | Letters, words, sentences |
| Whole number | 25 | 25 | Numbers without a decimal point |
| Decimal number | 99.5 | 99.5 | Numbers with a decimal point |
| True / False | True, False | true, false | A yes-or-no value (boolean) |
Notice that Python writes True with a capital T, while JavaScript writes true in lowercase.
Using Variables Together
Variables become useful when you combine them. Here a small shop in Karachi works out a bill:
Example
item = "Notebook"
price = 120
quantity = 3
total = price * quantity
print(item, "total: Rs", total)Notebook total: Rs 360
Example
let item = "Notebook";
let price = 120;
let quantity = 3;
let total = price * quantity;
console.log(item, "total: Rs", total);Notebook total: Rs 360
Both print() and console.log() can show several values at once if you separate them with commas. They put a space between each value.
Joining Text
You can join strings with +. In Python, a number must be turned into text first with str():
Example
name = "Ayesha"
marks = 88
print("Student: " + name + ", marks: " + str(marks))Student: Ayesha, marks: 88
Example
let name = "Ayesha";
let marks = 88;
console.log("Student: " + name + ", marks: " + marks);Student: Ayesha, marks: 88
JavaScript turns the number into text for you automatically.
Naming Rules
Good names make code easy to read. Follow these rules in both languages:
- Names can use letters, digits and the underscore
_ - A name cannot start with a digit (
2priceis not allowed) - No spaces: write
total_price(Python style) ortotalPrice(JavaScript style) - Names are case sensitive:
Ageandageare two different variables - Choose names that describe the value:
feeis better thanx
Exercise
Exercise
After these lines run, what is the value of score? score = 10, then score = score + 5
