Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "!--" do in JavaScript?

I have this piece of code (taken from this question):

var walk = function(dir, done) {     var results = [];      fs.readdir(dir, function(err, list) {         if (err)             return done(err);          var pending = list.length;          if (!pending)              return done(null, results);          list.forEach(function(file) {             file = path.resolve(dir, file);             fs.stat(file, function(err, stat) {                 if (stat && stat.isDirectory()) {                     walk(file, function(err, res) {                         results = results.concat(res);                          if (!--pending)                             done(null, results);                     });                 } else {                     results.push(file);                      if (!--pending)                          done(null, results);                 }             });         });     }); }; 

I'm trying to follow it, and I think I understand everything except for near the end where it says !--pending. In this context, what does that command do?

Edit: I appreciate all the further comments, but the question has been answered many times. Thanks anyway!

like image 506
Kieran E Avatar asked Dec 16 '15 22:12

Kieran E


People also ask

What does != Mean in JavaScript?

The inequality operator ( != ) checks whether its two operands are not equal, returning a Boolean result. Unlike the strict inequality operator, it attempts to convert and compare operands that are of different types.

What does '!' Mean in JavaScript?

symbol is used to indicate whether the expression defined is false or not. For example, !( 5==4) would return true , since 5 is not equal to 4. The equivalent in English would be not .

What is $$ in JavaScript?

$ and $$ are valid variable names in JavaScript, they have no special meaning. Usually they set their value to library instances, in your example if you check the closure call, at the end of the file you'll see that $ is jQuery in this case if it is defined and $$ is cytoscape.


2 Answers

! inverts a value, and gives you the opposite boolean:

!true == false !false == true !1 == false !0 == true 

--[value] subtracts one (1) from a number, and then returns that number to be worked with:

var a = 1, b = 2; --a == 0 --b == 1 

So, !--pending subtracts one from pending, and then returns the opposite of its truthy/falsy value (whether or not it's 0).

pending = 2; !--pending == false  pending = 1; !--pending == true pending = 0; !--pending == false 

And yes, follow the ProTip. This may be a common idiom in other programming languages, but for most declarative JavaScript programming this looks quite alien.

like image 67
TbWill4321 Avatar answered Sep 22 '22 08:09

TbWill4321


That's not a special operator, it's 2 standard operators one after the other:

  1. A prefix decrement (--)
  2. A logical not (!)

This causes pending to be decremented and then tested to see if it's zero.

like image 41
Amit Avatar answered Sep 19 '22 08:09

Amit