简体   繁体   中英

What does 0; means in javascript

Looking at grunt-strip plugin to remove console.logs. I realized that it replaces console.log statements with 0; . Does 0; have any effect?

It doesn't have any effect, no. JS evaluates 0 and nothing picks up the result. The idea is that removing console.* (with a simple replace by nothing) might break code like this:

if(condition)
    console.log('');

functionCall();

Would become (with reformatting for emphasis...)

if(condition)
    functionCall();

So it's replaced by a dummy statement.

if(condition)
    0; // Does nothing

functionCall();

Also, code checking if the console is present will return false because 0 is falsy.

if(console) { // Not executed when replaced by if(0)
    // debugging action
}

How it's done

Right now, the logic consists of a simple replacement of your selected nodes with a falsy statement (0). This is proving to work in all reasonable situations and alleviates the need for complex rewrite logic.

At some later date that rewrite logic may be important, but the cost vs reward isn't there right now.

Source: https://github.com/jsoverson/grunt-strip

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM