Oracle NVL equivalent function in javascript / jQuery

Just wondering if there is an equivalent to the Oracle NVL function in javascript / jQuery - if so, you really will like an example of how this works.

Thanks.

+7
source share
4 answers

The ternary operator is commonly used here.

For example, if you create a dynamic action in Apex, you can do something like this:

( $v("P1_VAL1") ? $v("P1_VAL1") : $v("P1_VAL2") ) 

This will return the value of P1_VAL1 if it is not empty, otherwise it will return the value of P1_VAL2 .

+8
source
 function nvl(value1,value2) { if (value1 == null) return value2; return value1; } 
+2
source

In Javascript, this can actually be handled by the || which returns the first "valid" value.

 var a = null; var b = "valid value"; var c = a || b; // c == "valid value" 

Just keep in mind that the β€œfalse” values ​​are not only null , but also, for example, the empty string '' , number 0 and the boolean false . Therefore, you must be sure that you either consider those that have the same value as null , or your variables cannot take these values, because in these cases you will also get a second value:

 var a = ""; var b = "valid value"; var c = a || b; // c == "valid value" 
+2
source

Here is my solution for what it costs

 var ignoreNaN = false; function nullButNot0(val) { if (typeof val == 'number' && isNaN(val)) { if (!ignoreNaN) throw Error('NaN passed to nullButNot0()'); return true; } if (typeof val == 'object') { try { if (val.constructor === Array) return false; } catch (e) { } } return (!val && val !== 0); } function nvl(s, defVal) { // WARNING: not guaranteed to work with blank arrays if (!nullButNot0(s)) return s; if (!defVal) defVal = ''; return defVal; } 
0
source

All Articles