2

In many javascript source code(mostly, config file), with code like

process.env.NODE_ENV = ~fs.readdirSync(configPath).map(function(file) {
    console.log(file);
    console.log(file.slice(0, -3));
    //The file is something like all.js. After use file.slice(0, -3) the output is like alljavascri
    return file.slice(0, -3);
}).indexOf(process.env.NODE_ENV) ? process.env.NODE_ENV : 'development';

My questions is, what does ~fs.readdirSync means? The ~ here doesn't look like xor.

wilbeibi
  • 3,403
  • 4
  • 25
  • 44

1 Answers1

1

~ is the bitwise NOT operator NOT. Every bit in x is inverted in ~x. For instance:

 x = 00011011
~x = 11100100

Alternatively, it is equivalent to doing XOR with 0xFFFFFFFF (all 1-bits):

     00011011
 XOR 11111111
     --------
   = 11100100 
Frxstrem
  • 38,761
  • 9
  • 79
  • 119
  • I checked javascript logical operators but didn't find it https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators. And, what's the meaning of the entire line of code? thank you – wilbeibi Jul 03 '14 at 22:24
  • It's not a logical operator, it's a bitwise operator. The difference is that logical operators work on the whole operand and returns either true or false, while bitwise operators work on each bit of the operands and return an integer. – Frxstrem Jul 03 '14 at 22:27