factor out generateJsBranch() function.