본문으로 건너뛰기

Comments

Comments

Comments are used by developers to explain the purpose, workings, or changes in the code, or to temporarily disable certain parts of it.


Types of JavaScript Comments

  1. Single-line Comment

A single-line comment starts with // and extends to the end of the line.

// This is a single-line comment
console.log('Hello, World!'); // This message will be printed on the console

  1. Multi-line Comment

A multi-line comment begins with /* and ends with */. This style of comment is used for writing comments that span multiple lines.

/*
This is a multi-line comment.
The contents within the comment are not executed.
*/
console.log('Hello, World!');

Uses of JavaScript Comments

  1. Code Explanation: You can use comments to explain the functionality, principles of operation, and the purpose of variables or functions.
// Assigns the value 10 to variable x.
let x = 10;

  1. Code Disabling: If you need to trace a bug, debug, or temporarily disable a specific piece of code, you can make use of comments.
// console.log("This won't run");

  1. Marking TODO or FIXME: By adding comments to code that is under development or needs to be revised, you can easily locate and make necessary changes later on.
// TODO: Implementation of additional features needed here.

Cautions:

  • Comments do not affect code execution, but overusing them can decrease the readability of the code. It is best to use comments appropriately only where necessary.

  • It's advisable not to include sensitive information or unnecessary personal details in comments.

다음 내용이 궁금하다면?

월 12,500원 PLUS 멤버십 가입 or 강의를 등록해 주세요!