JavaScript Essentials For Dummies
()
About this ebook
The concise and digestible get-started guide to JavaScript programming
JavaScript Essentials For Dummies is your quick reference to all the core concepts about JavaScript—the dynamic scripting language that is often the final step in creating powerful websites. This no-nonsense book gets right to the point, eliminating review material, wordy explanations, and fluff. Find out all you need to know about the foundations of JavaScript, swiftly and crystal clear. Perfect for a brush-up on the basics or as an everyday desk reference on the job, this is the reliable little book you can always turn to for answers.
- Get a quick and thorough intro to the basic concepts of coding with JavaScript
- Review what you've already learned or pick up essential new skills
- Add interactive features to websites with JavaScript programming
- Keep this concise reference book handy for jogging your memory as you work
This book is to the point, focusing on the key topics you need to know about this popular programming language. Great for supplementing classroom learning, reviewing for a certification, or staying knowledgeable on the job.
Paul McFedries
Paul McFedries has written nearly 100 books, which have sold over four million copies world-wide
Read more from Paul Mc Fedries
The Complete Idiot's Guide to Google Chrome and Chrome OS Rating: 2 out of 5 stars2/5HTML, CSS, & JavaScript All-in-One For Dummies Rating: 0 out of 5 stars0 ratingsData Analytics & Visualization All-in-One For Dummies Rating: 0 out of 5 stars0 ratingsExcel All-in-One For Dummies Rating: 0 out of 5 stars0 ratingsAlexa For Dummies Rating: 0 out of 5 stars0 ratingsWeb Coding & Development All-in-One For Dummies Rating: 1 out of 5 stars1/5Excel Data Analysis For Dummies Rating: 0 out of 5 stars0 ratingsThe Complete Idiot's Guide to Weird Word Origins: Over 500 Strange Stories of the World’s Oddest Words and Phrases Rating: 0 out of 5 stars0 ratingsExcel Workbook For Dummies Rating: 4 out of 5 stars4/5The Complete Idiot's Guide to Windows 8: Master the All New Microsoft Operating System Rating: 4 out of 5 stars4/5G Suite For Dummies Rating: 5 out of 5 stars5/5HTML & CSS Essentials For Dummies Rating: 0 out of 5 stars0 ratingsBuild a Website with ChatGPT: No coding experience necessary Rating: 0 out of 5 stars0 ratingsAmazon Fire TV For Dummies Rating: 0 out of 5 stars0 ratingsWord Spy: The Word Lover's Guide to Modern Culture Rating: 4 out of 5 stars4/5Microsoft 365 Office All-in-One For Dummies Rating: 0 out of 5 stars0 ratingsThe Complete Idiot's Guide to Using Your Computer - for Seniors Rating: 5 out of 5 stars5/5Cord Cutting For Dummies Rating: 0 out of 5 stars0 ratingsiPad and iPad Pro For Dummies Rating: 0 out of 5 stars0 ratingsMacs All-in-One For Dummies Rating: 0 out of 5 stars0 ratingsWindows 8 Visual Quick Tips Rating: 0 out of 5 stars0 ratingsGoogle Workspace For Dummies Rating: 0 out of 5 stars0 ratingsJava Essentials For Dummies Rating: 0 out of 5 stars0 ratings
Related to JavaScript Essentials For Dummies
Related ebooks
JavaScript Unleashed: Harnessing the Power of Web Scripting Rating: 0 out of 5 stars0 ratingsJavascript Concepts: 1St Edition Rating: 0 out of 5 stars0 ratingsJavaScript for Beginners Rating: 5 out of 5 stars5/5Professional JavaScript for Web Developers Rating: 0 out of 5 stars0 ratingsLearning jQuery Rating: 4 out of 5 stars4/5Instant IntroJs Rating: 0 out of 5 stars0 ratingsJust the basics of JavaScript Rating: 0 out of 5 stars0 ratingsReact Key Concepts: An in-depth guide to React's core features Rating: 0 out of 5 stars0 ratingsJump Start Web Performance Rating: 0 out of 5 stars0 ratings10 Lessons in Front-end Rating: 2 out of 5 stars2/5Learn Javascript In 1 Hour Rating: 0 out of 5 stars0 ratingsThe Art of JavaScript Performance Optimization: From Theory to Practice Rating: 0 out of 5 stars0 ratingsJasmine JavaScript Testing - Second Edition Rating: 0 out of 5 stars0 ratingsjQuery Design Patterns Rating: 0 out of 5 stars0 ratingsPractical Programming 6 in 1: Python Machine Learning, JavaScript, React 17, And Angular With Typescript Rating: 0 out of 5 stars0 ratingsModern Full-Stack React Projects: Build, maintain, and deploy modern web apps using MongoDB, Express, React, and Node.js Rating: 0 out of 5 stars0 ratingsHTML & CSS Essentials For Dummies Rating: 0 out of 5 stars0 ratingsFull Stack Development Explained: From Frontend to Backend Rating: 0 out of 5 stars0 ratingsBuilding the Future: Advanced Web Development Techniques with Flask and Python Rating: 0 out of 5 stars0 ratingsCSS Mastery: Styling Web Pages Like a Pro Rating: 0 out of 5 stars0 ratingsMicrosoft 365 Access For Dummies Rating: 0 out of 5 stars0 ratingsMicrosoft .NET Framework 4.5 Quickstart Cookbook Rating: 0 out of 5 stars0 ratingsHTML5 Web Application Development By Example Beginner's guide Rating: 0 out of 5 stars0 ratingsFrom Zero to Market with Flutter Rating: 0 out of 5 stars0 ratingsUltimate Node.js for Cross-Platform App Development Rating: 0 out of 5 stars0 ratings
Programming For You
Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Python: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Linux: Learn in 24 Hours Rating: 5 out of 5 stars5/5Microsoft Azure For Dummies Rating: 0 out of 5 stars0 ratingsSQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5C All-in-One Desk Reference For Dummies Rating: 5 out of 5 stars5/5JavaScript All-in-One For Dummies Rating: 5 out of 5 stars5/5Beginning Programming with C++ For Dummies Rating: 4 out of 5 stars4/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Learn SQL in 24 Hours Rating: 5 out of 5 stars5/5Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS Rating: 5 out of 5 stars5/5Excel 101: A Beginner's & Intermediate's Guide for Mastering the Quintessence of Microsoft Excel (2010-2019 & 365) in no time! Rating: 0 out of 5 stars0 ratingsLearn NodeJS in 1 Day: Complete Node JS Guide with Examples Rating: 3 out of 5 stars3/5PYTHON: Practical Python Programming For Beginners & Experts With Hands-on Project Rating: 5 out of 5 stars5/5The 1 Page Python Book Rating: 2 out of 5 stars2/5Python Data Structures and Algorithms Rating: 5 out of 5 stars5/5Mastering JavaScript: The Complete Guide to JavaScript Mastery Rating: 5 out of 5 stars5/5
Reviews for JavaScript Essentials For Dummies
0 ratings0 reviews
Book preview
JavaScript Essentials For Dummies - Paul McFedries
Introduction
HTML and CSS are awesome technologies, and you can use them to create pages that look amazing. But after you funnel your page to your web server and look at it a few (dozen) times, you may notice a subtle feeling of disappointment creeping in. Why? It can be hard to pin down, but that hint of dismay comes from a stark fact: Your web page just kind of sits there.
Sure, you probably have a link or three to click, but most likely those links just take you to more of your pages that also just kind of sit there. Or maybe a link takes you to another site altogether, one that feels dynamic and alive and interactive. Ah, engagement! Ooh, excitement!
What’s the difference between a page that does nothing and a page that seems to be always dancing? One word: JavaScript. If you want your pages to be dynamic and interactive, you need a bit of behind-the-scenes JavaScript to make it so.
But,
I hear you object, HTML isn’t that hard to learn. JavaScript is a programming language, for crying out loud!
I hear you. It’s true that anyone can learn HTML as long as they start with the basic tags, examine lots of examples of how they work, and slowly work their way up to more complex pages. It’s just a matter of creating a solid foundation and then building on it.
I’m convinced that JavaScript can be approached in much the same way. I’m certainly not going to tell you that JavaScript is as easy to learn as HTML. That would be a bald-faced lie. However, I will tell you that there is nothing inherently difficult about JavaScript. I believe that if you begin with the basic syntax and rules, study tons of examples to learn how they work, and then slowly build up to more complex scripts, you can learn JavaScript programming. I predict here and now that by the time you finish this book, you’ll even be a little bit amazed at yourself and at what you can do.
About This Book
Welcome, then, to JavaScript Essentials For Dummies. This book gives you a solid education on the standard programming language underlying the World Wide Web. You learn how to set up the tools you need and, given any web pages you have (or someone else has) built with HTML and CSS, you learn how to use JavaScript to program those pages. My goal is to show you that adding a sprinkling of JavaScript magic to a page isn’t hard to learn, and that even the greenest rookie programmer can learn how to create dynamic and interactive web pages that will amaze their family and friends (and themselves).
If you’re looking for lots of programming history, computer science theory, and long-winded explanations of concepts, I’m sorry, but you won’t find it here. My philosophy throughout this book comes from Linus Torvalds, the creator of the Linux operating system: Talk is cheap. Show me the code.
I explain what needs to be explained and then I move on without further ado (or, most of the time, without any ado at all) to examples and scripts that do more to illuminate a concept that any verbose explanations I could muster (and believe me, I can muster verbosity with the best of them).
Foolish Assumptions
This book is not a primer on the internet or on using the World Wide Web. This is a book on coding web pages, pure and simple. This means I assume the following:
You know how to operate a basic text editor, and how to get around the operating system and file system on your computer.
You have an internet connection.
You know how to use your web browser.
You know the basics of HTML and CSS.
Yep, that’s it.
Icons Used in This Book
Remember This icon points out juicy tidbits that are likely to be repeatedly useful to you — so please don’t forget them.
Tip Think of these icons as the fodder of advice columns. They offer (hopefully) wise advice or a bit more information about a topic under discussion.
Warning Look out! In this book, you see this icon when I’m trying to help you avoid mistakes that can cost you time, money, or embarrassment.
Where to Go From Here
How you approach this book depends on your current level of coding and/or JavaScript expertise (or lack thereof):
If you’ve never programmed before, begin at the beginning with Chapter 1 and work at your own pace sequentially through Chapters 2, 3, 4, and 5. This will give you all the knowledge you need to pick and choose what you want to learn throughout the rest of the book.
If you’ve done some non-JavaScript programming, start with Chapter 1, skim through Chapters 2 through 5 to see how JavaScript does the standard programming tasks, and then pick and choose your topics from there.
If you’ve done some JavaScript coding already, I suggest working quickly through the material in Chapters 2 through 5, and then diving into the all-important material on the Document Object Model in Chapter 6. From there, you can peruse the rest of the chapters as you see fit.
Chapter 1
JavaScript: The Big Picture
IN THIS CHAPTER
Bullet Getting a feel for programming in general, and JavaScript in particular
Bullet Checking out the tools you need to get coding
Bullet Adding comments to your JavaScript code
Bullet Storing your code in a separate JavaScript file
In this chapter, you explore some useful JavaScript basics. Don’t worry if you’ve never programmed before. I take you through everything you need to know, step-by-step, nice and easy. As you’re about to find out, it really is fun to program.
Adding JavaScript Code to a Web Page
Okay, it’s time to roll up your sleeves, crack your knuckles, and start coding. This section describes the standard procedure for constructing and testing a script and takes you through a couple of examples.
The
The basic container for a script is, naturally enough, the HTML end tag:
JavaScript statements go here
Where do you put the
With certain exceptions, it doesn’t matter a great deal where you put your
en>
utf-8>
JavaScript statements go here
Here are the exceptions to the put-your-script-anywhere technique:
If your script is designed to write data to the page, the
If your script refers to an item on the page (such as a form object), the script must be placed after that item.
With many HTML tags, you can add one or more JavaScript statements as attributes directly within the tag.
Remember It’s perfectly acceptable to insert multiple end tag, and as long as you don’t put one
Example #1: Displaying a message to the user
You’re now ready to construct and try out your first script. This example shows you the simplest of all JavaScript actions: displaying a basic message to the user. The following code shows the script within an HTML file:
en>
utf-8>
alert(Hello JavaScript World!
);
As shown in here, place the script within the header of a page, save the file, and then open the HTML file within your browser.
This script consists of just a single line:
alert(Hello JavaScript World!
);
This is called a statement, and each statement is designed to perform a single JavaScript task. Your scripts will range from simple programs with just a few statements to huge projects consisting of hundreds of statements.
You may be wondering about the semicolon (;) that appears at the end of the statement. Good eye. You use the semicolon to mark the end of each of your JavaScript statements.
In the example, the statement runs the JavaScript alert() method, which displays to the user whatever message is enclosed within the parentheses (which could be a welcome message, an announcement of new features on your site, an advertisement for a promotion, and so on). Figure 1-1 shows the message that appears when you open the file.
A snapshot of an alert message. The message reads, paulmcfederies.com says Hello JavaScript World. The OK option is given below.FIGURE 1-1: This alert
message appears when you open the HTML file containing the example script.
How did the browser know to run the JavaScript statement? When a browser processes (parses, in the vernacular) a page, it basically starts at the beginning of the HTML file and works its way down, one line at a time. If it trips over a function, which I explain in Chapter 5.
Warning One of the cardinal rules of JavaScript programming is one statement, one line.
That is, each statement must appear on only a single line, and there should be no more than one statement on each line. I said should
in the second part of the previous sentence because it is possible to put multiple statements on a single line, as long as you separate each statement with a semicolon (;). There are rare times when it’s necessary to have two or more statements on one line, but you should avoid it for the bulk of your programming because multiple-statement lines are difficult to read and to troubleshoot.
Example #2: Writing text to the page
One of JavaScript’s most powerful features is the capability to write text and even HTML tags and CSS rules to the web page on-the-fly. That is, the text (or whatever) gets inserted into the page when a web browser loads the page. What good is that? For one thing, it’s ideal for time-sensitive data. For example, you may want to display the date and time that a web page was last modified so that visitors know how old (or new) the page is. Here’s some code that shows just such a script:
en>
utf-8>
This is a regular line of text.
document.write(Last modified:
+ document.lastModified)