Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

115
Views
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.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

The syntax of a variable initializer stipulates that it must be an expression. An if statement is not an expression.

You can write

var b = (x > 5) ? true : undefined;

(or whatever you want b to be when x is not greater than 5). That ? : expression is an expression, so it works as the initializer part of the declaration.

about 4 years ago · Juan Pablo Isaza Report

0

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
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!