1

This line of javascript is in the Magento base code in app/design/frontend/base/default/template/persistent/checkout/onepage/billing.phtml:

        $('billing-address-select') && billing.newAddress(!$('billing-address-select').value);

Here is the line in context:

    <script type="text/javascript">
//<![CDATA[
var billing = new Billing('co-billing-form', 
                          '<?php echo $this->getUrl('checkout/onepage/getAddress') ?>address/', 
                          '<?php echo $this->getUrl('checkout/onepage/saveBilling') ?>');
var billingForm = new VarienForm('co-billing-form');

/*billingForm.setElementsRelation('billing:country_id', 
                                  'billing:region', 
                                  '<?php echo $this->getUrl('directory/json/childRegion') ?>', 
                                  '<?php echo $this->__('Select State/Province...') ?>');*/

$('billing-address-select') && billing.newAddress(!$('billing-address-select').value);

var billingRegionUpdater = new RegionUpdater('billing:country_id', 
                                             'billing:region', 
                                             'billing:region_id', 
                                             <?php echo $this->helper('directory')->getRegionJson() ?>, 
                                             undefined, 
                                             'billing:postcode');
if ($('onepage-guest-register-button')) {
    Event.observe($('onepage-guest-register-button'), 'click', function(event) {
        var billingRememberMe = $('co-billing-form').select('#remember-me-box');
        if (billingRememberMe.length > 0) {
            if ($('login:guest') && $('login:guest').checked) {
                billingRememberMe[0].hide();
            } else if ($('login:register') && ($('login:register').checked || $('login:register').type == 'hidden')) {
                billingRememberMe[0].show();
            }
        }
    });
}
//]]>
</script>        

It looks like it's comparing two values but doing nothing with the comparison. What is the purpose of it?

  • Did you try to paste it into the console of the developers tool of your browser and see what it returns? – Akif Apr 2 '15 at 19:22
  • It returns "undefined". I tested while on one page checkout using persistent billing. I verified that billing.phtml is present by turning on the template outlines in the admin panel's developer tools. – Reflexorozy Apr 2 '15 at 20:06
0

I did some research on this. I thought the && was a purely logical "and", but it can also be used as a shortcut for "if then".

In this case, if $('billing-address-select') is true, the newAddress function (in opcheckout.js) will be called with "!$('billing-address-select').value" as an argument. It's a weird way to programmatically show or hide the new address form on one page checkout.

Here's a Stack Overflow question where this is explained: https://stackoverflow.com/questions/3859433/an-explanation-of-shorthand-in-javascript

And a good explanation of & vs &&: https://stackoverflow.com/questions/7310109/whats-the-difference-between-and-in-javascript

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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