Uncaught SyntaxError: Unexpected token || (OR)

I am getting this unexpected token || error for the below code-

if (x == "u")||(x == "u2"){
        var d = JSON.parse(m[1]);
        upd(d);
    }

but not if i separate the if statements like below-

if (x == "u") {
        var d = JSON.parse(m[1]);
        update(d);
    }
    if (x == "u2") {
        var d = JSON.parse(m[1]);
        upd(d);
    }

I am not sure why is this happening, very new to javascript. Any suggestions?

Thanks in advance!

2 answers

  • answered 2018-02-13 00:29 Mamun

    The basic syntax requires the whole condition should be in parenthesis. So wrap the condition in if in parenthesis:

    if ((x == "u") || (x == "u2")){

    OR: Even more simply by removing the extra parenthesis:

    if (x == "u" || x == "u2"){

  • answered 2018-02-13 00:29 CodeLover

    Shorter fix:

    if(x == "u" || x == "u2") {
    }
    

    In addition, conditional operators should be used in side parenthesis.