Eloquent Javascript

Page 53

The function body of a function created this way must always be wrapped in braces, even when it consists of only a single statement. A function can have multiple parameters or no parameters at all. In the following example, makeNoise does not list any parameter names, whereas power lists two: const makeNoise = function() { console.log("Pling!"); }; makeNoise(); // → Pling! const power = function(base, exponent) { let result = 1; for (let count = 0; count < exponent; count++) { result *= base; } return result; }; console.log(power(2, 10)); // → 1024

Some functions produce a value, such as power and square, and some don’t, such as makeNoise, whose only result is a side effect. A return statement determines the value the function returns. When control comes across such a statement, it immediately jumps out of the current function and gives the returned value to the code that called the function. A return keyword without an expression after it will cause the function to return undefined. Functions that don’t have a return statement at all, such as makeNoise, similarly return undefined. Parameters to a function behave like regular bindings, but their initial values are given by the caller of the function, not the code in the function itself.

Bindings and scopes Each binding has a scope, which is the part of the program in which the binding is visible. For bindings defined outside of any function or block, the scope is the whole program—you can refer to such bindings wherever you want. These

41


Turn static files into dynamic content formats.

Create a flipbook

Articles inside

Compatibility and the browser wars

0
page 231

In the sandbox

0
page 230

The standard

1min
page 234

HTML and JavaScript

1min
page 229

Exercises

4min
pages 221-223

Functions

3min
pages 218-219

The evaluator

1min
page 214

Asynchronous bugs

3min
pages 205-206

The environment

1min
page 217

Special forms

2min
pages 215-216

The event loop

1min
page 204

Generators

1min
page 203

Async functions

3min
pages 201-202

Message routing

4min
pages 198-200

Failure

3min
pages 192-193

Network flooding

1min
page 197

Networks are hard

4min
pages 194-195

Collections of promises

1min
page 196

Promises

1min
page 191

Callbacks

2min
pages 189-190

Crow tech

1min
page 188

Building and bundling

1min
page 181

ECMAScript modules

3min
pages 179-180

Improvised modules

1min
page 175

CommonJS

2min
pages 177-178

Evaluating data as code

1min
page 176

Packages

1min
page 174

Summary

3min
pages 169-170

International characters

1min
page 168

Parsing an INI file

2min
pages 166-167

Greed

1min
page 161

The lastIndex property

2min
pages 164-165

The replace method

1min
pages 159-160

Dynamically creating RegExp objects

1min
page 162

The search method

1min
page 163

Backtracking

2min
pages 157-158

Word and string boundaries

1min
page 155

The Date class

1min
page 154

Cleaning up after exceptions

3min
pages 141-142

Repeating parts of a pattern

1min
page 151

Exceptions

2min
pages 139-140

Assertions

1min
page 145

Selective catching

3min
pages 143-144

Error propagation

1min
page 138

Debugging

2min
pages 136-137

Testing

1min
page 135

Simulation

2min
pages 126-127

Types

0
page 134

Strict mode

0
page 133

Exercises

1min
page 131

Persistent data

1min
page 125

The task

2min
pages 123-124

Exercises

2min
pages 119-120

Summary

1min
page 118

Inheritance

1min
page 116

The iterator interface

2min
pages 112-113

Maps

2min
pages 108-109

Overriding derived properties

2min
pages 106-107

Class notation

1min
page 105

Classes

1min
page 104

Prototypes

2min
pages 102-103

Methods

1min
page 101

Exercises

0
page 99

Recognizing text

1min
page 97

Strings and character codes

2min
pages 95-96

Summary

1min
page 98

Composability

2min
pages 93-94

Summarizing with reduce

1min
page 92

Transforming with map

1min
page 91

Filtering arrays

1min
page 90

Exercises

3min
pages 82-84

Script data set

1min
page 89

5 Higher-Order Functions

1min
page 85

Higher-order functions

0
page 88

Summary

1min
page 81

JSON

1min
page 80

Destructuring

1min
page 79

The Math object

2min
pages 77-78

Rest parameters

1min
page 76

Strings and their properties

2min
pages 74-75

Further arrayology

1min
page 73

The final analysis

3min
pages 71-72

Array loops

0
page 70

Computing correlation

1min
page 69

The lycanthrope’s log

3min
pages 67-68

Mutability

2min
pages 65-66

Data sets

1min
page 60

Properties

1min
page 61

Objects

3min
pages 63-64

Methods

1min
page 62

Exercises

2min
pages 57-58

Summary

1min
page 56

Functions and side effects

1min
page 55

Growing functions

2min
pages 53-54

Recursion

3min
pages 50-52

Closure

1min
page 49

Optional Arguments

2min
pages 47-48

The call stack

1min
page 46

Arrow functions

1min
page 45

Declaration notation

1min
page 44

Functions as values

1min
page 43

Bindings and scopes

2min
pages 41-42

Exercises

2min
pages 38-39

Summary

1min
page 37

Dispatching on a value with switch

1min
page 35

Summary

0
page 21

Updating bindings succinctly

1min
page 34

Indenting Code

1min
page 32

while and do loops

2min
pages 30-31

Conditional execution

2min
pages 28-29

Strings

4min
pages 13-14

Bindings

3min
pages 23-24

Unary operators

1min
page 15
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.