Why 2 /// 2 - 2 in Javascript?

Does anyone know why 2 /// 2 is 2 in Javascript?

What is this behavior called? Is this documented somewhere? Thanks.

+8
javascript
source share
4 answers

The answer is simple: a one-line comment. The first two slashes start the comment, so the whole operator is just 2

+6
source share

It is called a comment. Everything that starts with // is a comment on one line.

Your code is essentially 2 , since the rest of the line is the comment " / 2 ".

+15
source share

// enter a single line comment in JavaScript

+10
source share

This is called a comment:

 2 /// 2 

It is equal to:

 2 

Because everything after and including // ignored (to the next line). JavaScript also has this awful feature in which it automatically inserts semicolons before newlines so that the code does not interrupt when you do something like this:

 var x = 2 /// 2 

BTW: Use a text editor with syntax highlighting.

+7
source share

All Articles