What is a local variable:- JavaScript

  • Variables declared within a JavaScript function, become LOCAL to the function.
  • Local variables have local scope: They can only be accessed within the function.
  • If you trying to access the local variable from other function it will return undefined texti




Code:-

<!DOCTYPE html> 
<html> 
 
<head> 
 <meta name="viewport" content="width=device-width, initial-scale=1"> 
</head> 
 
<body> 
 <h1>Local Variable</h1> 
 
 <p>A local variable can only be accessed from within the function </p> 
 
 <input type="button" onclick="cal_fun1();" value="Function 1"> 
 <input type="button" onclick="cal_fun2();" value="Function 2"> 
 
 <script> 
  function cal_fun1() 
  { 
   var carname = "bus"; 
   alert(typeof(carname)); 
 
  } 
 
  function cal_fun2() 
  { 
   //Here we can't access carname variable 
   alert(typeof(carname)); 
 
  } 
 </script> 
 
</body> 
 
</html>


Important Note
If you declare the variable without varkeyword, it will consider as global variable.




What is a local variable:- JavaScript What is a local variable:- JavaScript Reviewed by Network security on June 03, 2019 Rating: 5

1 comment:

  1. There are dissertation sheets together with the web site while you turned out to be obviously believed in the blog page. include javascript file in html

    ReplyDelete

Useful Information

Powered by Blogger.