Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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.

share|improve this question
2  
How could you wonder if a cyphered code can be preferred to the most simple ’if (x != null )’ ??? –  GameAlchemist 10 hours ago
    
@GameAlchemist - Please see the additional information I posted. –  PM 77-1 9 hours ago

2 Answers 2

Can you do this?

Yes, you can.

When to do this?

Everywhere, except collaboration purposes.

Why?

(document.getElementById(arr[i].id) || 0).value = arr[i].value;

and

(document.getElementById(arr[i].id) || Object).value = arr[i].value;

tells nothings about what it does when you read this first time, because there are two logical parts:

  • retriving element and replacing null with default value
  • setting attribute value

You can write long one line code only if it is separate logic part.

BTW: I'd wrote something like this

function process(arr) {
    arr.forEach(function(item){
        var elem = document.getElementById(item.id) || 0;
        elem.value = item.value;
    });
}

Because programming language is tools to express your thoughts.

share|improve this answer
(document.getElementById(arr[i].id) || 0).value = arr[i].value;

In this code there is a pointless assignment when the element is not found. In this case, the 0 gets wrapped into a temporary object, and the assignment will work, but it will be pointless, it's result will be thrown away. This assignment is merely a side effect of the shortcut, it has no purpose of its own.

Every operation in a program should be intentional, purposeful, and this shortcut violates that. As such, I consider this a dirty hack. Don't use it.


When in doubt, go for the more intuitive and readable solution. The first example in your post is slightly longer, but it's simple and clear: there's no question about what will happen.

The second way, although shorter, may not be obvious for everyone.

Keep in mind that code is read far more times than it’s written. It's better to favor a technique that's clear and easy to understand to something "clever" but potentially confusing / obscure.


Additional excellent recommendation by @GameAlchemist, verbatim:

1) for optimisation purposes, the if is faster. and more importantly 2) a piece of code should be as much self-documenting as possible. Even if (element) is better written if (element != null) (which makes obvious the defensive check against getElementById returning null)

share|improve this answer
    
Why do you consider this hack dirty? Do you believe that future ECMA standards or implementations will make it invalid? –  PM 77-1 8 hours ago
2  
I consider it dirty because it uses a pointless assignment. The pointless assignment, although harmless, it's not intended, it's just a side-effect of the shortcut. Every operation in a program should be intentional, purposeful, and this shortcut violates that. –  janos 8 hours ago
2  
I would add that 1) for optimisation purposes, the ‘if‘ is faster. and more importantly 2) a piece of code should be as much self-documenting as possible. Even ‘if (element)‘ is better written ‘if (element != null)‘ (which makes obvious the defensive check against getElementById returning null) . –  GameAlchemist 5 hours ago
    
Thanks @GameAlchemist, excellent points! (added to my post) –  janos 5 hours ago
    
Good answer, but I generally prefer if (element) as a compact & popular Javascript idiom. Not only is it shorter, it guards against undefined as well. –  Thomas W 57 mins ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.