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

252
Views
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 answers
Answer question

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 Report

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 Report

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 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!