Head First JavaScript Programming, 2nd Edition

Book description

What will you learn from this book?

Now in its second edition, this brain-friendly guide is your comprehensive journey into modern JavaScript, covering everything from the core language fundamentals to cutting-edge features that define JavaScript today. You'll dive into the nuances of JavaScript types and the unparalleled flexibility of its functions. You'll learn how to expertly navigate classes and objects, and you'll finally understand closures. But that's just the beginning—you'll also get hands-on with the browser's document object model (DOM), engaging with JavaScript in ways you've only imagined. You won't just be reading—you'll be playing games, solving puzzles, pondering mysteries, and interacting with JavaScript. And you'll write real code, lots of it, so you can start building your own web applications.

What's so special about this book?

If you've read a Head First book, you know what to expect: a visually rich format designed for the way your brain works. If you haven't, you're in for a treat. With this book, you'll learn JavaScript through a multisensory experience that engages your mind—rather than a text-heavy approach that puts you to sleep.

Publisher resources

View/Submit Errata

Table of contents

  1. Brief Table of Contents (Not Yet Final)
  2. 1. A Quick Dip into Javascript: Getting your Feet Wet
    1. The way JavaScript works
    2. How you’re going to write JavaScript
    3. How to get JavaScript into your page
    4. A little test drive
    5. JavaScript, you’ve come a long way...
    6. How to make a statement
    7. Variables and values
    8. Constants, another kind of variable
    9. Back away from that keyboard!
    10. Express yourself
    11. Doing things more than once
    12. How the while loop works
    13. Making decisions with JavaScript
    14. And, when you need to make LOTS of decisions
    15. Reach out and communicate with your user
      1. Create an alert
      2. Write directly into your document
      3. Use the console
      4. Directly manipulate your document
    16. A closer look at console.log
    17. Opening the console
    18. Coding a Serious JavaScript Application
    19. How do I add code to my page? (let me count the ways)
    20. We’re going to have to separate you two
    21. JavaScript cross
    22. JavaScript cross Solution
  3. 2. Writing Real Code: Going Further
    1. Let’s build a Battleship game
    2. Our first attempt...
      1. ... a simplified Battleship
    3. First, a high-level design
    4. A few more details...
      1. Representing the ships
      2. Getting user input
      3. Displaying the results
    5. Working through the Pseudocode
    6. Oh, before we go any further, don’t forget the HTML!
    7. Writing the Simple Battleship code
    8. Now let’s write the game logic
    9. Step One: setting up the loop, getting some input
    10. How prompt works
    11. Checking the user’s guess
    12. So, do we have a hit?
    13. Adding the hit detection code
    14. Hey, you sank my battleship!
    15. Provide some post-game analysis
    16. And that completes the logic!
    17. Doing a little Qualit y Assurance
    18. Can we talk about your verbosit y...
    19. Finishing the Simple Battleship game
    20. How to assign random locations
    21. The recipe for generating a random number
    22. Back to do a little more QA
    23. Congrats on your first true JavaScript program, and a short word about reusing code
    24. JavaScript cross
    25. JavaScript cross Solution
  4. 3. Introducing Functions: Getting functional
    1. What’s wrong with the code anyway?
    2. By the way, did we happen to mention FUNCTIONS?
    3. Okay, but how does it actually work?
    4. Code Magnets
    5. What can you pass to a function?
    6. JavaScript is pass-by-value
      1. That means pass-by-copy
    7. Weird Functions
    8. Functions can return things too
    9. Tracing through a function with a return statement
    10. Global and local variables
      1. Know the difference or risk humiliation
    11. Knowing the scope of your local and global variables
    12. There’s more to the story: any block can act as the scope of a variable
    13. Don’t forget to declare your locals!
    14. The short lives of variables
    15. The Thing-A-Ma-Jig
    16. Who am I?
    17. Five Minute Mystery
    18. JavaScript cross
    19. Code Magnets Solution
    20. The Thing-A-Ma-Jig
    21. Five Minute Mystery Solution
    22. Who am I? Solution
    23. JavaScript cross Solution
  5. 4. Putting some order in your Data: Arrays
    1. Can you help Bubbles-R-Us?
    2. How to represent multiple values in JavaScript
    3. How arrays work
      1. How to create an array
    4. How to access an array item
    5. Updating a value in the array
    6. How big is that array anyway?
    7. The Phrase-O-Matic
    8. Meanwhile, back at Bubbles-R-Us...
    9. Cubicle Conversation
    10. How to iterate over an array
    11. But wait, there’s a better way to iterate over an array
    12. Test drive the bubble report
    13. Fireside Chats
    14. It’s that time again.... Can we talk about your verbosity?
    15. Redoing the for loop with the post-increment operator
    16. Quick test drive
    17. Cubicle Conversation Continued...
    18. Creating an array from scratch (and adding to it)
    19. Test drive the final report
    20. And the winners are...
    21. A quick survey of the code...
    22. Writing the printAndGetHighScore function
    23. Refactoring the code using printAndGetHighScore
    24. Putting it all together...
    25. JavaScript cross
    26. JavaScript cross Solution
  6. 5. Understanding Objects: A trip to Objectville
    1. Did someone say “Objects”?!
    2. Thinking about properties...
    3. How to create an object
    4. What is Object-Oriented Anyway?
    5. How properties work
    6. How does a variable hold an object? Inquiring minds want to know...
    7. Behind the Scenes
    8. Comparing primitives and objects
    9. Behind the Scenes
      1. Initializing a primitive variable
      2. Initializing an object (a reference) variable
    10. Doing even more with objects...
    11. Doing some pre-qualification
    12. Does the taxi cut it?
    13. Stepping through pre-qualification
    14. Let’s talk a little more about passing objects to functions
    15. Putting Fido on a diet....
    16. The Auto-O-Matic
    17. The Auto-O-Matic
    18. Oh Behave! Or, how to add behavior to your objects
    19. Improving the drive method
    20. Take the fiat for a test drive
    21. Uh oh, not so fast...
    22. Why doesn’t the drive method know about the started property?
    23. A test drive with “this”
    24. How this works
    25. Method shorthand
    26. How behavior affects state... Adding some Gas-o-line
    27. Now let’s affect the behavior with the state
    28. Gas up for a test drive
    29. Congrats on your first objects!
    30. Guess what? There are objects all around you! (and they’ll make your life easier)
    31. Crack the Code Challenge
    32. JavaScript cross
    33. JavaScript cross Solution
  7. 6. Interacting with your web page: Getting to know the DOM
    1. In our last chapter, we left you with a little challenge. The “crack the code challenge.”
    2. So what does the code do?
    3. A quick recap
    4. How JavaScript really interacts with your page
    5. How to bake your very own DOM
    6. A first taste of the DOM
    7. Getting an element with getElementById
    8. What, exactly, am I getting from the DOM?
    9. Finding your inner HTML
    10. What happens when you change the DOM
    11. A test drive around the planets
    12. Don’t even think about running my code until the page is fully loaded!
      1. Let’s try that again...
    13. You say “event handler,” I say “callback”
    14. Why stop now? Let’s take it further
      1. How to set an attribute with setAttribute
      2. More fun with attributes! (you can GET attributes too)
      3. Meanwhile, back at the solar system...
    15. So what else is a DOM good for anyway?
    16. JavaScript cross
    17. JavaScript cross Solution
  8. 7. Types, Equality, Conversion and all that Jazz: Serious Types
    1. The truth is out there...
    2. Who am I?
    3. Watch out, you might bump into undefined when you aren’t expecting it...
    4. How to use null
    5. Believe It or Not!! The Number that isn’t a Number
    6. Dealing with NaN
    7. It gets even weirder
    8. We have a confession to make
    9. Understanding the equality operator (other wise known as ==)
      1. If the two values have the same type, just compare them
      2. If the two values have different types, try to convert them into the same type and then compare them
    10. How equality converts its operands (sounds more dangerous than it actually is)
    11. How to get strict with equality
      1. Two values are strictly equal only if they have the same type and the same value.
    12. Even more type conversions...
      1. Another look at concatenation, and addition
      2. What about the other arithmetic operators?
    13. How to determine if two objects are equal
      1. When we test equality of two object variables, we compare the references to those objects
      2. Two references are equal only if they reference the same object
    14. The truthy is out there...
    15. What JavaScript considers falsey
    16. The Secret Life of Strings
    17. How a string can look like a primitive and an object
    18. How template literals work
    19. A five-minute tour of string properties and methods
    20. String Soup
    21. Chair Wars
      1. In Larry’s cube
      2. In Brad’s cube
      3. But wait! There’s been a spec change.
      4. Back in Larry’s cube
      5. At Brad’s laptop at the beach
      6. Larry snuck in just ahead of Brad.
      7. The suspense is killing me. Who got the chair?
    22. JavaScript cross
  9. 8. Bringing it all Together: Building an app
    1. This time, let’s build a REAL Battleship game
    2. Stepping back... to HTML and CSS
    3. Creating the HTML page: the Big Picture
    4. Step 1: The Basic HTML
    5. Step 2: Creating the table
    6. Step 3: Player interaction
    7. Adding some more style
    8. Step 4: Placing the hits and misses
    9. Using the hit and miss classes
    10. How to design the game
    11. Implementing the View
    12. How displayMessage works
    13. Implementing displayMessage
    14. How displayHit and displayMiss work
    15. Implementing displayHit and displayMiss
    16. Another Test Drive...
    17. The Model
    18. How the model interacts with the view
    19. You’re gonna need a bigger boat... and game board
    20. How we’re going to represent the ships
    21. Ship Magnets
    22. Implementing the model object
    23. Thinking about the fire method
    24. Setting up the fire method
    25. Looking for hits
    26. Putting that all together...
    27. Wait, can we talk about your verbosity again?
    28. Meanwhile back at the battleship...
    29. A view to a kill...
    30. Implementing the Controller
    31. Processing the player’s guess
    32. Planning the code...
    33. Implementing parseGuess
    34. Meanwhile back at the controller...
    35. Counting guesses and firing the shot
    36. Game over?
    37. Getting a player’s guess
    38. How to add an event handler to the Fire! button
    39. Getting the player’s guess from the form
    40. Passing the input to the controller
    41. What’s left? Oh yeah, darn it, those hardcoded ships!
    42. Code Magnets
    43. How to place ships
      1. The generateShipLocations function
    44. Writing the generateShip method
    45. Generate the starting location for the new ship
    46. Completing the generateShip method
    47. Avoiding a collision!
    48. Two final changes
    49. Congrats, It’s Startup Time!
    50. JavaScript cross
    51. JavaScript cross Solution
  10. About the Authors

Product information

  • Title: Head First JavaScript Programming, 2nd Edition
  • Author(s): Eric Freeman, Elisabeth Robson
  • Release date: September 2024
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781098147945