You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I think aside from variable hoisting, it's pretty fair to describe the scoping as JS-like. And since hoisting is (arguably) a quirk in JS and lack of hoisting is not a quirk in R, I don't think we need a clarification. I don't think we're making a rigorous claim, just giving a gist.
Eventually ES6 implementations will start to use let and we'll have block scoping in JS, but that's another story. :)
Hello; thanks for the feedback! I think the relatively narrow claim the text makes (that neither Javascript or R has block scope) is correct and is not likely to be confusing, but maybe I'm underestimating the importance of hoisting to Javascript developers.
@tdsmith It's not important, strictly speaking. You need to know about it to reason about the code, but it's not really a matter of scope. Hoisting works within a scope and impacts only the resolution order for variable definitions. Here is a broad overview.
While it's true that variable scope is not per-block, it's not the same as in JavaScript, where local variables are hoisted to top of the function:
Example from Mozilla.org:
Try the same in R and you'll get "Error in print(myvar) : object 'myvar' not found":
But yes, there is no block scope:
will print "local variable". For block-scope, you can use
local
:will throw: "Error in print(myvar) : object 'myvar' not found"
The text was updated successfully, but these errors were encountered: