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

202
Vistas
Difference between local scope and function scope in JavaScript

I could not fully understand the difference between these two scopes, as some sources are old and some tell different things from each other. Could you please explain to me the difference between local scope and function scope in a simple way?

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

0

There is no difference between "local scope" and "function scope".

Up to and including ES5 there were only two scopes: global and "not global". The latter one would be called "local" or "function". Both terms means the same thing:

var a = "this is global scope";

function foo() {
    var b = "this is local/function scope";

  if (true) {
    var c = "this is in the same local/function scope as b";
  }

  for (var i = 0; i < 4; i++) {
    var d = "this is also in the same local/
function scope as b and c";
  }
}

It is function because it is inside the function.


As of ES6 the term "local" scope might be a bit of a misnomer, since there are more scopes than just two. For example block scope for variables like let or const:

const a = "this is global scope";;

function foo() {
  const b = "this is function scope";
  if (true) {
    const c = "this is in block scope";
  }

  for (let i = 0; i < 4; i++) {
    const d = "this is also in block scope but different to c";
  }
}

as well as module scope for any file that is a module:

export const a = "this is in module scope";
about 4 years ago · Juan Pablo Isaza Denunciar

0

Take a look at this source

https://javascript.info/closure

Best regads!

about 4 years ago · Juan Pablo Isaza Denunciar

0

Local scope just refers to the scope available to a given variable, but function scope would refer to variables inside a function.

Here var x is inside a function, so it's only accessible inside the function.

function hi() {
  var x = 'hello';
  console.log(x);
}
hi();

console.log(x); // this will throw an error: "x is not defined"

Here var x is locally scoped to the window object, which is also called globally scoped, and thus it is available inside or outside the function:

var x = 'hello';
console.log(x);
function hi() {
  x = 'world';
  console.log(x);
}
hi();

console.log(x);
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