Concept
1. Variables
const gstRate = 0.03; // const: value won't change
let total = 0; // let: value can change
total = total + 500;
Use const by default, let when the value must change. Avoid the old var.
2. Data types
| Type | Example | Sheet equivalent |
|---|---|---|
| Number | 1250.5 |
A numeric cell |
| String | 'Ring' |
A text cell |
| Boolean | true / false |
A checkbox |
| Date | new Date() |
A date cell |
| Empty | '' |
A blank cell (Sheets returns '', not null) |
Template literals make text easy:
const name = 'Rahul';
const amount = 4500;
const msg = `Dear ${name}, your bill is ₹${amount}.`;
3. Functions
A function is a reusable block of steps — like a formula you write yourself.
function addGst(amount, rate) {
return amount + amount * rate;
}
const finalAmount = addGst(10000, 0.03); // 10300
Arrow-function shorthand (you'll see it in examples):
const addGstShort = (amount, rate) => amount * (1 + rate);
4. Arrays — and how a sheet looks in code
An array is an ordered list:
const items = ['Ring', 'Chain', 'Bangle'];
console.log(items[0]); // 'Ring' (counting starts at 0!)
console.log(items.length); // 3
When you read a range from a sheet, you get an array of rows, and each row is an array of cells:
// Range A1:C3 comes back as:
const data = [
['Item', 'Qty', 'Price'], // row 1 → data[0]
['Ring', 2, 15000], // row 2 → data[1]
['Chain', 1, 42000] // row 3 → data[2]
];
console.log(data[1][0]); // 'Ring' → row 2, column A
console.log(data[2][2]); // 42000 → row 3, column C
Watch out: sheet rows and columns start at 1, arrays start at 0. data[1][0] is cell A2.
5. Objects
An object groups named values — like one row with headings:
const order = { item: 'Ring', qty: 2, price: 15000 };
console.log(order.item); // 'Ring'
console.log(order.qty * order.price); // 30000
6. Conditions
if (qty <= reorderLevel) {
console.log('Reorder needed');
} else if (qty <= reorderLevel * 2) {
console.log('Running low');
} else {
console.log('Stock OK');
}
Comparison: === equal, !== not equal, >, <, >=, <=. Combine with && (and), || (or). Always use ===, not ==.
7. Loops
for (let i = 1; i < data.length; i++) { // start at 1 to skip the header row
const row = data[i];
console.log(row[0] + ' → ' + row[1] * row[2]);
}
// Shorter, when you don't need the index:
for (const row of data.slice(1)) {
console.log(row[0]);
}
8. Array helpers you'll use constantly
const rows = data.slice(1); // drop header
const expensive = rows.filter(r => r[2] > 20000); // like FILTER()
const names = rows.map(r => r[0]); // pick one column
const totalValue = rows.reduce((sum, r) => sum + r[1] * r[2], 0); // like SUMPRODUCT()
Key points
- Sheet data = array of rows;
data[row][col], both starting at 0. constby default,letwhen it changes,===for comparing.filter,map,reducereplace most loops.