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

264
Vistas
Why is the ternary operator defaulting to true

I have the following bit of code:

const production = process.env.PRODUCTION
console.log(production)

const corsOptions = {
    origin: production ? 'https://myproductionurl' : 'http://localhost:3000',
    credentials: true
}

console.log(corsOptions)


app.use(cors(corsOptions))

When I run this code the first console.log is returning false as desired however the second console.log statement is returning the production URL as if the variable is true.

Not sure why this is if someone spots something then please let me know!

Furthermore, if I remove the ternary and URLs to test what's going on then i will get origin: false in the corsOptions which is stumping me even more!

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Environment variables are always strings, unless they aren't set in which case they will be undefined.

If it is logging false, then you have set it to the string "false" which is a truthy value.

You can test for that explicitly:

production !== 'false' ? 'https://myproductionurl' : 'http://localhost:3000',

… although you should probably write a test that is more robust than that such as:

const isProduction = () => {
    const env = process.env.PRODUCTION; 
    if (!env) return false;
    if (env === "false") return false;
    return true;
}

and then

isProduction() ? 'https://myproductionurl' : 'http://localhost:3000',
about 4 years ago · Juan Pablo Isaza Denunciar

0

Update your ternary logic

const env = process.env.PRODUCTION
console.log(env)

const corsOptions = {
    origin: env == "production" ? 'https://myproductionurl' : 'http://localhost:3000',
    credentials: true
}

console.log(corsOptions)


app.use(cors(corsOptions))
about 4 years ago · Juan Pablo Isaza Denunciar

0

Because all environment variables are strings. And string that is false does not have boolean value of false, therefore the ? just see string that contains some characters => therefore resolving it as true.

const production = 'false'
const productionBool = false
console.log(production)

const corsOptions = {
    origin: production ? 'https://myproductionurl' : 'http://localhost:3000',
    originBool: productionBool ? 'https://myproductionurl' : 'http://localhost:3000',
    credentials: true
}

console.log(corsOptions)

You can resolve it i.e. by (String(production).toLowerCase() === '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