jeudi 17 janvier 2019

Different ways in checking if a number is greater than zero (0)

What are the differences between using condition, coercion and Boolean conversion in checking a number type for greater than zero (0)?

Each will give the same output (Given that we also treat undefined and null as zeros):

var zero = 0;
var three = 3;
var isGreaterThanZero;

// condition
isGreaterThanZero= zero > 0 ? true : false; // isGreaterThanZero= false
isGreaterThanZero= three > 0 ? true : false; // isGreaterThanZero= true

// coercion
isGreaterThanZero= zero ? true : false; // isGreaterThanZero= false
isGreaterThanZero= three ? true : false; // isGreaterThanZero= true

// Boolean conversion
isGreaterThanZero= Boolean(zero); // isGreaterThanZero= false
isGreaterThanZero= Boolean(three); // isGreaterThanZero= true

What is the difference between these operations? (In terms of speed/performance, principles, practices, etc.)

Aucun commentaire:

Enregistrer un commentaire