Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

477
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda