admin管理员组

文章数量:1415119

I am new to JavaScript and webdevelopment and am currently doing a course with Codecademy. I am creating a random message app that has an array of objects and prints a random message on run.

I have created the array of objects as follows:

const quoteMessage = [
    {   title   : "It’s up to you.",
        meaning : "This one speaks for itself, really. No one is going to achieve your dream for you, and likewise, no one can stop you. It’s up to you.",
        aurthor : "Unknown.",
    },
    {   title   : "Failure is the opportunity to begin again more intelligently.",
        meaning : "For being only nine words long, this is an incredibly powerful, thought-provoking quote. It’s also incredibly accurate if you take the time to think about it. With every failure, we learn and grow, meaning that when we start over we’re almost guaranteed to do a better job. Failure isn’t a step back; it’s a step forward.",
        aurthor : "Henry Ford.",
    },
    {   title   : "The best revenge is massive success.",
        meaning : "It can be hard not to lash out at the people who tell you that you’re never going to succeed. However, if you let anger and hatred consume you, you’re never going to get anywhere and you’ll just prove them right. Rather than shout and swear, why not watch their faces as you mount that last step and reach the top?",
        aurthor : "Unknown.",
    }];

I have created the random selector as follows:

console.log(quoteMessage[Math.floor(Math.random()*(quoteMessage.length - 1))]);

I am copying and pasting this code directly into the console. When I run the code I get an error:

Uncaught SyntaxError: Identifier 'quoteMessage' has already been declared.

I don't understand why I am getting this error. What am I doing wrong?

I am new to JavaScript and webdevelopment and am currently doing a course with Codecademy. I am creating a random message app that has an array of objects and prints a random message on run.

I have created the array of objects as follows:

const quoteMessage = [
    {   title   : "It’s up to you.",
        meaning : "This one speaks for itself, really. No one is going to achieve your dream for you, and likewise, no one can stop you. It’s up to you.",
        aurthor : "Unknown.",
    },
    {   title   : "Failure is the opportunity to begin again more intelligently.",
        meaning : "For being only nine words long, this is an incredibly powerful, thought-provoking quote. It’s also incredibly accurate if you take the time to think about it. With every failure, we learn and grow, meaning that when we start over we’re almost guaranteed to do a better job. Failure isn’t a step back; it’s a step forward.",
        aurthor : "Henry Ford.",
    },
    {   title   : "The best revenge is massive success.",
        meaning : "It can be hard not to lash out at the people who tell you that you’re never going to succeed. However, if you let anger and hatred consume you, you’re never going to get anywhere and you’ll just prove them right. Rather than shout and swear, why not watch their faces as you mount that last step and reach the top?",
        aurthor : "Unknown.",
    }];

I have created the random selector as follows:

console.log(quoteMessage[Math.floor(Math.random()*(quoteMessage.length - 1))]);

I am copying and pasting this code directly into the console. When I run the code I get an error:

Uncaught SyntaxError: Identifier 'quoteMessage' has already been declared.

I don't understand why I am getting this error. What am I doing wrong?

Share Improve this question edited Jun 25, 2021 at 10:08 JessicaRyan asked Jun 25, 2021 at 10:06 JessicaRyanJessicaRyan 1711 gold badge4 silver badges15 bronze badges 3
  • Please fix the formatting of your question -> How do I format my posts using Markdown or HTML? – Andreas Commented Jun 25, 2021 at 10:07
  • You cannot re-declare something with const. If you're copy and pasting in the console, then you don't need the const declaration. – VLAZ Commented Jun 25, 2021 at 10:09
  • Anything you execute in the console happens in the global scope. If you execute the same script again the variables are already declared - unless you previously have "reset" that (global) scope -> A way to reset Chrome DevTools console's context – Andreas Commented Jun 25, 2021 at 10:14
Add a ment  | 

2 Answers 2

Reset to default 2

Since you say you're pasting directly into the console, you can't redeclare a const variable.

Either use a let or var, or put your code in a <script> in an HTML file you'd reload.

simply don't repeat your declarations "let, const or var" in your new variable code if you have already used such declaration in the same variable somewhere else in your code. Example: let ageJohn = 25; Now if you want to write ageJohn again in the code don't write let, just write ageJohn = 25; I hope this was helpful.

本文标签: javascriptUncaught SyntaxError Identifier 39x39 has already been declaredStack Overflow