In Node, process.env
property gives you access to the user environmental variables. Opposite to what you could think, these variables are always treated as strings, no matter the nature of your environment or the way you defined them.
Misunderstanding of that said above can give you unpredictible behaviours, as that shown in the next code snippet regarding the usage of a presumably boolean env variable:
process.env.FALSY = false;
let evaluation = process.env.FALSY
? "This is imposible"
: "This is the expected result";
// Since process.env.FALSY is returned as string,
// and the bool value of a non empty string is true
// it is evaluated not as falsy but as truthy
The problem is not just related to boolean values, but also to numeric values, as shown the next code snippet:
process.env.ONE = 1;
let mustBeSix = process.env.ONE + 5;
// Though you could expect mustBeSix being 6
// its result is the string concatenation
// of '1' and '5' ie, '15'
let forSureIsTrue = mustBeSix < 10;
// Though you could expect the previous expression
// being truthy, the fact is that '15' is converted to
// number for comparison, so it's evaluated as falsy
Comentarios
Publicar un comentario