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

133
Vistas
Can't declare top in js

I am using following code to quickly assign window.getComputedStyle to a variable:

const {
  width,
  height,
  left
} = window.getComputedStyle(document.querySelector('canvas'));
console.log(width, left, height)
<canvas></canvas>

But when I want to declare top, it returns:

Uncaught SyntaxError: Identifier top has already been declared

const {
  width,
  height,
  left,
  top
} = window.getComputedStyle(document.querySelector('canvas'));
console.log(width, left, height)
<canvas></canvas>
But I haven't declared it before and it seems that it does work with left.

What am I missing?

Thanks.

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

0

top is a global variable in the browser. That means it's already defined in the context you're trying to define it in. You can name it something else:

const {
  width,
  height,
  left,
  top: topStyle, // this basically renames "top" to "topStyle"
} = window.getComputedStyle(document.querySelector('canvas'));

console.log(width, left, height, topStyle)

Or assign the computed style to a variable and access it there:

const canvasStyles = window.getComputedStyle(document.querySelector('canvas'));

console.log(
  canvasStyles.width,
  canvasStyles.left,
  canvasStyles.height,
  canvasStyles.top
);

Or just don't try to define the variable in the global scope. Instead define it in a function:

function accessPosition() {
  const {
    width,
    height,
    left,
    top
  } = window.getComputedStyle(document.querySelector('canvas'));

  console.log(width, left, height, top);
}

accessPosition();
about 4 years ago · Juan Pablo Isaza Denunciar

0

if you open your browser's developer tools and type:

console.log(top)

You will see it's already declared to represent the top-most window.

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