-1

I recently learned about the hashbang syntax for writing comments in JavaScript, and need to know if it is or has the potential to be any different then just putting a // at the top of a file. Are there any conventions associated with the hashbang?

I am referring to usage in a browser environment only, not in a shell or NodeJS, etc.

I am asking because I am writing about JavaScript syntax and need to make sure it is 100% correct.

luek baja
  • 1,475
  • 8
  • 20
  • Wow, I am incorrect, shebangs are valid JavaScript. Will have to see where in ECMAScript sometime soon. As for your question, [here](https://stackoverflow.com/questions/10696222/make-node-js-support-the-shebang-for-javascript-files) is a post about it. – kelsny Sep 28 '22 at 23:07
  • 1
    [What exactly does "/usr/bin/env node" do at the beginning of node files?](https://stackoverflow.com/q/33509816) probably a better dupe target – SuperStormer Sep 28 '22 at 23:18
  • @SuperStormer I am referring to JS in the browser, rather than NodeJS – luek baja Sep 28 '22 at 23:26

2 Answers2

0

Hashbang has no meaning to JavaScript, as far as it's concerned it's just a comment. The only reason it exists is because Unix treats files beginning with #! specially when you try to run them as commands, and this can be used to run node.js automatically on a server.

This is meaningless in a browser, which loads scripts explicitly. So The hashbang has no special function on the client.

Barmar
  • 741,623
  • 53
  • 500
  • 612
0

No, #! doesn't have any JavaScript functionality, neither on the server nor on the client side. It's just a comment that does nothing. It has a functionality in executable files on some operating systems, but that has nothing to do with JavaScript - it's just to allow writing JavaScrit scripts as text files that make use of this operating system functionality.

On a related note, JavaScript does also use <!-- … and --> as comment syntax, which is equally useless in non-HTML context like in serverside javascript, but it's permitted for compatibility anyway.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375