There is Uncaught SyntaxError: Unexpected identifier when defining a variable in code block

Viewed 33

I have this error when I define a const variable in a code block:

Uncaught SyntaxError: Unexpected identifier

I have already checked if I missed a extra comma, colon, parenthesis, quote or bracket, but still I get the error after I replaced the comma into curly brackets.

It works when I remove the variables and replace the a in Math.pow in 10. So what is going wrong with the const variables?

const multipleCircles = [{
  calc1: {
    const a = 10;
    area: Math.PI * Math.pow(a, 2);
  },
  calc2: {
    const a = 100;
    area: Math.PI * Math.pow(a, 2);
  }
}];
console.log(multipleCircles);

1 Answers

It is not clear what you are trying but the closest with the least change to your code is this

const multipleCircles = [{
  calc1: () => {
    const a = 10;
    return Math.PI * Math.pow(a, 2);
  },
  calc2: () => {
    const a = 100;
    return Math.PI * Math.pow(a, 2);
  }
}];
console.log(multipleCircles[0].calc1());
console.log(multipleCircles[0].calc2());

Related