JS Essentials (9)
JS Essentials (9)
Contents
Contents .................................................................................................................................................. 2
About ...................................................................................................................................................... 7
What is in this book? ........................................................................................................................... 8
Is this book for you? ............................................................................................................................ 8
Chapter 1: Getting Started ....................................................................................................................... 9
What is programming? ........................................................................................................................ 9
What is Node.js?.................................................................................................................................. 9
What is JavaScript? ........................................................................................................................... 10
What is EcmaScript? ......................................................................................................................... 10
Installation......................................................................................................................................... 10
Windows and macOS..................................................................................................................... 11
Ubuntu (Debian) ............................................................................................................................ 11
Chapter 2: Starting with JavaScript ........................................................................................................ 12
Statements and keywords ................................................................................................................... 12
Comments ......................................................................................................................................... 13
Variables and data types .................................................................................................................... 14
Primitive data types ....................................................................................................................... 14
Objects .......................................................................................................................................... 16
Defining variables.......................................................................................................................... 16
Functions ....................................................................................................................................... 18
Defining constants ......................................................................................................................... 18
Strings ............................................................................................................................................... 18
Template literals (template strings) ................................................................................................ 19
Code blocks....................................................................................................................................... 19
Operators ........................................................................................................................................... 20
Arithmetic operators ...................................................................................................................... 20
Comparison operators .................................................................................................................... 21
Logical operators ........................................................................................................................... 22
Assignment operators .................................................................................................................... 23
Chapter 3: Structure and Flow Control ................................................................................................... 25
Flow control statements ..................................................................................................................... 26
2
Conditional statements ................................................................................................................... 26
Switch statements .......................................................................................................................... 29
Looping statements ........................................................................................................................ 30
Labelled statements ....................................................................................................................... 34
Chapter 4: Functions and Scopes ........................................................................................................... 36
Types of functions ............................................................................................................................. 36
Regular functions........................................................................................................................... 36
Anonymous functions .................................................................................................................... 38
Arrow functions ............................................................................................................................. 39
Arguments object .............................................................................................................................. 40
Default argument values .................................................................................................................... 41
Recursive functions ........................................................................................................................... 41
Scope ................................................................................................................................................ 42
Chapter 5: Data Type Object Wrappers and Prototypes .......................................................................... 46
String ................................................................................................................................................ 46
String prototypes ........................................................................................................................... 46
Number ............................................................................................................................................. 48
Number prototypes ........................................................................................................................ 48
Arrays ............................................................................................................................................... 49
Array prototypes ............................................................................................................................ 49
Iteration through arrays .................................................................................................................. 53
Spread syntax ................................................................................................................................ 54
Objects .............................................................................................................................................. 56
Object literals ................................................................................................................................ 57
Object constructors ........................................................................................................................ 63
Iteration through objects ................................................................................................................ 66
ES5 object methods ....................................................................................................................... 67
Custom prototypes ............................................................................................................................. 70
Destructuring assignment (ES6)......................................................................................................... 71
JSON................................................................................................................................................. 73
Stringify ........................................................................................................................................ 74
Parse.............................................................................................................................................. 75
Symbols ............................................................................................................................................ 76
Exercise: Finding The Largest or Smallest Number ............................................................................... 78
Extra: Ternary Operator and Short-circuits ............................................................................................ 80
3
Ternary operator ................................................................................................................................ 80
Short-circuits ..................................................................................................................................... 80
Chapter 6: Arrow Functions In-depth..................................................................................................... 82
“This” and arguments binding............................................................................................................ 82
Callable and constructible .................................................................................................................. 85
Chapter 7: Classes (ES6) ....................................................................................................................... 86
Constructor and properties ................................................................................................................. 86
Methods ............................................................................................................................................ 88
Getter and setter ................................................................................................................................ 89
Static methods ................................................................................................................................... 90
Class fields ........................................................................................................................................ 92
Public fields ................................................................................................................................... 92
Private fields.................................................................................................................................. 93
Inheritance......................................................................................................................................... 94
Super ............................................................................................................................................. 95
Using “insatcneof”............................................................................................................................. 97
Exercise: Queue and Stack..................................................................................................................... 98
Queue ................................................................................................................................................ 98
Stack ............................................................................................................................................... 106
Chapter 8: Code Splitting and Modules................................................................................................ 109
Export/Import your own code .......................................................................................................... 109
Importing a module ......................................................................................................................... 112
Export/Import (ES6) ........................................................................................................................ 112
Default export .............................................................................................................................. 115
Exercise: Arithmetic Expression Evaluation with Dijkstra’s Two-Stack Algorithm .............................. 117
Chapter 9: Working with bits ............................................................................................................... 122
Binary base...................................................................................................................................... 122
Bitwise operators ............................................................................................................................. 123
AND............................................................................................................................................ 124
OR............................................................................................................................................... 124
XOR ............................................................................................................................................ 124
NOT ............................................................................................................................................ 125
Zero fill left shift ......................................................................................................................... 125
Signed right shift ......................................................................................................................... 126
Zero fill right shift ....................................................................................................................... 126
4
Chapter 10: Asynchronous Programming............................................................................................. 127
What is asynchronous programming?............................................................................................... 127
Callbacks......................................................................................................................................... 130
Promises.......................................................................................................................................... 133
Chained promises ........................................................................................................................ 136
Async/Await.................................................................................................................................... 138
Chapter 11: Errors and Error Handling ................................................................................................ 141
Throw and Try/Catch....................................................................................................................... 142
Error class ....................................................................................................................................... 145
<EvalError> .................................................................................................................................... 146
<SyntaxError> ................................................................................................................................. 146
<RangeError>.................................................................................................................................. 148
<ReferenceError> ............................................................................................................................ 149
<TypeError> ................................................................................................................................... 150
<URIError> ..................................................................................................................................... 151
System errors................................................................................................................................... 152
User-specified errors........................................................................................................................ 153
AssertionError ................................................................................................................................. 157
assert.deepStrictEqual(actual, expected[, message]) ..................................................................... 160
assert.notDeepStrictEqual(actual, expected[, message])................................................................ 160
assert.ifError(value) ..................................................................................................................... 160
Error handling in callbacks .............................................................................................................. 160
Chapter 12: Date ................................................................................................................................. 162
Constructor...................................................................................................................................... 162
Methods .......................................................................................................................................... 163
Getters ......................................................................................................................................... 163
Setters ......................................................................................................................................... 166
Conversions ................................................................................................................................. 170
Chapter 13: NPM ................................................................................................................................ 172
npm install....................................................................................................................................... 173
Versioning ................................................................................................................................... 175
Global packages........................................................................................................................... 176
npm remove..................................................................................................................................... 176
npm list ........................................................................................................................................... 177
Chapter 14: File System....................................................................................................................... 178
5
File .................................................................................................................................................. 178
Read ............................................................................................................................................ 178
Write ........................................................................................................................................... 179
Rename ....................................................................................................................................... 180
Delete .......................................................................................................................................... 181
Directory (folder) ............................................................................................................................ 181
Open............................................................................................................................................ 181
Read ............................................................................................................................................ 182
Create .......................................................................................................................................... 182
Rename ....................................................................................................................................... 182
Delete .......................................................................................................................................... 183
Resources ............................................................................................................................................ 184
Books .............................................................................................................................................. 184
Websites .......................................................................................................................................... 184
6
About
This book will help you step by step to start working with Node.js. The main goal in this
book is to keep it as concise and straightforward as possible, so it is comprehensible for
everyone. As I’m writing this book, I’m quarantined due to the COVID-19 pandemic, and I hope
things get better soon. This book is a work of hard dedication, and I hope you enjoy it while
reading as much as I enjoyed while writing and always remember being a good programmer is
neither memorizing all the codes and syntax nor following every idea without questioning it! It is
about being able to solve a problem efficiently, so don’t be ashamed of opening this book or
other books or even a documentation on the internet thousands of times to check out a
description and question it if you think it is wrong. You can use the information below in case
you wanted to contact me:
adnanbabakan.personal@hotmail.com
- Adnan Babakan
7
What is in this book?
This book includes all the basic concepts of JavaScript to get started as well as Node.js
code samples. It will walk you through some popular approaches of Node.js. The main goal of
this book is to be as concise as possible while explaining everything, so it doesn’t make the
reader confused or overwhelmed. While reading, this book provides you with some exercises
which can help you understand what you’ve read previously.