Home>

There is the following description in the JS teaching materials that you are currently studying.

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

Semicolon (semicolon) on the 1st and 5th lines;) Is attached at the end. On the other hand, the third line is attached in the middle. What rules is this based on? I don't understand the rules, so I find it very difficult to remember. I didn't understand even if I googled, so I asked a question here. Thank you.

I feel that there is a semicolon at the end (that is, at the end) of the third line, but could you tell me a little more about what it means to be in the middle?

supa2022-02-13 09:07:24

Thank you for your comment. In all the cases I learned so far, the semicolon was attached after the parentheses ([], {}, (), etc.), so I expected to add it after the parentheses. Therefore, I expected it to follow the parentheses on the 4th line. As a general rule, I was convinced that I was taught to add a semicolon at the end of the line.

user513312022-02-13 09:07:24
  • Answer # 1

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

    return element +'san';

    console.log (result);

    Each of these is a "statement".
    Semicolon at the end of the sentence;Is attached.

    (JavaScript has this semicolon auto-insertion function, and in most cases it's okay if you don't add it, but there are exceptions, so I'm sure to add it)

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

    This is also one sentence, so it's actually the third line)You also need a semicolon after.
    However, even if it does not exist as described above, it will be automatically inserted, so no error will occur.

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

    Statements and Expressions · JavaScript Primer # jsprimer

    Thank you for telling me in an easy-to-understand manner. It must be attached to the end of the line. I understand that it is necessary for the 3rd line of the description (4th line of the question) that was originally shown. Also, thank you for providing the reference link. I will study.

    user513312022-02-13 09:07:24