Youssef
Youssef

Reputation: 645

Setting variables in if statement using JavaScript throw error (already declared)

I have this code

if (found === true && premium === 'active' || premium === 'trialing') {
  var free = false
} else {
  var free = true
}

The express server works fine on both localhost and remotely on Heroku, I tried deploying this app to firebase hosting, one the error that prevented me to deploy is

75:17 error 'free' is already defined no-redeclare

I am not sure how to fix that, my goal is simply to pass free variable with a response like so

response.render('master.html', { links, profile, free })

Upvotes: 2

Views: 411

Answers (2)

keshin
keshin

Reputation: 534

https://eslint.org/docs/rules/no-redeclare

var free = true;
if (found === true && premium === 'active' || premium === 'trialing') {
  free = false
}

Upvotes: 1

Narendra Chouhan
Narendra Chouhan

Reputation: 2319

You can declare this way and remove the else condition

let free = true;
if (found === true && premium === 'active' || premium === 'trialing') {
   free = false 
}

Upvotes: 1

Related Questions