Eloquent Javascript

Page 44

let yourName; do { yourName = prompt("Who are you?"); } while (!yourName); console.log(yourName);

This program will force you to enter a name. It will ask again and again until it gets something that is not an empty string. Applying the ! operator will convert a value to Boolean type before negating it, and all strings except "" convert to true. This means the loop continues going round until you provide a non-empty name.

Indenting Code In the examples, I’ve been adding spaces in front of statements that are part of some larger statement. These spaces are not required—the computer will accept the program just fine without them. In fact, even the line breaks in programs are optional. You could write a program as a single long line if you felt like it. The role of this indentation inside blocks is to make the structure of the code stand out. In code where new blocks are opened inside other blocks, it can become hard to see where one block ends and another begins. With proper indentation, the visual shape of a program corresponds to the shape of the blocks inside it. I like to use two spaces for every open block, but tastes differ—some people use four spaces, and some people use tab characters. The important thing is that each new block adds the same amount of space. if (false != true) { console.log("That makes sense."); if (1 < 2) { console.log("No surprise there."); } }

Most code editor programs will help by automatically indenting new lines the proper amount.

32


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.