Can someone explain how this code works in detail. Taken from Eloquent JavaScript

Can someone explain how this code works in detailed terms. This is taken from the book Eloquent JavaScript from parsing an INI file.

function parseINI(string) {
  // Start with an object to hold the top-level fields
  let result = {};
  let section = result;
  string.split(/\r?\n/).forEach(line => {
    let match;
    if (match = line.match(/^(\w+)=(.*)$/)) {
      section[match[1]] = match[2];
    } else if (match = line.match(/^\[(.*)\]$/)) {
      section = result[match[1]] = {};
    } else if (!/^\s*(;.*)?$/.test(line)) {
      throw new Error("Line '" + line + "' is not valid.");
  return result;

// → {name: "Vasilis", address: {city: "Tessaloniki"}}

Thanks in advance!


Are you familiar with how to read regular expressions (regex)?
It’s too much to explain them in detail but they are used for pattern matching. It may seem daunting at first but once you get the gist of it you can learn to read basic ones fairly well.

Here’s a small reference for them:

If you are not interested in learning about them, then just put them into an online regex translator to see what they do.

I am very much familiar with regex and actually I love them. So I only want to know that object parts…

I’d recommend to play around with all of the methods on objects you’re not familiar with (split(), forEach()) etc.

MDN offers great documentation with interactive examples if you’re short of ideas.

What does this part test for, comments??

Ok I found it myself…