Stage 2 · Lesson 2 of 10
Functions and scope
In , functions are values. You can store them in variables, pass them to other functions and return them from functions. That is how callbacks, and all work.
Learn the three ways to write one: function declarations, function expressions and . Declarations behave as if they were moved to the top of their scope, which is called , so you can call them before the line that defines them.
Scope decides where a variable is visible. let and const follow block scope: a variable declared inside an if or a loop does not exist outside it. An inner function can read the variables of the functions around it.
Common traprelying on globals and shared variables, which makes code that works alone break when combined with other code.
A question to check yourself, an AI practice session and 1 cheat sheet entry come with the full roadmap. Unlock
Resources for this stage
Start with· follow one of these
- CoursePaidGrokking JavaScript Fundamentals
Design Gurus · Beginner · $50
Guided beginner course on the language core, with quizzes after every chapter
- GuideFreeJavaScript.info
Ilya Kantor
Free and thorough: Part 1 for the language, Part 2 for the DOM and events, Part 3 for fetch
Practise
Keep handy
Also worth your time
- You Don't Know JS YetGo deeper
For when scope, this, coercion or async behaviour still feel like magic