Search code examples
javascriptif-statementreturnconditional-statementsexpression

Why this code is incorrect in javaScript? JS experts please explain


var x = 10;


var b = if(x>5){return true};

console.log(b);

I'm not getting why this is incorrect and will give an error.


Solution

  • You can use this solution

    var x = 10;
    
    var b =  x>5? true:false
    # if the condition (x>5) is true. 
    # will execute the first return(true) 
    # or else the second return will execute (false) 
    
    
    console.log(b) #Output= true