www.it-ebooks.info JUMP START JAVASCRIPT BY ARA PEHLIVANIAN & DON NGUYEN www.it-ebooks.info ii Jump Start JavaScript by Ara Pehlivanian and Don Nguyen Copyright © 2013 SitePoint Pty Ltd Product Manager: Simon Mackie English Editor: Kelly Steele Technical Editor: Colin J Ihrig Cover Designer: Alex Walker Notice of Rights All rights reserved No part of this book may be reproduced, stored in a retrieval system or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embodied in critical articles or reviews Notice of Liability The author and publisher have made every effort to ensure the accuracy of the information herein However, the information contained in this book is sold without warranty, either express or implied Neither the authors and SitePoint Pty Ltd., nor its dealers or distributors will be held liable for any damages to be caused either directly or indirectly by the instructions contained in this book, or by the software or hardware products described herein Trademark Notice Rather than indicating every occurrence of a trademarked name as such, this book uses the names only in an editorial fashion and to the benefit of the trademark owner with no intention of infringement of the trademark Published by SitePoint Pty Ltd 48 Cambridge Street Collingwood VIC Australia 3066 Web: www.sitepoint.com Email: business@sitepoint.com ISBN 978-0-9873321-8-9 (print) ISBN 978-0-9873321-9-6 (ebook) Printed and bound in the United States of America www.it-ebooks.info iii About Ara Pehlivanian Ara Pehlivanian has been working on the web since 1997 Most recently, he's worked on high-end, highly visible projects as a web developer and practice lead at Nurun, a front-end engineer at Yahoo! Mail and is currently a JavaScript developer on the HP Cloud Services team About Don Nguyen Like many programmers, Don dabbled in JavaScript for a number years as a secondary language It wasn't until he began implementing server-side projects in Node.js that JavaScript began to take center stage Having been heavily involved in a number of web projects from the back-end all the way to the front, he is now equally at home with JavaScript on the client and on the server He currently spends his time working on building startups from his home base of Sydney, Australia About SitePoint SitePoint specializes in publishing fun, practical, and easy-to-understand content for web professionals Visit http://www.sitepoint.com/ to access our blogs, books, newsletters, articles, and community forums You’ll find a stack of information on JavaScript, PHP, Ruby, mobile development, design, and more About Jump Start Jump Start books provide you with a rapid and practical introduction to web development languages and technologies Typically around 150 pages in length, they can be read in a weekend, giving you a solid grounding in the topic and the confidence to experiment on your own www.it-ebooks.info www.it-ebooks.info To my loving wife Krista, without whose grace, patience and support I would not have been able to write this; and to our two brilliant daughters —Ara To Lorraine, thank you for keeping me nourished with hot food, a warm heart and a beaming smile —Don www.it-ebooks.info www.it-ebooks.info Table of Contents Preface xiii Who Should Read This Book xiii Conventions Used xiii Code Samples xiii Tips, Notes, and Warnings xv Supplementary Materials xv Do you want to keep learning? xvi Setting Up Console Chrome Firefox Internet Explorer Safari Using JavaScript in HTML Files In the HTML File In a Separate File Location of the Tag Summary 2 2 4 5 Chapter Chapter Variables First, a Quick Comment Declarations 10 Types 12 Number 12 String 13 www.it-ebooks.info viii Boolean 13 Undefined 13 Null 13 Object 14 Operations 14 The Dangers of Loose Typing 15 Type Conversion (aka Type Coercion) 15 Comparison Operators 16 Equal (==) 16 Not Equal (!=) 16 Strict Equal (===) 17 Strict Not Equal (!==) 17 Greater than (>) 18 Greater than or Equal to (>=) 18 Less than (