DEV Community

Sharavana
Sharavana

Posted on

Comments In JavaScript

Audience:

Anyone who wrote Comments in any language, except JavaScript.

For Impatient People like Me:

JavaScript has two types of Comments:

1.Single-line Comments:

//This is a comment.
console.log(`Hello World!`); // This is also a comment.
Enter fullscreen mode Exit fullscreen mode

2.Multi-line Comments:

/* This is 
also a comment
 spread on
many lines.
*/
Enter fullscreen mode Exit fullscreen mode

Why On Earth we need them?

To feel the need for Comments in Js, you need to pass through three stages:

Stage-1: Hello World:

I wrote hello world and other programs which are under-10-lines-programs.
(And felt "What are Comments?")

Stage-2: Guess the number:

Now I am writing 30-60 lines programs(like 'Guess the number').
(Feeling "I am a Pro, who can write code without Comments!"

Stage-3: Calculator:

When I start writing programs which are above 100 lines:
1)Continuously writing for a week.
2)Suddenly stuck with a problem.
3)Take a break.
4)And guess what? Booom!
Everything I just wrote is a mess for myself.
(Here, I will feel "A little bit of Comments should make sense!")


Buddy: Can you prove it!
Me: Sure!
Here is a piece of Rust code without Comments:

    let short_cut_path: &str = &format!(
        "/home/{}/.local/share/applications/{}.desktop",
        whoami::username(),
        desktop_entry.app_name
    );
    let short_cut_path = Path::new(&short_cut_path);

    let mut short_cut_file: fs::File = match fs::File::open(&short_cut_path) {
        Ok(file) => {
            println!(
                "A short-cut with name '{}' already exists!!",
                desktop_entry.app_name
            );
            panic!("Try any other name for your short-cut!");
        }
        Err(error) => match fs::File::create(&short_cut_path) {
            Ok(file) => file,

            Err(why) => panic!("Error!\n\nUnable to create the short-cut!"),
        },
    };

    match short_cut_file.write_all(short_cut_template.as_bytes()) {
        Err(why) => panic!("Error:\n  Unable to create the short-cut!"),
        Ok(_) => println!("Successfully created short-cut!\n\nWait for a moment..."),
    };

Enter fullscreen mode Exit fullscreen mode

Now, with Comments:

// Path to Desktop Short-cut in string format
    let short_cut_path: &str = &format!(
        "/home/{}/.local/share/applications/{}.desktop",
        whoami::username(),
        desktop_entry.app_name
    );
    // Creating Short-cut path by giving path-string to Path constructor 
    let short_cut_path = Path::new(&short_cut_path);

    //Opening the file as short_cut_file
    let mut short_cut_file: fs::File = match fs::File::open(&short_cut_path) {
        Ok(file) => {
            println!(
                "A short-cut with name '{}' already exists!!",
                desktop_entry.app_name
            );
            panic!("Try any other name for your short-cut!");
        }
        Err(error) => match fs::File::create(&short_cut_path) {
            Ok(file) => file,

            Err(why) => panic!("Error!\n\nUnable to create the short-cut!"),
        },
    };

    //Writing to the file
    match short_cut_file.write_all(short_cut_template.as_bytes()) {
        Err(why) => panic!("Error:\n  Unable to create the short-cut!"),
        Ok(_) => println!("Successfully created short-cut!\n\nWait for a moment..."),
    };

Enter fullscreen mode Exit fullscreen mode

Buddy, clearly you can see the difference.
With just Comments, now the code makes a little sense to even newbies to code.

Buddy:Wait! You are explaining importance of Comments in JavaScript with Rust code?
Me:Ah, yeah!
Buddy:Unbelievable!
Me: Thank you!! Thank you!!
Because, if you are able to get Rust code with just Comments,then Js code with Comments would be a cake!

Top comments (0)