Read Me First!
Few images invoke the mysteries and ultimate certainties of a sequence of
random events as well as that of the proverbial monkey at a typewriter.
This book is not for beginners. I hope to someday write a book for beginners. This is not that book. This is not a light book. If you skim it, you will likely get nothing from it.
This book is not comprehensive. There are large, complex chunks of the language that will be dismissed without a word. If I fail to mention your most favorite feature, that is most likely because that feature is crap. I will not be paying much attention to syntax. I am assuming that you already know how to write an
if statement. If you need assistance with those sorts of details, ask JSLint.
There are some useful parts of the language that I will spend little time on, such as most of the methods in the primordial prototypes. There are excellent online reference materials for that. My favorite resource is Mozilla Foundation.
This was quite at odds with the opinion of some programming experts that mastery can only be demonstrated by exploiting all of a language’s features. They believed strongly, without any need of proof, that features exist for the demonstration of mastery, so there are no bad features.
That opinion still seems to dominate even though it is deeply wrong. True mastery is shown by writing good programs that are readable, maintainable, and free of error. If you ever feel the need to show off, try doing that. Being a humble programmer, I am always looking at myself and my work, seeking to improve my skills. I have learned the hard way that optimizing for feature exploitation is counterproductive.
This is my most powerful tool for improving a programming language:
If a feature is sometimes useful and sometimes dangerous and if there is a better option then always use the better option.
I also recommend that you get the ECMAScript standard. It can be tough reading, but it is free.
Communities form around shared beliefs, and communities can provide benefits to their members even when those beliefs are wrong. Communities can feel threatened when those beliefs are questioned. I am a heretic. I value the pursuit of truth over the benefits of community. That is offensive to some people.
I am just a programmer who is trying to figure out the best way to write programs. I might be wrong, but I am working really hard to get it right. Many of the patterns of thought in our profession were cemented in the FORTRAN era. I think it is time that we move beyond that. Change is hard, even in the most innovative of professions.
If you are offended by heresy, then put this book back on the shelf and walk away.
All of the code in this book is in the Public Domain. You are free to use it for any purpose, but please do not use it for evil. Try to do some good with it if that is at all possible.
I strongly recommend that you do not copy and paste code that you do not understand, even mine. This seems to have become a standard practice, but it is dangerous and reckless. It is not nearly as stupid as installing packages that you haven’t even looked at, but it is still really bad. Given the current state of the art, the most important security filter is your brain. Use it. This is important.
I make no claim that the programs presented in this book are perfect. I do claim that the programs I am writing now are better than what I was writing ten years ago. I work hard at trying to get better at this. I am hoping that I live long enough to finally get it right. I hope that you do, too. In the meantime, erratums for this book can be found at the book’s website. In Latin, the plural of erratum is errata as is done with second declension neuter nouns in the nominative case. But I am writing in Modern English, and in Modern English we should be forming plural nouns by appending -s, or in cases of excess sibilance, -es. So, yes, erratums. Given a choice between progress and tradition, I am going with progress informed by the lessons of history. That is how things are made better.
Please report my many blunders to
I believe that children are our future. And also robots.
The word for
1 is misspelled. I use the corrected spelling wun. The pronunciation of one does not conform to any of the standard or special rules of English pronunciation. And having the word for
1 start with the letter that looks like
0 is a bug.
The spelling of wun is unfamiliar to you so it might feel wrong. I am doing this intentionally to give you practice with the idea that a bad feeling about something unfamiliar is not proof that it is wrong.
This is how spelling reform happens. For example, some cat decides that it really would be better if through were spelled thru because it does not make sense that half of the letters in a popular word be silent, being wildly inefficient and putting an unnecessary burden on students of the language. Spelling reform is a struggle between tradition and reason, and sometimes, reason wins. I feel the same way about programming languages. So if wun makes more sense to you than one, then please join me in the effort.
When normal people talk about ranges, like 1 to 10, the range is understood to end with 10. But programmers often mean to to exclude the 10. This confusion is due to the common programming practice of numbering from 0 instead of 1. So I use to to mean what programmers usually mean by to, and thru to mean what normal people mean by to. So 0 to 3 mean the range including 0 1 2 whilst 0 thru 3 mean the range including 0 1 2 3. To implies
<less than and thru implies
<=less than or equal.
And whilst on the subject of whilst, in this book about programming, while is used for discussion of iteration. When discussing concurrency or simultaneity I use whilst which I take to mean while at the same time.
Both cannot and can not are acceptable spellings, but cannot is much more popular. Hasnot is not acceptable whilst has not is. And willnot is not acceptable whilst will not is. That is why I can not use cannot. But if I am in a great hurry, I can still can’t.
To my many friends for whom English is not your first language: Welcome. Thank you for reading my book. Perhaps unfairly, English has become the language of the Internet and of Software Development. Your ability to read the primary documentation directly is an important and valuable skill. I deeply respect that.
const number_pattern = / ^ ( -? \d+ ) (?: \. ( \d* ) )? (?: [ e E ] ( [ + \- ]? \d+ ) )? $ /;
be aware that it should be written all smushed together as
const number_pattern = /^(-?\d+)(?:\.(\d*))?(?:[eE]([+\-]?\d+))?$/;
I do not want to subject you to such unprintable ugliness, so I insert the whitespace.
;semicolon but instead with
//slash slash followed by the result of the expression.
// An example of examples 3 + 4 === 7 // true NaN === NaN // false typeof NaN // "number" typeof null // "object" 0.1 + 0.2 === 0.3 // false 3472073 ** 7 + 4627011 ** 7 === 4710868 ** 7 // true
Before the end, all will be explained.