简体   繁体   中英

Can't find the cause of Uncaught SyntaxError: Invalid or unexpected token on an array

I know there are a lot of questions like this but it seems like a really basic problem so it doesn't look like anyone else has this issue. the line its referencing is

var stick = [‘stick’, stickDamage];

and it reports uncaught syntax error when it gets there and it can't continue on. am I writing the array wrong?

You are using smart quotes ( '' ) instead of straight quotes ( '' ).

Use a basic text editor that doesn't apply formatting to the text (there are numerous free web editors out there, NotePad++ is a good one) and save your code with UTF-8 encoding.

On a different note, it seems like you may be trying to create an associative array (where the string stick is associated with the data in the stickDamage variable). JavaScript doesn't use arrays for this kind of associative data structure. Instead, it uses objects, that have keys (which are always strings and because of that, you don't even need to put quotes around the key name) and values.

In that case, you'd want this instead of an array:

 const stickDamage = 99; const punchDamage = 59; const cutDamage = 79; var player = { stick: stickDamage, punch: punchDamage, cut: cutDamage }; console.log(player.stick, player.punch, player.cut); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM