• Tech Travel Hub is your one stop, ultimate tour guide to all things tech, travel, visas and digital nomads
  • Home
  • Blog
    • Technology
      • General Tech
      • Blogging
      • Java
    • Lifestyle
      • General Lifestyle
    • Travel
    • Educational
    • Business
  • About
  • Contact
Menu
  • Home
  • Blog
    • Technology
      • General Tech
      • Blogging
      • Java
    • Lifestyle
      • General Lifestyle
    • Travel
    • Educational
    • Business
  • About
  • Contact
View Blog
April 30, 2023April 30, 2023

Scope of Variable In JavaScript: Quick Facts in 2021

Introduction to Scope of Variable In JavaScript

Whenever we define a variable in javascript, the next question comes what is the scope of that variable? Well, javascript has two variable types.

  • Global
  • Local

Global:

The variable is defined outside of any block called global and can be accessed through any part of the page.

Local:

The variable which is defined inside of any function or block is called the local variable. This can only be accessed through the function or block where it is defined.
Javascript uses scope chains to establish the scope for a given function. There is typically one global scope, and each function defined has its own nested scope.
Any function defined within another function has a local scope that is linked to the outer function. It’s always the position in the source that defines the scope.
An element in the scope chain is basically a Map with a pointer to it’s parent scope. When resolving a variable, javascript starts at the innermost scope and searches outwards.
This is a sample code how to get input from the user then store that inside a variable. Now use that variable whenever it is required.

<HTML>
<HEAD>
<SCRIPT language = JavaScript>
var name = prompt(“Enter your name”, “Name”);
</SCRIPT>
</HEAD>

<BODY>
<SCRIPT language = “JavaScript”>
document.write(“<H2> Hello ” + name  + “</H2>”);
</SCRIPT>
</BODY>
</HTML>

In this example, the name is defined as global and accessed through different portions of the page. Now let us understand how it is saved inside the memory.

when it is running it takes the input from the user and stores that inside of the “name” variable. This is outside of any function hence it is placed inside the global scope.

Further visit: Best Tip to Use 4 Methods in Button in JavaScript

Now when we require the “name” it checks the scope. It is not inside of any function or block so it then goes for the global scope. If found inside the global scope, the reference is established.

Let us take a second example

<html>
<head>
<script type=”text/javascript”>
function product(a,b)
{
var c=a*b;
return c
}
</script>
</head>

<body>
<script type=”text/javascript”>
document.write(product(5,4));
</script>

</body>
</html>

here c is defined inside of a function called product so this is a property of the function. All private property of any function is written inside of a structure of that function.

This structure is placed inside the DOM stack. So again when we are trying to print the output .. the Javascript engine checks the location of the variable. It starts from the innermost block and found c-the variable is present inside the function block. It prints.

For further reference:

  • http://stackoverflow.com/questions/500431/javascript-variable-scope
  • http://www.mredkj.com/tutorials/reference_js_intro_ex.html
Your Ad Here

Post navigation

Previous post
Next post

Umer Umer

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • 7 Best Features To Be Considered While Developing An Uber Clone App
  • 5 Hacks To Develop Excellent Mobile Friendly Website And Content
  • How to Develop a Telemedicine App in 2021? 8 Important Points
  • How To Develop A Fantasy Sports Website And App Like Dream 11?
  • 5 Best Top Destination to Visit in Peru

Categories

  • Adventure
  • Agile Methodology
  • All world Tour
  • Android Development
  • Ansible
  • App
  • App Development
  • Artificial Intelligence
  • AWS
  • Bitcoin
  • BlockChain
  • Blogging
  • Box Packing
  • Business
  • business
  • Business Communication
  • buying guide
  • C
  • car
  • Care
  • Certification
  • Certification
  • Chatbot
  • cinema
  • Cloud Tech
  • Content Marketing
  • Corporate
  • covid-19
  • crucible
  • Cryptocurrency
  • Database
  • DevOps
  • DevOps
  • Digital Marketing
  • Drawing
  • Dress
  • Educational
  • Employment
  • Entertainment
  • Exceptions In Java
  • Fashion
  • Festivals
  • Finance
  • Fitness
  • Food
  • Game
  • Gift ideas
  • Groovy
  • Health
  • Health
  • home
  • home improvement
  • HR
  • IllegalThreadStateException in Java
  • India Tour
  • Instagram
  • Ios
  • ISO
  • Java
  • Javascript
  • job
  • Kids Section
  • Kitchen
  • Law
  • Life Style
  • Lifestyle
  • Lifestyle Home Improvement
  • Main Stories
  • Management
  • Mattress
  • Motorable
  • Movie
  • nursing
  • Pendrive
  • Perspective
  • Pet
  • Popular
  • Programming
  • Python
  • Relationships
  • Selenium
  • SEO
  • Skin
  • SOAPUI
  • Social Media
  • Social Media Marketing
  • Software
  • software development
  • Software Development Model
  • Software Engineering
  • Software Testing
  • Sports
  • Startup
  • T Robot
  • TDD
  • Tech
  • Tech News
  • Tech News
  • Technology
  • Tool
  • Tour Guide
  • Tour Guide
  • Travel
  • UFT
  • Uncategorized
  • USB Cable
  • VPN
  • Web Development
  • Wordpress
  • Yoga
  • Youtube

Tags

Instagram Instagram Marketing Marketing SEO Thoptv App

Categories

  • Tech
  • Travel
  • Visas
  • Digital Nomad
  • Tech
  • Travel
  • Visas
  • Digital Nomad

Publishing

If you would like to publish on our site, please email admin@techtravelhub.com

Quick Links

  • Home
  • Blog
    • Technology
      • General Tech
      • Blogging
      • Java
    • Lifestyle
      • General Lifestyle
    • Travel
    • Educational
    • Business
  • About
  • Contact
  • Home
  • Blog
    • Technology
      • General Tech
      • Blogging
      • Java
    • Lifestyle
      • General Lifestyle
    • Travel
    • Educational
    • Business
  • About
  • Contact

Contact

Tech Travel Hub LLC

Address:
8 The Green STE A
Dover
Delaware, 19901

Phone: +1 302 956 9948