6

I wonder whether it is safe to use let in this scenario:

function test() {
  let result = 0;
  result++;
  return result;
}
longlifelearner
  • 195
  • 1
  • 10
  • 3
    Why exactly wouldn't it be safe? – Andrew Li Feb 15 '17 at 22:49
  • 1
    it wouldn't be safe in a browser that doesn't understand `let` ... looking at you, Internet Exploder ... otherwise, it's perfectly fine – Jaromanda X Feb 15 '17 at 22:50
  • http://stackoverflow.com/questions/762011/ – david25272 Feb 15 '17 at 23:10
  • What do you mean by "safe"? – Bergi Feb 15 '17 at 23:58
  • I'm voting to close this question as off-topic because it's a trivial question answered in the syntax doc of ES. – Ondra Žižka Feb 16 '17 at 03:38
  • 1
    The reason I ask this question is I don't know how javascript machine will deal with "let" when we use it to return value to a function. Since "let" variable has function scope, will gc collect it later? If so, the object gets return value may be gotten undefined value. Look at the answer below. – longlifelearner Feb 16 '17 at 19:55
  • 1
    @OndraŽižka It was silly closing this question, the question is perfectly understandable. Most users search google for their answers, as I did. This question appeared at the top, and was clearly relevant. Expecting everyone to go and read the manual is basically an argument against most of SO, and sometimes a human factor helps understanding, as opposed to the highly technical language used in documentation. – Chris A Sep 18 '18 at 08:29

2 Answers2

13

Yes, it is perfectly safe and it is the expected way to declare a local variable that you later want to return as the value of the function. Because Javascript is a garbage collected language, the local variable can safely contain any type of value. It can have a simple primitive like you show, or it can even contain an object like this:

function test() {
  let result = {cnt: 0};
  result.cnt++;
  return result;
}

let obj = test();
console.log(obj);     // shows {cnt: 1}

This is safe because though you are returning an object that was declared within the scope of a function and that function scope will go out of scope when the function returns, because Javascript is a garbage-collected language, the object will separately live on in whatever the return value of your function is assigned to outside of the function scope.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Thank you for your answer. Will gc collect it later, and make obj become undefined? – longlifelearner Feb 16 '17 at 19:56
  • 2
    @longlifelearner - GC will only ever clean up something that is no longer in use. So, if you've assigned the obj to a variable that is still accessible by your code, then it will not be GCed. That's how GC works. It cleans up things that are no longer reachable by any code. So, it will never clean up something you are still using. – jfriend00 Feb 16 '17 at 22:27
1

To piggy back on the comments posted above.
Const - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const is a great way to let yourself as well as other people who are reading your code know that 'this variable wont be reassigned or modified.'

Let - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let lets people know that this variable will most likely get reassigned or modified.

Use of let in your case is perfectly okay!