Sknoe / Notes
JavaScript Before React: A Step-by-Step Guide to the Fundamentals
Build confidence with values, objects, functions, callbacks, and array methods before combining them in React.
You open a React example and find this:
products.map((product) => <ProductCard key={product.id} product={product} />)
It is easy to look at that line and think, “I should already know what all of this means.” But that one line combines an array, an object, a function, a callback, map(), JSX, and a React component. If one piece is unfamiliar, the whole line can feel like a new language.
You do not need to know all of JavaScript before starting React( although I do recommend it), but you do need a steady way to unpack the patterns React uses. I postponed learning react many time… convincing myself that there is more JS to “learn” for me to actually get “React”. But that was not the case. So this guide builds them one Javascript concept at a time - starting with plain data and ending with the kind of list expression you will meet in a component. Turns out, that is all I needed to get started! Each step uses the previous one, so you can see what changed and why. Come Join me!
What should you understand before React?
React code is still JavaScript. Before we add React, we will work with values, objects, arrays, functions, and conditions. Then we will put a function inside another function call, learn why map() calls it, and use the returned array to describe a list.
We will use product data throughout as an example. You do not need to know React or JSX to follow the first examples.
1. Start with values and variables
const productName = 'Trail Backpack';const price = 64;const inStock = true;
const gives a name to a value. Here, productName refers to a string, price to a number, and inStock to a boolean. We will use the names to make later examples easier to read.
The value 64 is not the same kind of value as '64': one is a number and one is text. That matters when you do calculations or compare data.
2. Group related data in objects and arrays
const products = [{ id: 'sku-101', name: 'Trail Backpack', price: 64, inStock: true },{ id: 'sku-102', name: 'Rain Shell', price: 89, inStock: false },];
The square brackets create the array. Each set of curly braces creates one object. The object properties describe one product; the array contains both products.
console.log(products[0].name); // 'Trail Backpack'
products[0] means the first item in the array—JavaScript starts counting at zero. .name reads that item's name property. React often receives data in exactly these shapes: an object for one thing, or an array when there are many.
3. Use a function to turn input into output
function formatPrice(price) {return '$' + price;}const label = formatPrice(64);console.log(label); // '$64'
price is a parameter: the name the function uses for its input. When we call formatPrice(64), the value 64 is passed in. return sends a result back to the place that called the function, so label receives the formatted text.
Returning a value is different from printing one. console.log() displays a value for us to inspect; it does not hand that value back to the caller. A function can log something and still return nothing.
You will also see the same function written as an arrow function:
const formatPrice = (price) => {return '$' + price;};
The arrow, =>, separates the function's input from its body. If the function body is one expression, parentheses let JavaScript return that expression automatically:
const getName = (product) => product.name;
This short form still returns a value. Keep that in mind; it will matter when map() uses a function to make each item in a new array.
4. Read a condition before choosing an item
const isAvailable = products[0].inStock === true;console.log(isAvailable); // true
The === operator checks whether the values on both sides are equal. A condition can also be used to choose between two values:
const stockMessage = products[0].inStock? 'Available': 'Currently unavailable';
You will see this form in JSX later, but it is ordinary JavaScript.
5. Pass a function to another function
function describeProduct(product, makeLabel) {return makeLabel(product);}const label = describeProduct(products[0], (product) => product.name);console.log(label); // 'Trail Backpack'
Here, the arrow function (product) => product.name is passed into describeProduct. Inside that function, makeLabel refers to the callback, and makeLabel(product) calls it with a product. The function receiving a callback decides when to call it and what value to pass.
This answers an important question to ask when reading unfamiliar code: who calls this function, and what does it pass in? With a callback, you provide the instructions; the receiving function runs them.
6. Use map() to make a new array
Suppose we want a list of product names. We could start with a loop:
const names = [];for (const product of products) {names.push(product.name);}
For each product, the loop reads its name and adds it to names. map() does this kind of one-item-to-one-result transformation:
const names = products.map((product) => product.name);console.log(names); // ['Trail Backpack', 'Rain Shell']
Read this in parts: products is the input array; the function (product) => product.name is the callback; map() calls the callback once for each product; each returned name goes into a new array; names refers to that new array.
The callback must return the value you want in the new array. With an arrow function followed by one expression, the return is implicit:
const names = products.map((product) => product.name);
With curly braces around the function body, write return yourself:
const names = products.map((product) => {return product.name;});
This version does not return a name:
const names = products.map((product) => {product.name;});
Because there is no return, the callback returns undefined each time. The result is [undefined, undefined], not an array of names. When a map() result looks wrong, check what the callback returns.
map() returns a new array and leaves the original array in place. It is for transforming every item—not for changing the original list or choosing only some items.
7. Choose between map(), filter(), and find()
These methods all accept callbacks, but they answer different questions: map() asks what new value should I make for every item? filter() asks which items should I keep? find() asks what is the first item that matches?
const availableProducts = products.filter((product) => product.inStock);const firstAvailableProduct = products.find((product) => product.inStock);const productNames = products.map((product) => product.name);
availableProducts is an array. firstAvailableProduct is one product object, or undefined if none match. productNames is an array with one name for every product.
You can combine these steps when each name should come from an available product:
const availableNames = products.filter((product) => product.inStock).map((product) => product.name);
Read it from left to right: start with the products, keep the available ones, then turn each remaining product into its name. Each method returns a value the next method can use.
8. Read shorter object syntax
const getProductName = ({ name }) => name;
This is object destructuring. The function still receives a product object, but the parameter pattern reads its name property into a local variable called name. It is shorthand for writing (product) => product.name.
Destructuring is common in React code, but it is not required. If the braces are distracting, write the longer version first and shorten it once you understand what it does.
When you want to make a changed object rather than change the original, object spread can copy its properties into a new one:
const updatedProduct = { ...products[0], inStock: false };
updatedProduct is a new object. The original product remains unchanged. This idea matters in React because props and state should be treated as read-only; we will use it when learning how React updates a screen.
This is a shallow copy: if a property contains another object or array, that nested value is still shared. Copy each level you change. The linked guide walks through a nested example.
9. Recognize imports and exports
// price.jsexport function formatPrice(price) {return '$' + price;}// ProductCard.jsimport { formatPrice } from './price.js';
The braces in import { formatPrice } mean this is a named export, so the imported name matches the exported name. You do not need to organize a large app yet. Just know that when you see an import at the top of a React file, it brings a value or function in from another module.
Put the pieces together
products.filter((product) => product.inStock).map((product) => (<ProductCard key={product.id} product={product} />));
First, filter() keeps the products whose inStock value is true. Then map() calls its callback once for each remaining product. The callback returns a JSX description of a ProductCard. JSX is a JavaScript syntax extension used to describe UI; <ProductCard ... /> is not an HTML tag.
React will teach you what a component is, what props do, why list items need keys, and how JSX becomes a screen. The JavaScript groundwork is smaller: you can read the array, follow the callback, and understand what each step returns.
Check that the foundations are there
Before moving on, try to answer these without looking back: What does products[0].name read? In products.map((product) => product.name), who calls the arrow function? What does map() return, and what must its callback do? How is filter() different from find()? Why does the example below produce an array of undefined values?
const names = products.map((product) => {product.name;});
Answers: It reads the first product's name. map() calls the arrow function once for each item. map() returns a new array, and its callback must return each new value. filter() returns an array of all matches; find() returns the first match or undefined. The callback has a block body but no return.
If you can explain those steps in your own words, you are ready to start React. You do not need to memorize every array method first. You can learn React concepts while strengthening the JavaScript they use.
What can wait until you meet it?
You do not need to master promises, async/await, closures, or every JavaScript feature before your first component. Those are useful, and they appear in real applications, but you can learn them when the next example needs them. In particular, data loading in a framework such as Storefront Next has its own server-side patterns; it is not a reason to postpone learning components.
Next, start learning React. When you reach a line you cannot explain, slow down and name the pieces: what is the data, who calls the function, what comes back, and where does that result go?
Did this click?
Tap the bulb if this explanation helped.
Further reading
JavaScript Callbacks Explained: How They Work and Why They Matter