Introduction:
Study, practice, then rest. If you’re intent on mastering the fundamentals of JavaScript, as opposed to just getting a feel for the language, work with this book and the online exercises in a 15-to-30-minute session, then take a break. Study a chapter for 5 to 10 minutes, practicing the lesson until you’ve coded everything correctly. Then go for a walk.
Use the largest, most colorful screen available. This book can be read on small phone screens and monochrome readers, but you’ll be happier if things appear in color on a larger screen. I use color as an important teaching tool, so if you’re reading in black-andwhite, you’re sacrificing some of the extra teaching value of a full-color ebook. Colored elements do show up as a lighter shade on some black-and-white screens, and on those devices the effect isn’t entirely lost, but full color is better. As for reading on a larger screen— the book includes more than 2,000 lines of example code. Small screens break long lines of code into awkward, arbitrary segments, jumbling the formatting. While still decipherable, the code becomes harder to read. If you don’t have a mobile device that’s ideal for this book, consider installing the free Kindle reading app on your laptop.
If you’re reading on a mobile device, go horizontal. For some reason, I resist doing this on my iPad unless I’m watching a video. But even I, Vern Vertical, put my tablet into horizontal mode to proof this book. So please: starting with Chapter 1, do yourself a favor and rotate your tablet, reader, or phone to give yourself a longer line of text. It’ll help prevent the unpleasant code jumble mentioned above.
Do the coding exercises on a physical keyboard. A mobile device can be ideal for reading, but it’s no way to code. Very, very few Web developers would attempt to do their work on a phone. The same thing goes for learning to code. Theoretically, most of the interactive exercises could be done on a mobile device. But the idea seems so perverse that I’ve disabled online practice on tablets, readers, and phones. Read the book on your mobile device if you like. But practice on your laptop.
If you have an authority problem, try to get over it. When you start doing the exercises, you’ll find that I can be a pain about insisting that you get every little detail right. For example, if you indent a line one space instead of two spaces, the program monitoring your work will tell you the code isn’t correct, even though it would still run perfectly. Do I insist on having everything just so because I’m a control freak? No, it’s because I have to place a limit on harmless maverick behavior in order to automate the exercises. If I were to grant you as much freedom as you might like, creating the algorithms that check your work would be, for me, a project of driverless-car proportions. Besides, learning to write code with fastidious precision helps you learn to pay close attention to details, a fundamental requirement for coding in any language. Subscribe, temporarily, to my formatting biases. Current code formatting is like seventeenth-century spelling. Everyone does it his own way. There are no universally accepted standards. But the algorithms that check your work when you do the interactive exercises need standards. They can’t grant you the latitude that a human teacher could, because, let’s face it, they aren’t that bright. So I’ve had to settle on certain conventions.
All of the conventions I teach are embraced by a large segment of the coding community, so you’ll be in good company. But that doesn’t mean you’ll be married to my formatting biases forever. When you begin coding projects, you’ll soon develop your own opinions or join an organization that has a stylebook. Until then, I’ll ask you to make your code look like my code.
Email me with any problems or questions. The book and exercises have been tested on many learners, but haven’t been tested on you. If you hit a snag, if you’re just curious about something, or if I’ve found some way to give you fits, email me at [email protected]. I’ll be happy to hear from you. I’ll reply promptly. And, with your help, I’ll probably learn something that improves the next edition.
Chapters
- Alerts
- Variables for Strings
- Variables for Numbers
- Variable Names Legal and Illegal
- Math Expressions: familiar operators
- Math Expressions: unfamiliar operators
- Math Expressions: eliminating ambiguity
- Concatenating text strings
- Prompts
- if statements
- Comparison operators
- if…else and else if statements
- Testing sets of conditions
- if statements nested
- Arrays
- Arrays: adding and removing elements
- Arrays: removing, inserting, and extracting elements
- for loops
- for loops: flags, Booleans, array length, and breaks
- for loops nested
- Changing case
- Strings: measuring length and extracting parts
- Strings: finding segments
- Strings: finding a character at a location
- Strings: replacing characters
- Rounding numbers
- Generating random numbers
- Converting strings to integers and decimals
- Converting strings to numbers, numbers to strings
- Controlling the length of decimals
- Getting the current date and time
- Extracting parts of the date and time
- Specifying a date and time
- Changing elements of a date and time
- Functions
- Functions: passing them data
- Functions: passing data back from them
- Functions: local vs. global variables
- switch statements: how to start them
- switch statements: how to complete them
- while loops
- do…while loops
- Placing scripts
- Commenting
- Events: link
- Events: button
- Events: mouse
- Events: fields
- Reading field values
- Setting field values
- Reading and setting paragraph text
- Manipulating images and text
- Swapping images
- Swapping images and setting classes
- Setting styles
- Target all elements by tag name
- Target some elements by tag name
- The DOM
- The DOM: Parents and children
- The DOM: Finding children
- The DOM: Junk artifacts and nodeType
- The DOM: More ways to target elements
- The DOM: Getting a target’s name
- The DOM: Counting elements
- The DOM: Attributes
- The DOM: Attribute names and values
- The DOM: Adding nodes
- The DOM: Inserting nodes
- Objects
- Objects: Properties
- Objects: Methods
- Objects: Constructors
- Objects: Constructors for methods
- Objects: Prototypes
- Objects: Checking for properties and methods
- Browser control: Getting and setting the URL
- Browser control: Getting and setting the URL another way
- Browser control: Forward and reverse
- Browser control: Filling the window with content
- Browser control: Controlling the window’s size and location
- Browser control: Testing for popup blockers
- Form validation: text fields
- Form validation: drop-downs
- Form validation: radio buttons
- Form validation: ZIP codes
- Form validation: email
- Exceptions: try and catch
- Exceptions: throw
- Handling events within JavaScript
Happy learning!