JS-API: Fix bug related to non-existent item.
JS-API: code sucks a little bit less.