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

476
Views
var vs const; why const is producing error: Uncaught SyntaxError: Identifier 'userOne' has already been declared?

When vars are replaced with const the code is producing an error. I run this code in the chrome browser console. I tried initializing the variable with let but that is producing an error as well. I ve checked docs on variables before asking this question and I know I am missing something crucial here.

class User {
    constructor(email, name){
        this.email = email;
        this.name = name;
        this.score = 0;
    }
    login(){
        console.log(this.email, 'just logged in');
        return this;
    }
    logout(){
        console.log(this.email, 'just logged out');
        return this;
    }
    updateScore(){
        this.score++;
        console.log(this.email, 'score is now', this.score);
        return this;
    }
}

class Admin extends User {
    deleteUser(user){
        users = users.filter(u => {
            return u.email != user.email
        });
    }
}

//const userOne = new User ('ryu@ninjas.com', 'Ryu');
var userOne = new User('ryu@ninjas.com', 'Ryu');
var userTwo = new User('yoshi@mariokorp.com', 'Yoshi');
var admin = new Admin('shaun@ninjas.com', 'Shaun');

var users = [userOne, userTwo, admin];
//const users = [userOne, userTwo, admin];

admin.deleteUser(userTwo);


console.log(users);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

That happen Because you have two variables userOne with the same name.

With let and const you can't declare more than one variable with the same name in the same scope, but with var it will not reproduce an error.

Also, because you are re-assing the const value of users, declare it with let not const

class User {
  constructor(email, name){
      this.email = email;
      this.name = name;
      this.score = 0;
  }
  login(){
      console.log(this.email, 'just logged in');
      return this;
  }
  logout(){
      console.log(this.email, 'just logged out');
      return this;
  }
  updateScore(){
      this.score++;
      console.log(this.email, 'score is now', this.score);
      return this;
  }
}

class Admin extends User {
  deleteUser(user){
      users = users.filter(u => {
          return u.email != user.email
      });
  }
}

const userOne = new User('ryu@ninjas.com', 'Ryu');
const userTwo = new User('yoshi@mariokorp.com', 'Yoshi');
const admin = new Admin('shaun@ninjas.com', 'Shaun');

let users = [userOne, userTwo, admin];

admin.deleteUser(userTwo);

console.log(users)

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!