Hashbang Comments - The Third Way of Adding Comments in Javascript Code

javascript
Published on December 9, 2019

Use-Cases of this tutorial

  • Know about the third way of adding comments to Javascript code (other than single-line & multi-line comments).
  • Know about the hashbang comment syntax newly added in Javascript.

Hashbang comments is the third type of syntax for adding comments to Javascript code. This comment can specify the Javascript interpreter that is to be used to execute the code within a script file or a module.

Until lately, Javascript had 2 types of comment syntax — one for adding single line comments and one for adding multi-line comments.

// this is single line comment
/* this is 
a multi-line 
comment */

However recent updates to Javascript have introduced a third type of comment syntax. This is the hashbang comment syntax.

Hashbang Comments in LINUX Operating Systems

Hashbang / shebang are commonly used in LINIX operating systems. Text files with hashbang start with #! characters sequence, and they specify the interpreter that is used to execute the file. For example :

  • This will will execute the file using the Bourne shell.

    #!bin/sh
    
    // contents of the file
    
  • This will will execute the file using the Bash shell.

    #!bin/bash
    
    // contents of the file
    

Hashbang Comments in Javascript

The hashbang comment syntax has now been added in Javascript also. This can be added to the start of a script or module to specify the path of the Javascript interpreter that is to be used to execute the containing code. The comment can be contained only in a single line.

#!/path-to-v8/v8-shell

// Javascript code that will be executed by the V8 interpreter

This syntax is useful for Javascript executing on the server-side. Servers can have multiple Javascript interpreters, and using the hashbang syntax we can choose a specific interpreter to execute our Javascript code..

In this Tutorial