Functions and Scope
Jeremy McPeak, Paul Wilton · 2015
Functions in JavaScript encapsulate a block of code that performs a certain task. This chapter shows an example of a function declaration. Each function defined in JavaScript must be given a unique name for that particular page. The name comes immediately after the function keyword. Functions enable you to return a value from a function to the code that called it. You use the return statement to return a value. When JavaScript comes across a return statement in a function, it treats it a bit like a break statement in a 'for' loop. Scope is important to any programming language, even more so in JavaScript. The chapter explains how scope works in JavaScript. When you use a variable or function, the JavaScript engine goes through the identifier lookup process to find the value associated with the identifier.