0

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.

5
  • 1
    Why not just use a local variable instead of updating the global variable? Commented Jan 28, 2020 at 18:32
  • @Barmar, 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. Commented Jan 28, 2020 at 18:34
  • 3
    @rɑːdʒɑ - Then just use className without modifying it: var methodName = className + "a"; Commented Jan 28, 2020 at 18:35
  • @T.J.Crowder, looking like a good way of doing. Is it not possible to reset global variable after function call got over ? Commented Jan 28, 2020 at 18:37
  • @rɑːdʒɑ - It's possible, just usually not best practice -- I've posted an answer with how. Commented Jan 28, 2020 at 18:38

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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)

Comments

0

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);

`

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.