×
1 Choose EITC/EITCA Certificates
2 Learn and take online exams
3 Get your IT skills certified

Confirm your IT skills and competencies under the European IT Certification framework from anywhere in the world fully online.

EITCA Academy

Digital skills attestation standard by the European IT Certification Institute aiming to support Digital Society development

LOG IN TO YOUR ACCOUNT

CREATE AN ACCOUNT FORGOT YOUR PASSWORD?

FORGOT YOUR PASSWORD?

AAH, WAIT, I REMEMBER NOW!

CREATE AN ACCOUNT

ALREADY HAVE AN ACCOUNT?
EUROPEAN INFORMATION TECHNOLOGIES CERTIFICATION ACADEMY - ATTESTING YOUR PROFESSIONAL DIGITAL SKILLS
  • SIGN UP
  • LOGIN
  • INFO

EITCA Academy

EITCA Academy

The European Information Technologies Certification Institute - EITCI ASBL

Certification Provider

EITCI Institute ASBL

Brussels, European Union

Governing European IT Certification (EITC) framework in support of the IT professionalism and Digital Society

  • CERTIFICATES
    • EITCA ACADEMIES
      • EITCA ACADEMIES CATALOGUE<
      • EITCA/CG COMPUTER GRAPHICS
      • EITCA/IS INFORMATION SECURITY
      • EITCA/BI BUSINESS INFORMATION
      • EITCA/KC KEY COMPETENCIES
      • EITCA/EG E-GOVERNMENT
      • EITCA/WD WEB DEVELOPMENT
      • EITCA/AI ARTIFICIAL INTELLIGENCE
    • EITC CERTIFICATES
      • EITC CERTIFICATES CATALOGUE<
      • COMPUTER GRAPHICS CERTIFICATES
      • WEB DESIGN CERTIFICATES
      • 3D DESIGN CERTIFICATES
      • OFFICE IT CERTIFICATES
      • BITCOIN BLOCKCHAIN CERTIFICATE
      • WORDPRESS CERTIFICATE
      • CLOUD PLATFORM CERTIFICATENEW
    • EITC CERTIFICATES
      • INTERNET CERTIFICATES
      • CRYPTOGRAPHY CERTIFICATES
      • BUSINESS IT CERTIFICATES
      • TELEWORK CERTIFICATES
      • PROGRAMMING CERTIFICATES
      • DIGITAL PORTRAIT CERTIFICATE
      • WEB DEVELOPMENT CERTIFICATES
      • DEEP LEARNING CERTIFICATESNEW
    • CERTIFICATES FOR
      • EU PUBLIC ADMINISTRATION
      • TEACHERS AND EDUCATORS
      • IT SECURITY PROFESSIONALS
      • GRAPHICS DESIGNERS & ARTISTS
      • BUSINESSMEN AND MANAGERS
      • BLOCKCHAIN DEVELOPERS
      • WEB DEVELOPERS
      • CLOUD AI EXPERTSNEW
  • FEATURED
  • SUBSIDY
  • HOW IT WORKS
  •   IT ID
  • ABOUT
  • CONTACT
  • MY ORDER
    Your current order is empty.
EITCIINSTITUTE
CERTIFIED

What is shadowing in JavaScript and how does it affect variable access?

by EITCA Academy / Monday, 07 August 2023 / Published in Web Development, EITC/WD/JSF JavaScript Fundamentals, Functions in JavaScript, Introduction to global and local scope, Examination review

Shadowing in JavaScript refers to the concept of a variable in an inner scope having the same name as a variable in an outer scope. When shadowing occurs, the inner variable temporarily hides the outer variable, affecting the access and visibility of the variables within the code. This phenomenon is closely related to the concept of variable scope in JavaScript.

In JavaScript, variables can be declared with the `var`, `let`, or `const` keywords. The scope of a variable determines where it can be accessed and how long it persists in memory. JavaScript has two main types of scope: global scope and local scope.

Global scope refers to variables that are accessible throughout the entire JavaScript code, regardless of where they are declared. On the other hand, local scope refers to variables that are only accessible within a specific block of code, such as a function or a block statement.

When a variable is shadowed, it means that a new variable with the same name is declared within a more nested scope, effectively hiding the variable with the same name in the outer scope. This can lead to unexpected behavior and bugs if not handled carefully.

Let's consider an example to illustrate shadowing in JavaScript:

javascript
var x = 10;

function myFunction() {
  var x = 20; // shadowing occurs here
  console.log(x); // outputs 20
}

console.log(x); // outputs 10

myFunction();

In this example, we have a global variable `x` with a value of 10. Inside the `myFunction` function, a new variable `x` is declared with a value of 20. When we log the value of `x` inside the function, it outputs 20 because the inner variable is in scope. However, when we log the value of `x` outside the function, it outputs 10 because the global variable is still accessible.

Shadowing can also occur with block-scoped variables declared using the `let` or `const` keywords. Consider the following example:

javascript
let x = 10;

if (true) {
  let x = 20; // shadowing occurs here
  console.log(x); // outputs 20
}

console.log(x); // outputs 10

In this case, we have a block statement (the `if` statement) that introduces a new block scope. Inside the block, a new variable `x` is declared and assigned a value of 20, shadowing the outer variable `x`. When we log the value of `x` inside the block, it outputs 20. However, when we log the value of `x` outside the block, it outputs 10 because the outer variable is still accessible.

It's important to note that shadowing can make code harder to read and understand, especially when variables have the same name but hold different values in different scopes. To avoid confusion and potential bugs, it's generally recommended to use different variable names or refactor the code to avoid shadowing.

Shadowing in JavaScript occurs when a variable in an inner scope has the same name as a variable in an outer scope, temporarily hiding the outer variable. This affects the access and visibility of the variables within the code. It's important to be aware of shadowing and handle it carefully to avoid unexpected behavior and bugs.

Other recent questions and answers regarding Examination review:

  • Why is it generally recommended to limit the use of global variables in JavaScript?
  • What happens when a function encounters a return statement?
  • How are global variables accessed within functions?
  • What is the difference between global and local scope in JavaScript?

More questions and answers:

  • Field: Web Development
  • Programme: EITC/WD/JSF JavaScript Fundamentals (go to the certification programme)
  • Lesson: Functions in JavaScript (go to related lesson)
  • Topic: Introduction to global and local scope (go to related topic)
  • Examination review
Tagged under: Global Scope, JavaScript, Local Scope, Shadowing, Variable Scope, Web Development
Home » Web Development » EITC/WD/JSF JavaScript Fundamentals » Functions in JavaScript » Introduction to global and local scope » Examination review » » What is shadowing in JavaScript and how does it affect variable access?

Certification Center

USER MENU

  • My Account

CERTIFICATE CATEGORY

  • EITC Certification (105)
  • EITCA Certification (9)

What are you looking for?

  • Introduction
  • How it works?
  • EITCA Academies
  • EITCI DSJC Subsidy
  • Full EITC catalogue
  • Your order
  • Featured
  •   IT ID
  • EITCA reviews (Medium publ.)
  • About
  • Contact

EITCA Academy is a part of the European IT Certification framework

The European IT Certification framework has been established in 2008 as a Europe based and vendor independent standard in widely accessible online certification of digital skills and competencies in many areas of professional digital specializations. The EITC framework is governed by the European IT Certification Institute (EITCI), a non-profit certification authority supporting information society growth and bridging the digital skills gap in the EU.
Eligibility for EITCA Academy 90% EITCI DSJC Subsidy support
90% of EITCA Academy fees subsidized in enrolment

    EITCA Academy Secretary Office

    European IT Certification Institute ASBL
    Brussels, Belgium, European Union

    EITC / EITCA Certification Framework Operator
    Governing European IT Certification Standard
    Access contact form or call +32 25887351

    Follow EITCI on X
    Visit EITCA Academy on Facebook
    Engage with EITCA Academy on LinkedIn
    Check out EITCI and EITCA videos on YouTube

    Funded by the European Union

    Funded by the European Regional Development Fund (ERDF) and the European Social Fund (ESF) in series of projects since 2007, currently governed by the European IT Certification Institute (EITCI) since 2008

    Information Security Policy | DSRRM and GDPR Policy | Data Protection Policy | Record of Processing Activities | HSE Policy | Anti-Corruption Policy | Modern Slavery Policy

    Automatically translate to your language

    Terms and Conditions | Privacy Policy
    EITCA Academy
    • EITCA Academy on social media
    EITCA Academy


    © 2008-2026  European IT Certification Institute
    Brussels, Belgium, European Union

    TOP
    CHAT WITH SUPPORT
    Do you have any questions?
    We will reply here and by email. Your conversation is tracked with a support token.