This book is a guide to JavaScript, a scripting language widely used in web pages and web applications such as email applications. JavaScript is not to be confused with Java, which is quite a separate language for creating stand-alone applications.
This book applies to version 1.6 and later of JavaScript. Multiple versions of JavaScript are covered in this book.
Conditions of Use
This book is licensed under a Creative Commons License (CC BY-NC-SA). You can download the ebook JavaScript by Wikibooks for free.
- Title
- JavaScript by Wikibooks
- Subtitle
- Scripting for the web
- Publisher
- wikibooks.org
- Published
- 2022-12-08
- Edition
- 1
- Format
- eBook (pdf, epub, mobi)
- Pages
- 193
- Language
- English
- License
- CC BY-NC-SA
- Book Homepage
- Free eBook, Errata, Code, Solutions, etc.
1 Introduction 1.1 Dynamic data types 1.2 Functional programming 1.3 Object-orientated programming 1.4 C-like syntax 1.5 Relation to Java 1.6 JS engines 1.7 References 2 First program 2.1 Exercises 3 Placing the code 3.1 Internal vs. external JavaScript 3.1.1 Separate Files for Javascript Code 3.1.2 Remote Code Injection vs. Local Library 3.1.3 Minified vs. Non-Minified Code 3.2 External JavaScript 3.2.1 The src attribute 3.2.2 The type attribute 3.2.3 The async and defer attributes 3.3 Location of elements 3.4 The element 3.5 JavaScript in XHTML files 3.6 Reference 4 Other Script Types 4.1 References 5 Lexical structure 5.1 Summary 5.2 Case Sensitivity 5.3 Whitespaces 5.4 Comments 5.5 Semicolons 5.6 Literals 5.7 Identifiers 5.8 Exercises 5.9 Notes 5.10 References 6 Automatic semicolon insertion 6.1 See also 7 Reserved words 7.1 References 8 Variables and types 8.1 Purpose 8.2 Declaration and initialization 8.2.1 Keyword let 8.2.2 Keyword const 8.2.3 Keyword var 8.2.4 Omitting the declaration 8.3 Data types 8.4 Scope 8.4.1 Block scope 8.4.2 Function scope 8.4.3 Module scope 8.4.4 Global scope 8.5 Exercises 8.6 See also 8.7 References 9 Numbers 9.1 String 9.2 Properties and methods for strings 9.2.1 length 9.2.2 concat(text) 9.2.3 indexOf(searchText) 9.2.4 lastIndexOf(searchText) 9.2.5 replace(text, newtext) 9.2.6 slice(start [, end]) 9.2.7 substr(start [, number of characters]) 9.2.8 substring(start [, end]) 9.2.9 toLowerCase() 9.2.10 toUpperCase() 9.3 Number 9.4 Properties and methods for numbers 9.4.1 Properties 9.4.2 Math.ceil(number) 9.4.3 Math.floor(number) 9.4.4 Math.round(number) 9.4.5 Math.max(number_1, number_2) 9.4.6 Math.min(number_1, number_2) 9.4.7 Math.random() 9.4.8 Number.parseInt(string) 9.4.9 Number.parseFloat(string) 9.5 BigInt 9.6 Boolean 9.7 Undefined 9.8 Null 9.9 Symbol 9.10 See also 9.11 Exercises 9.12 References 10 Strings 10.1 String 10.2 Properties and methods for strings 10.2.1 length 10.2.2 concat(text) 10.2.3 indexOf(searchText) 10.2.4 lastIndexOf(searchText) 10.2.5 replace(text, newtext) 10.2.6 slice(start [, end]) 10.2.7 substr(start [, number of characters]) 10.2.8 substring(start [, end]) 10.2.9 toLowerCase() 10.2.10 toUpperCase() 10.3 Number 10.4 Properties and methods for numbers 10.4.1 Properties 10.4.2 Math.ceil(number) 10.4.3 Math.floor(number) 10.4.4 Math.round(number) 10.4.5 Math.max(number_1, number_2) 10.4.6 Math.min(number_1, number_2) 10.4.7 Math.random() 10.4.8 Number.parseInt(string) 10.4.9 Number.parseFloat(string) 10.5 BigInt 10.6 Boolean 10.7 Undefined 10.8 Null 10.9 Symbol 10.10 See also 10.11 Exercises 10.12 References 11 Dates 11.1 Constructor 11.2 Methods 11.3 'As Integer' 11.4 Timezones 11.5 New API: Temporal 11.6 Exercises 12 Arrays 12.1 Create an array 12.2 Access an array element 12.3 Varying data types 12.4 Nested arrays 12.5 Properties and methods 12.5.1 length 12.5.2 concat 12.5.3 join and split 12.5.4 push 12.5.5 pop 12.5.6 unshift 12.5.7 shift 12.6 Exercises 12.7 See also 13 Regular expressions 13.1 Overview 13.2 Compatibility 13.3 Examples 13.4 Modifiers 13.5 Operators 13.6 Function call 13.7 See also 13.8 External links 14 Operators 14.1 String concatenation 14.2 Arithmetic operators 14.3 Bitwise operators 14.4 Assignment operators 14.5 Increment operators 14.5.1 Pre and post-increment operators 14.6 Comparison operators 14.7 Logical operators 14.8 Other operators 14.8.1 ? : 14.8.2 delete 14.8.3 new 14.8.4 instanceof 14.8.5 typeof 14.9 Exercises 14.10 See also 15 Control structures 15.1 if / else 15.2 switch 15.3 try / catch / finally 15.4 throw 15.5 Exercises 15.6 Loops 15.7 See also 16 Loops 16.1 for (;;) {} 16.1.1 Optional syntax parts 16.1.2 Nesting 16.2 continue / break 16.3 do {} while () 16.4 while () {} 16.5 for (x in Object) {} 16.6 for (x of Array) {} 16.7 for..in vs. for..of 16.8 Object.entries() method 16.9 Array.forEach() method 16.10 Exercises 16.11 See also 17 Functions 17.1 Declaration 17.2 Invocation 17.2.1 Hoisting 17.3 Immediately Invoked Function 17.4 Arguments 17.4.1 Call-by-value 17.4.2 Default values 17.4.3 Variable number of arguments 17.4.3.1 Individual checks 17.4.3.2 The 'rest' parameter 17.4.3.3 The 'arguments' keyword 17.5 Return 17.6 Arrow functions (=>) 17.7 Recursive Invokations 17.8 Exercises 17.9 See also 18 Anonymous functions 18.1 Declaration 18.2 Invocation 18.2.1 Hoisting 18.3 Immediately Invoked Function 18.4 Arguments 18.4.1 Call-by-value 18.4.2 Default values 18.4.3 Variable number of arguments 18.4.3.1 Individual checks 18.4.3.2 The 'rest' parameter 18.4.3.3 The 'arguments' keyword 18.5 Return 18.6 Arrow functions (=>) 18.7 Recursive Invokations 18.8 Exercises 18.9 See also 19 Object-based programming 19.1 Class-based OOP 19.2 Prototype-based OOP 19.3 OOP in JavaScript "Two jackets for one body" 19.3.1 The classical syntax 19.3.2 The 'class' syntax 19.4 See also 20 Objects 20.1 Create an object 20.2 Read a property 20.3 Add or modify a property 20.4 Delete a property 20.5 Merge objects 20.6 Functions/methods as part of an object 20.7 Exercises 21 Constructors and prototypes 21.1 Constructor 21.2 Prototypes 21.3 Dynamically extending objects 21.4 References 22 Inheritance 22.1 instanceof operator 22.2 Inheritance by prototypes 22.3 Inheritance by functions 22.4 References 23 Access control 23.1 Access Control 24 Closures 24.1 Lexical environment 24.2 Closure 24.3 Exercises 24.4 See also 25 Debugging 25.1 JavaScript Debuggers 25.1.1 Firebug 25.1.2 Venkman JavaScript Debugger 25.1.3 Internet Explorer debugging 25.1.4 Safari debugging 25.1.5 JTF: JavaScript Unit Testing Farm 25.1.6 jsUnit 25.1.7 built-in debugging tools 25.2 Common Mistakes 25.3 Debugging Methods 25.3.1 Following Variables as a Script is Running 25.4 Browser Bugs 25.5 browser-dependent code 25.6 Further reading 25.7 References 26 Optimization 26.1 JavaScript optimization 26.1.1 Optimization Techniques 26.2 Common Mistakes and Misconceptions 26.2.1 String concatenation 27 Strict Mode 27.1 The strict mode 27.2 External links 28 Shell 28.1 Standalone 28.2 From browser 28.3 External links 29 Introduction to the Document Object Model (DOM) 29.1 Nodes 29.2 Accessing nodes 29.3 Accessing content 29.4 Changing content 29.5 Modifying the tree structure 29.6 See also 30 Runtime document manipulation 30.1 Nodes 30.2 Accessing nodes 30.3 Accessing content 30.4 Changing content 30.5 Modifying the tree structure 30.6 See also 31 Finding elements 31.1 Using ID 31.2 Using tag name 31.3 Using class name 31.4 Using a query selector 31.5 Navigating the DOM tree 31.6 See also 31.7 Exercises 32 Adding elements 32.1 Creating elements 32.2 Creating attributes 32.2.1 Alternative syntax 32.3 Join the puzzles pieces 32.4 'Misusing' innerHTML 32.5 write() 32.6 See also 32.7 Exercises 33 Changing elements 33.1 Example page 33.2 Change the content 33.3 Change an attribute 33.3.1 setAttribute() 33.4 See also 33.5 Exercises 34 Removing elements 34.1 Remove elements 34.1.1 Children of children 34.1.2 parentNode 34.2 Remove attributes 34.3 See also 35 Changing element styles 35.1 An example 35.2 Properties of 'style' 35.3 Exercises 35.4 See also 36 Event handling 36.1 Create and invoke events 36.1.1 Embedded in HTML 36.1.2 Programmatically in JavaScript 36.2 Event types 36.3 Event properties 36.4 removeEventListener 36.5 Synthetic events 36.6 (A)synchronous behaviour 36.7 Exercises 36.8 See also 37 W3C event handlers 38 Working with the mouse 38.1 JavaScript Console Tests 38.2 Test x,y 38.3 Test offsetX,offsetY 38.4 Test clientX,clientY 38.5 Test pageX,pageY 38.6 References 38.7 Further reading 39 Forms 39.1 Further reading 39.2 References 40 Images 40.1 Introduction 40.1.1 Image Tag 40.1.2 Canvas Tag 40.2 Workflow 40.3 Assignment 40.3.1 Adding an Image to HTML Page 40.3.2 Assigning the Image Variable 40.3.3 Error Handling for Image Assignment 40.4 Processing 40.4.1 Images and Canvas Tags 40.4.2 Javascript Function to merge Images 40.4.3 Button to Execute Image Merge 40.5 Load/Save 41 Cookies 41.1 Intoduction 42 LocalStorage 42.1 Introduction 42.2 Activities 43 Bookmarklets 43.1 JavaScript URI scheme 43.2 Example uses 43.2.1 Media controls 43.3 Using multiple lines of code 43.4 The JavaScript Protocol in Links 43.5 Examples 44 XMLHttpRequest 44.1 Example 44.2 Object reference 44.2.1 Methods 44.2.2 Properties 44.3 Bugs and inconsistencies 44.3.1 Caching 44.3.1.1 Workaround 44.3.2 Reusing XMLHttpRequest Object in IE 44.4 Links 44.5 Further reading 45 Handling HTML 45.1 Using the browser's HTML parser 46 Handling JSON 46.1 Native JSON 46.1.1 Modern JSON Handling 46.1.2 Old way 46.2 JSONP 46.3 More information 47 Handling XML 47.1 Simple function to open an XML file 47.2 Usage 48 Working with files 48.1 References 49 JScript in Microsoft WSH 50 Index 50.1 A 50.2 B 50.3 C 50.4 D 50.5 E 50.6 F 50.7 H 50.8 I 50.9 L 50.10 M 50.11 N 50.12 O 50.13 R 50.14 S 50.15 T 50.16 U 50.17 V 51 Links 52 Bookmarklets 52.1 JavaScript URI scheme 52.2 Example uses 52.2.1 Media controls 52.3 Using multiple lines of code 52.4 The JavaScript Protocol in Links 52.5 Examples 53 Useful software tools 53.1 Editors / IDEs 53.2 Engines and other tools 54 History of JavaScript 54.1 Versions of JavaScript 54.2 ECMAScript 55 Best practices 55.1 document.write 55.2 JavaScript protocol 55.3 Email validation 55.3.1 Examples valid according to RFC2822 55.3.2 Examples invalid according to RFC2822s 55.3.3 Test page 55.4 use strict 55.5 For further reading 55.5.1 Best practices in other languages 55.6 References 56 Code structuring 57 Generators 57.1 Examples 57.2 Parameters 57.3 References 58 Contributors
Related Books