DEV Community

Discussion on: Closures: The JS Scope Hack

Collapse
 
boyum profile image
Sindre Bøyum

Using closures this way is a nice touch! It's true that the secret variable inside checkSecret can't be changed, however I would be careful not to add anything too secret this way, as checkSecret.toString() returns

"() => {
  const secret = 'pillow' // gets tossed into the bin! 
  return attempt => {
    return secret === attempt
  }
}"
Enter fullscreen mode Exit fullscreen mode

thus revealing the secret string.

Collapse
 
bbarbour profile image
Brian Barbour • Edited

👍 Right! Put actual secrets into .env variables.