{"action":"created","issue":{"url":"https://api.github.com/repos/jashkenas/coffeescript/issues/4931","repository_url":"https://api.github.com/repos/jashkenas/coffeescript","labels_url":"https://api.github.com/repos/jashkenas/coffeescript/issues/4931/labels{/name}","comments_url":"https://api.github.com/repos/jashkenas/coffeescript/issues/4931/comments","events_url":"https://api.github.com/repos/jashkenas/coffeescript/issues/4931/events","html_url":"https://github.com/jashkenas/coffeescript/issues/4931","id":298216112,"number":4931,"title":"CS2 Discussion: Features: Block assignment let and const operators","user":{"login":"coffeescriptbot","id":36576729,"avatar_url":"https://avatars1.githubusercontent.com/u/36576729?v=4","gravatar_id":"","url":"https://api.github.com/users/coffeescriptbot","html_url":"https://github.com/coffeescriptbot","followers_url":"https://api.github.com/users/coffeescriptbot/followers","following_url":"https://api.github.com/users/coffeescriptbot/following{/other_user}","gists_url":"https://api.github.com/users/coffeescriptbot/gists{/gist_id}","starred_url":"https://api.github.com/users/coffeescriptbot/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/coffeescriptbot/subscriptions","organizations_url":"https://api.github.com/users/coffeescriptbot/orgs","repos_url":"https://api.github.com/users/coffeescriptbot/repos","events_url":"https://api.github.com/users/coffeescriptbot/events{/privacy}","received_events_url":"https://api.github.com/users/coffeescriptbot/received_events","type":"User","site_admin":false},"labels":[{"id":841194363,"url":"https://api.github.com/repos/jashkenas/coffeescript/labels/new%20feature","name":"new feature","color":"ededed","default":false}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":9,"created_at":"2018-02-19T09:56:04Z","updated_at":"2018-02-19T09:56:20Z","closed_at":"2018-02-19T09:56:05Z","author_association":"COLLABORATOR","body":"_From @GeoffreyBooth on [2016-09-12 06:46](https://github.com/coffeescript6/discuss/issues/35)_\n\n\nBuilding off of #1 and [this comment](https://github.com/coffeescript6/discuss/issues/1#issuecomment-245963553), the great advantage of `let`/`const` is its block-scoping, i.e.:\r\n\r\n``` js\r\nlet a = 1;\r\nif (true) {\r\n  let b = 2;\r\n}\r\nconsole.log(b); // undefined\r\n```\r\n\r\nThis is a dramatic improvement over `var`, and a big reason why `let` and `const` have become popular features. This block scoping is probably something that CoffeeScript should have, separate from the feature of `const` that means “throw an error on reassignment.”\r\n\r\nWe could have both, via `:=` and `:==` operators (or whatever two operators people think are best):\r\n- `a := 1` would mean, “declare `a` at the top of this block using `let`, and on this line assign `a` with the value of `1`.”\r\n- `a :== 1` would mean, “declare and assign `a` on this line using `const`. If it gets reassigned later, throw an error.”\r\n\r\nWe don’t necessarily need the second operator, if we don’t care to give the “throw an error on reassignment” feature.\r\n\r\n`let` has the same issue as `var`, in that its declaration is hoisted to its entire scope (what [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let) refers to as the [temporal dead zone](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let#Temporal_dead_zone_and_errors_with_let)), so `let` declarations should be grouped together at the top of their block scope similar to how `var` declarations are currently grouped at the top of their function scope. `const` must be declared and assigned on the same line, so it can’t get moved up.\r\n\r\nSo this CoffeeScript:\r\n\r\n``` coffee\r\na = 1\r\nb := 2\r\nc :== 3\r\n\r\nif (yes)\r\n  b := 4\r\n  c :== 5\r\n```\r\n\r\nwould compile into this JavaScript:\r\n\r\n``` js\r\nvar a;\r\nlet b;\r\n\r\na = 1;\r\nb = 2;\r\nconst c = 3;\r\n\r\nif (true) {\r\n  let b;\r\n\r\n  b = 4;\r\n  const c = 5;\r\n}\r\n```\r\n"},"comment":{"url":"https://api.github.com/repos/jashkenas/coffeescript/issues/comments/366639778","html_url":"https://github.com/jashkenas/coffeescript/issues/4931#issuecomment-366639778","issue_url":"https://api.github.com/repos/jashkenas/coffeescript/issues/4931","id":366639778,"user":{"login":"coffeescriptbot","id":36576729,"avatar_url":"https://avatars1.githubusercontent.com/u/36576729?v=4","gravatar_id":"","url":"https://api.github.com/users/coffeescriptbot","html_url":"https://github.com/coffeescriptbot","followers_url":"https://api.github.com/users/coffeescriptbot/followers","following_url":"https://api.github.com/users/coffeescriptbot/following{/other_user}","gists_url":"https://api.github.com/users/coffeescriptbot/gists{/gist_id}","starred_url":"https://api.github.com/users/coffeescriptbot/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/coffeescriptbot/subscriptions","organizations_url":"https://api.github.com/users/coffeescriptbot/orgs","repos_url":"https://api.github.com/users/coffeescriptbot/repos","events_url":"https://api.github.com/users/coffeescriptbot/events{/privacy}","received_events_url":"https://api.github.com/users/coffeescriptbot/received_events","type":"User","site_admin":false},"created_at":"2018-02-19T09:56:20Z","updated_at":"2018-02-19T09:56:20Z","author_association":"COLLABORATOR","body":"_From @lydell on [2016-09-17 09:06](https://github.com/coffeescript6/discuss/issues/35#issuecomment-247758678)_\n\n\nJust throwing a thing in here:\n\n```\n$ coffee -e 'console.log foo\nfoo = 5'\nundefined\n\n$ node -e 'console.log(foo)\nvar foo = 5'\nundefined\n\n$ node -e 'console.log(foo)\nlet foo = 5'\n[eval]:1\nconsole.log(foo)\n            ^\n\nReferenceError: foo is not defined\n\n$ node -e 'console.log(foo)\nconst foo = 5'\n[eval]:1\nconsole.log(foo)\n            ^\n\nReferenceError: foo is not defined\n```\n"}}
{"actor":{"display_login":"coffeescriptbot"}}