JavaScript Notes for Professionals - JavaScriptNotesForProfessionals
JavaScript Notes for Professionals - JavaScriptNotesForProfessionals
JavaScript
Notes for Professionals
®
400+ pages
of professional hints and tricks
Disclaimer
GoalKicker.com This is an unocial free book created for educational purposes and is
not aliated with ocial JavaScript® group(s) or company(s).
Free Programming Books All trademarks and registered trademarks are
the property of their respective owners
Contents
About ................................................................................................................................................................................... 1
Chapter 1: Getting started with JavaScript ..................................................................................................... 2
Section 1.1: Using console.log() ..................................................................................................................................... 2
Section 1.2: Using the DOM API .................................................................................................................................... 4
Section 1.3: Using window.alert() .................................................................................................................................. 5
Section 1.4: Using window.prompt() ............................................................................................................................. 6
Section 1.5: Using window.confirm() ............................................................................................................................ 7
Section 1.6: Using the DOM API (with graphical text: Canvas, SVG, or image file) ................................................. 8
Chapter 2: JavaScript Variables ......................................................................................................................... 10
Section 2.1: Defining a Variable ................................................................................................................................. 10
Section 2.2: Using a Variable ..................................................................................................................................... 10
Section 2.3: Types of Variables .................................................................................................................................. 10
Section 2.4: Arrays and Objects ................................................................................................................................ 11
Chapter 3: Built-in Constants ................................................................................................................................ 12
Section 3.1: null ............................................................................................................................................................. 12
Section 3.2: Testing for NaN using isNaN() .............................................................................................................. 12
Section 3.3: NaN .......................................................................................................................................................... 13
Section 3.4: undefined and null .................................................................................................................................. 14
Section 3.5: Infinity and -Infinity ................................................................................................................................. 15
Section 3.6: Number constants .................................................................................................................................. 15
Section 3.7: Operations that return NaN ................................................................................................................... 16
Section 3.8: Math library functions that return NaN ................................................................................................ 16
Chapter 4: Comments .............................................................................................................................................. 17
Section 4.1: Using Comments ..................................................................................................................................... 17
Section 4.2: Using HTML comments in JavaScript (Bad practice) ........................................................................ 17
Chapter 5: Console ..................................................................................................................................................... 19
Section 5.1: Measuring time - console.time() ............................................................................................................ 22
Section 5.2: Formatting console output .................................................................................................................... 23
Section 5.3: Printing to a browser's debugging console ......................................................................................... 24
Section 5.4: Including a stack trace when logging - console.trace() ..................................................................... 26
Section 5.5: Tabulating values - console.table() ...................................................................................................... 26
Section 5.6: Counting - console.count() .................................................................................................................... 28
Section 5.7: Clearing the console - console.clear() .................................................................................................. 30
Section 5.8: Displaying objects and XML interactively - console.dir(), console.dirxml() ...................................... 30
Section 5.9: Debugging with assertions - console.assert() ..................................................................................... 32
Chapter 6: Datatypes in JavaScript ................................................................................................................. 33
Section 6.1: typeof ....................................................................................................................................................... 33
Section 6.2: Finding an object's class ........................................................................................................................ 34
Section 6.3: Getting object type by constructor name ............................................................................................ 34
Chapter 7: Strings ....................................................................................................................................................... 37
Section 7.1: Basic Info and String Concatenation ..................................................................................................... 37
Section 7.2: Reverse String ......................................................................................................................................... 38
Section 7.3: Comparing Strings Lexicographically ................................................................................................... 39
Section 7.4: Access character at index in string ....................................................................................................... 40
Section 7.5: Escaping quotes ...................................................................................................................................... 40
Section 7.6: Word Counter .......................................................................................................................................... 41
Section 7.7: Trim whitespace ...................................................................................................................................... 41
Section 7.8: Splitting a string into an array .............................................................................................................. 41
Section 7.9: Strings are unicode ................................................................................................................................. 42
Section 7.10: Detecting a string .................................................................................................................................. 42
Section 7.11: Substrings with slice ............................................................................................................................... 43
Section 7.12: Character code ...................................................................................................................................... 43
Section 7.13: String Representations of Numbers .................................................................................................... 43
Section 7.14: String Find and Replace Functions ...................................................................................................... 44
Section 7.15: Find the index of a substring inside a string ....................................................................................... 45
Section 7.16: String to Upper Case ............................................................................................................................. 45
Section 7.17: String to Lower Case ............................................................................................................................. 46
Section 7.18: Repeat a String ...................................................................................................................................... 46
Chapter 8: Date ........................................................................................................................................................... 47
Section 8.1: Create a new Date object ....................................................................................................................... 47
Section 8.2: Convert to a string format .................................................................................................................... 49
Section 8.3: Creating a Date from UTC ..................................................................................................................... 50
Section 8.4: Formatting a JavaScript date ............................................................................................................... 53
Section 8.5: Get the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC ................................. 55
Section 8.6: Get the current time and date ............................................................................................................... 55
Section 8.7: Increment a Date Object ....................................................................................................................... 56
Section 8.8: Convert to JSON ..................................................................................................................................... 57
Chapter 9: Date Comparison ................................................................................................................................ 58
Section 9.1: Comparing Date values .......................................................................................................................... 58
Section 9.2: Date Dierence Calculation .................................................................................................................. 59
Chapter 10: Comparison Operations ................................................................................................................ 60
Section 10.1: Abstract equality / inequality and type conversion ........................................................................... 60
Section 10.2: NaN Property of the Global Object ..................................................................................................... 61
Section 10.3: Short-circuiting in boolean operators ................................................................................................. 63
Section 10.4: Null and Undefined ............................................................................................................................... 65
Section 10.5: Abstract Equality (==) ........................................................................................................................... 65
Section 10.6: Logic Operators with Booleans ........................................................................................................... 66
Section 10.7: Automatic Type Conversions ............................................................................................................... 67
Section 10.8: Logic Operators with Non-boolean values (boolean coercion) ....................................................... 67
Section 10.9: Empty Array ........................................................................................................................................... 68
Section 10.10: Equality comparison operations ........................................................................................................ 68
Section 10.11: Relational operators (<, <=, >, >=) ........................................................................................................ 70
Section 10.12: Inequality .............................................................................................................................................. 71
Section 10.13: List of Comparison Operators ............................................................................................................ 72
Section 10.14: Grouping multiple logic statements ................................................................................................... 72
Section 10.15: Bit fields to optimise comparison of multi state data ...................................................................... 72
Chapter 11: Conditions .............................................................................................................................................. 74
Section 11.1: Ternary operators .................................................................................................................................. 74
Section 11.2: Switch statement .................................................................................................................................... 75
Section 11.3: If / Else If / Else Control ......................................................................................................................... 77
Section 11.4: Strategy ................................................................................................................................................... 78
Section 11.5: Using || and && short circuiting ............................................................................................................. 79
Chapter 12: Arrays ...................................................................................................................................................... 80
Section 12.1: Converting Array-like Objects to Arrays ............................................................................................. 80
Section 12.2: Reducing values .................................................................................................................................... 82
Section 12.3: Mapping values ..................................................................................................................................... 84