Raja G
Raja G

Reputation: 6643

Is there any way to reset Global variable in JavaScript

I have a global variable with classname defined to it.

var className = "My Class is.";

And I will use that className variable to add more

var a = function() {
  className = className + "a" ;
  console.log(className);
}    
console.log(className);
a();
console.log(className);

But after calling the function a(), variable className is still holding the data. I believe this is how JS behaves, but is there any way to reset className variable every time when it comes out of a function.

Upvotes: 0

Views: 787

Answers (3)

ControlAltDel
ControlAltDel

Reputation: 35011

Two ways:

  1. (Better) Don't add the letter in the first place

var a = function() { console.log(className + "a"); }

or

var a = function() {
  let temp = className + "a" ;
  console.log(temp);
}    
  1. Store the original value

`

 var className = ...;
 var backup = className;

 var a = function() {
   className = className + "a" ;
   console.log(className);
 }    
 console.log(className);
 a();
 className = backup;
 console.log(className);

`

Upvotes: 0

junvar
junvar

Reputation: 11574

If you really want to use a local variable with the same name, that is possible:

var className = "My Class is."; // define global var

let a = function() {
  let className = window.className; // define local var and copy global var value to local var
  className = className + "a"; // modify local var
  console.log(className); // print local var
};

console.log(className); // print global var value
a(); // does not change global var value
console.log(className) // print unchanged global var value

But it would be much clearer to just have different names for your local and global variables:

var className = "My Class is.";

let a = function() {
  let localClassName = className;
  localClassName = localClassName + "a";
  console.log(localClassName)
};

console.log(className);
a();
console.log(className)

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1074475

Based on your comment:

I am defining a local variable named FunctionName with className:MethodName as format. If I can define a variable with className, then I can use that instead of placing complete className in every method, just trying avoid retyping and spelling mistakes mostly.

...I think you want to just use className without modifying it:

var a = function() {
    var functionName = className + "a";
    // ...use functionName...
};

But if you really want to modify it for some reason, without using a local, then you can remember its old value and restore it:

var a = function() {
    var savedClassName = className + "a";
    try {
        className = className + "a";
        // ...use className...
    } finally {
        className = savedClassName;
    }
};

No matter how you exit the code in the try block, the finally block will be executed.

But there are very few use cases where that's the best approach.

Upvotes: 1

Related Questions