JavaScript · Programming

What does “!--” do in JavaScript?

Sahedi Khan Internet Marketing Executive & Web Developer

October 5th, 2016

For me right now this is a wonderful way to confuse the next person that is maintaining the code… Help please!

David Rowell CEO & Founder at LifeLinker Inc

October 5th, 2016

At the risk of sounding curmudgeonly, this is Founder Dating.

A technical low-level question about the syntax of a specific programming language has no place on the Founder Dating site. Surely it should be on Stack Overflow or any of the dozens of sites devoted to assisting with programming.

Garet Claborn CEO, Original Author at Approach Corporation

October 5th, 2016

1. !-- May be related to HTML comments. 
// <!--


// -->

If the code was corrupted somehow you could be left with !--

2. Is it being used as an operator, in a string, what? Could simply be an unenclosed string.

3. It's possible this is a typo of !== which means, a does not equal to the typed value of b.

Fabio Nagao Owner, Become Evolved

October 5th, 2016

When applied in an integer decrementing variable (otherwise no reason for -- unary operator), !--x is false until x === 0 because of the falsy nature of 0.
You are 100% right when you are saying it's just a way to confuse the next person maintaining the system, its just a hipster way of asking x === 0 ?. So:

!2 = false; !1 = false; !0 = true.

Hope it helps.

Sheldon Poon Technical Director, Drive Marketing

October 5th, 2016

can you give us the context in which the !-- was used? <!-- means 'begin comment' in html

Charles Bastian Experienced Fixer

October 5th, 2016

I've never seen that operator used in JavaScript before.  Are you sure it is not supposed to be !==?  That is a comparison operator that means not equal to the value and the type.  -- is a decrement operator.  ! usually just means not.

There may be a JavaScript framework that uses that to increment, maybe?  Can you share a small code snippet that is using that operator?  That could help clear it up.

Brandon S UX Designer at REC1 Software

October 5th, 2016

It doesn't do anything unless you're using it as a string "!--" or if you want to add a less than sign before to create a comment. Javascript, along with other programming languages such as php have commenting syntax which works as follows:

<script type="text/javascript">
// This is a single line JavaScript comment

document.write("I have comments in my JavaScript code!");
//document.write("You can't see this!");

Dan Dascalescu Developer Advocate at Google

October 5th, 2016

What David said. This question belongs on the JavaScript StackOverflow chat.