Scope
Let’s learn about lexical scoping in Dart.
We'll cover the following
Lexical scope
Lexical scope is the range of functionality of a variable so that it may only be called from within the block of code in which it is defined.
Dart is a lexically scoped language, which means that the scope of variables is determined statically, simply by the layout of the code.
Each set of curly brackets ({}
) acquires its own new scope while inheriting from the scope in which it was declared. With lexical scoping, descendant scopes will access the most recently declared variable of the same name.
We know nestedFunction
is outside the scope of main();
this was because it was declared in the scope of outerFunction
.
You can follow the curly brackets outwards to see if a variable is in scope.
Create a free account to access the full course.
Continue your learning journey with a 14-day free trial.
By signing up, you agree to Educative's Terms of Service and Privacy Policy