CSV stands for Comma-Separated Values. It is a plain text file that stores a table, like a spreadsheet.
It’s a handy way to prepare data for a website: you can fill a table in Excel, Google Sheets or Numbers, export it as .csv, and read it with JavaScript. No need to write JSON by hand.
Here is a sample file called projects.csv (download it):
the first line is the header: it gives a name to each column
Opened in a spreadsheet app, the same file looks like this:
title
category
year
color
Poster Series
Graphic design
2024
#ff6b6b
Type Specimen
Typography
2025
#4ecdc4
Zine
Book design
2025
#ffd93d
Portfolio Website
Web design
2026
#7c6ef7
Variable Font
Typography
2026
#a29bfe
Exporting a CSV from a spreadsheet
Google Sheets: File → Download → Comma-separated values (.csv)
Excel: File → Save As → CSV UTF-8 (.csv)
Numbers: File → Export To → CSV…
Excel in French
Excel set to French often uses ; instead of , to separate the columns. Open your .csv in VS Code to check which one your file uses.
Vanilla JS
JavaScript can’t read a CSV file directly: it only receives one long string of text. Our job is to turn that text into an array of objects, the shape we already know how to loop through:
We load the file with fetch, exactly like we do with an API. The only difference: a CSV is not JSON, so we use response.text() instead of response.json().
\n is the invisible “new line” character: it’s what you type when you press Enter.
Use a local server
If you open index.html by double-clicking it (the address starts with file:///), the browser blocks fetch and shows a CORS error in the console.
In VS Code, open your project with the Live Server extension instead (the address will start with http://127.0.0.1).
2. Split the text into lines
split(separator) cuts a string into an array, each time it finds the separator. Here, the separator is the new line "\n":
trim() removes spaces and empty lines at the start and end of the text. Most CSV files end with an empty line: without trim(), we would get an empty "" row at the end of the array.
A CSV file is only text, so every value we get is a string: "2024" and not 2024.
To do maths, compare or sort years, convert them with Number(). See numbers.
Writing title: values[0] by hand works well for a small file. If your CSV has many columns, you can use the names in headers as the keys of the object:
project[header] uses the bracket notation: when header is "title", it does the same as project.title. This version works with any CSV file, but every value stays a string.
Display the data on the page
Once we have our array of objects, we use the DOM to create one card per project.
In your project, the path would be "projects.csv". In the example below the file is hosted on this site, so the path is "/files/projects.csv".
const list = document.querySelector("#projects");fetch("/files/projects.csv") .then(response => response.text()) .then(text => { // 1. split the text into lines const lines = text.trim().split("\n"); // 2. remove the header const rows = lines.slice(1); // 3. one row = one project for (const row of rows) { const values = row.split(","); const project = { title: values[0], category: values[1], year: Number(values[2]), color: values[3], }; // 4. create a card for each project const li = document.createElement("li"); li.classList.add("project"); li.style.borderColor = project.color; li.innerHTML = ` <h3>${project.title}</h3> <p>${project.category} · ${project.year}</p> `; list.append(li); } }) .catch(error => { console.error(error); list.textContent = "Could not load the CSV file."; });
Try it: download projects.csv, add a line in it, and the page shows one more card.
Use the data
Now that projects is a regular array of objects, every array method works:
// only the typography projectsconst typography = projects.filter(project => project.category === "Typography");// the most recent projects firstprojects.sort((a, b) => b.year - a.year);
Opening index.html without a local server fetch is blocked on file:/// addresses. Use Live Server.
Forgetting .trim()
The empty line at the end of the file becomes an empty row, and the last object is full of undefined.
Spaces after the commas
With Zine, Book design, the category becomes " Book design" (with a space). Don’t put spaces around commas in your CSV, or clean each value with values[1].trim().
Forgetting Number() "2024" is a string. Convert numbers before comparing or sorting them.
Limits of split(”,”)
split(",") is perfect for simple files like ours, but it breaks in a few cases:
A value contains a comma. In CSV, such values are wrapped in quotes: "Paris, France". split(",") cuts it in two.
The file uses ;. Use split(";") instead (see Excel in French).
The file comes from Windows. Lines may end with \r\n instead of \n, leaving an invisible \r at the end of the last value. Use split(/\r?\n/) to handle both.
For real-world files, a library like Papa Parse handles all of these cases for you.
TLDR CSV boilerplate
Works with any simple CSV file. data is an array of objects, using the header names as keys.