What does "!--" do in JavaScript?

Asked 2 years ago, Updated 2 years ago, 21 views

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 saw a code like this on the Internet, and while interpreting the code, I saw a line called !--pending and asked because it is a phrase that I have never seen in my knowledge. Please tell me what code !--- is and what it does.

javascript

2022-09-22 22:08

1 Answers

The !-- operator is a combination of ! and --. I just put it on. First of all, the operator is an operator that invert the value.

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

In this way, make it true if it's true or true if it's false.

And -- an operator is an operator that reduces the value by one.

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

Like this.

For reference, in the operator !, make a number 0 if it is not zero, and make it 1 if it is zero.

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

Like this.


2022-09-22 22:08

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.