Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

117
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda