I will ask my question using the following example: HTML <div>    <input id=ABC placeholder=abc></input>    <textarea id=XYZ placeholder=xyz></textarea></div>  JS Code var valArray = [{id: ABC, value: INPUT}, {id: UNKNOWN, value: }, {id: XYZ, value: TEXTAREA}]function process(arr) {    for(var i=0; i<arr.length; i++) {        var elem = document.getElementById(arr[i].id);        if (elem != null) {            elem.value = arr[i].value;        }    }   }process(valArray);  It works fine, but now I want to do it without an explicit if statement.How about the following code? Is it a proper JavaScript way to handle such situations? function process(arr) {    for(var i=0; i<arr.length; i++) {        (document.getElementById(arr[i].id) || 0).value = arr[i].value;    }   }From Programming JavaScript Applications by Eric Elliott- Chapter 3. Objects: Even primitive types get the object treatment when you refer to them  with the property access notations. They get automatically wrapped  with an object so that you can call their prototype methods.Primitive types behave like objects when you use the property access  notations, but you can't assign new properties to them. Primitives get  wrapped with an object temporarily, and then that object is  immediately thrown away. Any attempt to assign values to properties  will seem to succeed, but subsequent attempts to access that new  property will fail.In general I saw a lot of JavaScript code where || <default or alternative value> construct was used to protect against null.So I thought, that the above described behavior would make primitives ideal candidates for throw-away objects.If this is so convoluted, what about using Object:        (document.getElementById(arr[i].id) || Object).value = arr[i].value;That was my original idea but it seems to pollute the environment.
Writing null-safe code to set form values
javascript;error handling;null
I wondered why one could even consider testing against null like this, then i found the 'trick' :   You are using this pattern only once.  Now in a real-world example where you have to test several objects, the mess effect will raise at an incredible rate.  Imagine the not so complex app init : initialize all controls -if found- with the value of another control -if found- otherwise with a default value.  var initMap = [ { tgt : 'display1', src : 'input1', dft : '0'     },                 { tgt : 'display2', src : 'inputB', dft : 'city'  }                /* ... */              ];var $ = document.getElementById.bind(document);function initControls(initMap) {    for (var i=0; i<initMap.length; i++) {        var thisInitData = initMap[i];        var tgtElement   = $(thisInitData.tgt);        var srcElement   = $(thisInitData.src);        var dft          = thisInitData.dft;        (tgtElement || 0).value = ( ( srcElement || 0 ).value || default );  // ?? wth ?    }        }Now i call the last line cryptic programming :)   I consider the following code much clearer about the intent : if there's a target, then either assign this or that value to the element depending on source being null.  function initControls(initMap) {    for (var i=0; i<initMap.length; i++) {        var thisInitData   = initMap[i];        var tgtElement     = $(thisInitData.tgt);        if (tgtElement !== null) {            var srcElement = $(thisInitData.src);            tgtElement.value = (srcElement !== null) ? srcElement.value                                                      : thisInitData.dft ;        }    }        }And if you are still not convinced, build an example with 3 or more tests.