JAVASCRIPT FOR BEGINNERS
A Complete Beginners Guide To Learn The Fundamentals Of JavaScript, Python, SQL & Java
[Mark Whistler]
Text Copyright [Mark Whistler]
All rights reserved. No part of this guide may be reproduced in any form without permission in writing from the publisher except in the case of brief quotations embodied in critical articles or reviews.
Legal & Disclaimer
The information contained in this book and its contents is not designed to replace or take the place of any form of medical or professional advice; and is not meant to replace the need for independent medical, financial, legal or other professional advice or services, as may be required. The content and information in this book has been provided for educational and entertainment purposes only.
The content and information contained in this book has been compiled from sources deemed reliable, and it is accurate to the best of the Author's knowledge, information and belief. However, the Author cannot guarantee its accuracy and validity and cannot be held liable for any errors and/or omissions. Further, changes are periodically made to this book as and when needed. Where appropriate and/or necessary, you must consult a professional (including but not limited to your doctor, attorney, financial advisor or such other professional advisor) before using any of the suggested remedies, techniques, or information in this book.
Upon using the contents and information contained in this book, you agree to hold harmless the Author from and against any damages, costs, and expenses, including any legal fees potentially resulting from the application of any of the information provided by this book. This disclaimer applies to any loss, damages or injury caused by the use and application, whether directly or indirectly, of any advice or information presented, whether for breach of contract, tort, negligence, personal injury, criminal intent, or under any other cause of action.
You agree to accept all risks of using the information presented inside this book.
You agree that by continuing to read this book, where appropriate and/or necessary, you shall consult a professional (including but not limited to your doctor, attorney, or financial advisor or such other advisor as needed) before using any of the suggested remedies, techniques, or information in this book.
Table of Contents
Description
Introduction
HTML is not very smart. It mostly lets people look at text and images and allows them to move to other pages where they will do more of the same. What adds the intelligence to a web page is JavaScript. It makes the website more engaging, effective, and useful by letting pages respond to our visitors when they interact with the content.
This book assumes that we already know how to use HTML to specify web page structure and content. It will be additionally beneficial if we are familiar how pages are styled with CSS, separate from the web page structure. If this is the case then we are ready to add a little behavior to the page and make it more dynamic and interactive with JavaScript. Otherwise, without HTML and CSS, JavaScript will not do us much good. They are viewed as the three fundamental pillars of the web page: structure, presentation and behavior.
Chapter 1: What is JavaScript?
JavaScript is the scripting language of the web with the sole purpose of adding interactivity to our pages. In addition to interactivity, modern versions of JavaScript can also be used to load and parse information from external sources or even the website's users. JavaScript is essentially a piece of programming code embedded in the HTML structure of a web page. When the web browser reads this code it activates a built-in interpreter that understands how to decipher this language and process its commands.
Although programming is involved during coding, JavaScript is not a programming language. In conventional web programming languages, like Java or .NET, the code has to be compiled before it is executed. Compiling means that the code has to be first sent to a special program that is run on the server. This program, also known as application server software, translates the code, creates the requested page and/or functionality and serves this back as HTML. Scripting languages, like JavaScript, are note compiled, but rather are interpreted on-the-fly. This means that no special software is involved as the user's own browser runs and executes the code as it is encountered.
Note: JavaScript was created during a time when Java was a very popular language. Other than that, the languages are not related and have almost nothing in common except for basic programming logic.
Implementing JavaScript
Now that we have a general idea as to what JavaScript is, we can start working with this language. As JavaScript code is part of the HTML document, we need to know how to tell browsers to run our scripts. There are two common options available to us when we want to include JavaScript in a web document and in both cases we will use the element. The tag is used when we want to tell the browser where the JavaScript code begins and where it ends within an HTML document. As such, this tag can be included either in the head or the body section of the page.
The first option is to place the code inline within the document structure. To do this we will begin by opening a tag, entering the JavaScript code, and then finish by closing with the tag. We can theoretically leave the document like this as almost all browsers will assume that the scripting language between the tags is JavaScript by default. Nevertheless, for maximum compatibility we will extend this tag with the type attribute and the text/javascript value in order to instruct the browser how to exactly interpret the code.
//A JavaScript comment
The second option is to load the JavaScript code from an external file into our HTML document. For this purpose we can use the element again, but this time in addition to the type attribute we will also include the URL to the external file in the src attribute of the element. The external file must be a text-only file with the .js file extension that contains only pure JavaScript code without any HTML elements or CSS rules. For example, to call the external scripts.js file into our browser we would use the following code:
We put JavaScript in an external file and include it in the web page when we like to share the functionalities across our entire web site. Otherwise, if we just need to add some local interactive behavior, we embed the code within the page.
Note: Script files are loaded in the order in which they are placed in the HTML code
Chapter 2: Fundamental JavaScript Concepts
Generally, when we hear the term programming we immediately think of other people typing an incomprehensible string of letters and numbers. Programming looks like magic beyond the realm of mere mortals. Nevertheless, the concepts in programming are not difficult to grasp as they always have real life applications. JavaScript, although it is not as simple as HTML or CSS, is not an overly complicated language. Unlike other languages, its "grammar" is more or less descriptive and intuitive making it a good fit for a first programming language. Basically, learning JavaScript is like learning a new language, but a new language that is similar to English. Once we learn the new words, and understand how to put them together to form "sentences" we'll be good to go.