Understanding the Location of a Semicolon in JavaScript

Asked 1 years ago, Updated 1 years ago, 26 views

The following is a description of the JS material that I am currently learning.

const user=['Taro Tanaka', 'Ichiro Sato', 'Jiro Yamada'];
constresult=user.map(element=>{
  return element + 'Mr';
})
console.log(result);

The first and fifth lines have semicolons (;) at the end.On the other hand, the third line is attached in the middle.What kind of rule is this based on?I don't know the rules, so I find it very difficult to remember.I didn't really understand even if I googled, so I asked you a question.Thank you for your cooperation.

javascript

2022-09-30 19:09

1 Answers

const user=['Taro Tanaka', 'Ichiro Sato', 'Jiro Yamada'];

return element+';

console.log(result);

These are all one 文statement. br
The sentence ends with a semicolon ;.

(JavaScript has this semicolon auto-insert function, which is fine in most cases without it, but there are exceptions, so I always do.)

const result=user.map(element=>{
return element+';
})

This is also a single sentence, so you really need a semicolon after the third line ).
However, it will be automatically inserted even if it is not as mentioned above, so there will be no error.

I think the following JavaScript syntax is easy for beginners to understand.

Statements and expressions ·JavaScript Primer#jsprimer


2022-09-30 19:09

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.