fx Getting Started

JavaScript essentials for Sheets users

⏱ 25 min

What you'll learn

  • Variables, data types and functions
  • Arrays and objects — and why sheet data is an "array of arrays"
  • if, loops, and the array helpers you'll use every day

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.
  • const by default, let when it changes, === for comparing.
  • filter, map, reduce replace most loops.

Exercises

mediumPractice 1

Using the data array above, write a function that returns the name of the item with the highest total value (qty × price).

Quiz

In data[3][1], which sheet cell is that (if data starts at A1)? a) C2 b) B4 c) D2
B4
Which helper works like Excel's FILTER? a) map b) filter c) reduce
filter
JavaScript essentials for Sheets users · Google Apps Script | ExcelWalaa