The Complete JavaScript Guide - Scope

The Complete JavaScript Guide - Scope

In JavaScript, there are two types of variable scopes:

  • Global Scope : Variables declared Globally (outside of any function) have Global Scope and Global variables can be accessed from anywhere in a program


	let globalLet = "This is a global variable";

	function fun() {
	let localLet = "This is a local variable";

	console.log(globalLet); 
	console.log(localLet); 
	}
	fun();


output ---> //This is a global variable
            // This is local varibale
        

  • Local Scope : Variables declared inside a function become local to the function. Local variables are created when a function starts and deleted when the function is executed.


	let globalLet = "This is a global variable";

	function fun() {
	let localLet = "This is a local variable";

	
	}
	fun();
           
        console.log(globalLet); // This is a global variable
	console.log(localLet); 

output ---> // This is global variable

We are still able to see the value of the global variable, but for local variable console.log throws an error. This is because now the console.log statements are present in global scope where they have access to global variables but cannot access the local variables.        


要查看或添加评论,请登录

Ajay Thopate的更多文章

  • RIP

    RIP

    Routing Information Protocol. RIP is open standard protocol.

    2 条评论
  • ROUTING PARAMETERS

    ROUTING PARAMETERS

    Metric :- It is the parameter used by the routing protocol to find it’s best path RIP – Hop count (distance) OSPF –…

  • STATIC ROUTING

    STATIC ROUTING

    Static routing is the manual method of routing. Static routes the administrative distance is as default value.

  • ROUTING

    ROUTING

    A router is a network device that connects computing devices and network to other network. Routing is the process of…

  • Collision domain and Broadcast domain

    Collision domain and Broadcast domain

    Collision Domain Collision Domain is a single physical line that a collision can occur. If one more device tries send…

  • IP Address

    IP Address

    An Internet Protocol (IP) address is the unique identifying number assigned to every device connected to the internet…

  • TCP/IP – Model

    TCP/IP – Model

    TCP – Transmission Control Protocol. IP – Internet Protocol.

  • OSI Model

    OSI Model

    OSI - Open System Interconnection. OSI model created in 1984 by ISO.

    1 条评论
  • INTRODUCTION AND BASIC INFORMATION OF NETWORK DEVICES

    INTRODUCTION AND BASIC INFORMATION OF NETWORK DEVICES

    Types of Network Devices :- 1. Hub 2.

  • The Complete JavaScript Guide - Functions

    The Complete JavaScript Guide - Functions

    Functions are one of the fundamental building blocks in JavaScript. With functions you can reuse code You can write…

社区洞察

其他会员也浏览了