home icon contact icon rss icon

Notes on Crockfords JavaScript Video Lecture 1

Notes I took while watching the first video of Douglas Crockford’s JavaScript lecture

Language Outline

“script” suggests that JavaScript isn’t a real programming language, Douglas posits that not only is it a real programming language, but a pretty good one.

The language does contain some design errors.

Early bad implementations resulted in JavaScript getting a bad reputation. The implementation are now good.

Programming for the browser and DOM is extremely difficult. A lot of people think that the problem is javacsript itself, but it’s more the environment.

Books on JavaScript are almost (as of 2007) universally bad. The Definitive Guide from O’Reilly is the “least bad” book.

JavaScript standard defined by ECMA, but the standard is not very good. Very difficult to read which led to the bad book problem – no good authoritive source to reference.

JavaScript is a functional language like lisp and scheme.

JavaScript’s History

1992: Jim Gosling started working on a language he called C++ then changed to Oak. Gosling go into a new startup, Oak was used in applications. Company fail. Moved back to Sun. What to do with Oak? Needed new name, Java. Application to demo new language would be web browser, HotJava.

Meanwhile at Netscape. They looked at Java but decided that it was too heavy for browsers. Brendan Ike had idea to do dialect of scheme in the browser, but management said to make a scripting language that looked like Java. That became Livescript. First scripting language to be put into a browser, and first to be put on a server.

Reason Netscape and Java so exciting at the time was that they were envisioned as a way to escape from Microsoft: applications on the net. They decided to ally. Sticky part: what to do with Livescript. A divide between those that wanted Java to be the language of the web, Netscape refused for above reasons. So Livescript name changed to JavaScript and then Sun took naming control of the language.

Microsoft didn’t like this so they reverse engineered the language into JScript.

Netscape didn’t care for this at all. To prevent Microsoft from assuming control of the language they decided to make JavaScript a standard. They took it to the European Computer Manufactors Association (ECMA) because they thought that their language standard would just be rubber stamped there. Unfortunately Microsoft was a member of ECMA, so from the first day Microsoft dictated the standardization. Microsoft had made some errors while reverse-engineering JavaScript into JScript, and they insisted that those errors become part of the standard. It was very important to Microsoft at that time to never do anything to cause anyone’s programs to break. ECMA agreed.

What is JavaScript?

JavaScript is not a web toy language, it is real, useful, and sophisticated. Also very effective.

JavaScript’s key set of ideas:
  • load and go delivery: programs are delivered to the execution site as source code. Reasoning is that it was intended to be embedded in webpages, which are text. This is, of course, no longer the preferred way to getting programs to browsers but the legacy remains.
  • Loose Typing
  • Objects are general containers: unification of objects and hash tables
  • Prototypal inheritance: much different than Class inheritance. Objects can only inherit from other objects. There are no Classes.
  • Lambda: functions are first class objects.
  • Linkage through global variables. Because of “load and go” JavaScript doesn’t have any kind of linker. Separate compilation units are combined into a global namespace, which is a really bad idea.
JavaScript has a small set of possible values
  • Numbers
  • Strings
  • Booleans
  • Objects
  • null
  • undefined

JavaScript doesn’t have multiple number types (e.g. Integer, Float). There are, in fact, no integers. All numbers are represented as 64-bit floating point (i.e. doubles). This means that common arithmetic can act strangely: .1 + .2 != .3

If dealing with decimals need to multiply by 100 for the operations, then scale back.

NaN is a value that is “not a number”. It is returned as the result of undefined or erroneous mathematical statements, e.g. 0/0. It is toxic: any arithmetic operation with NaN as an input will return NaN. NaN is not equal to anything, even itself. NaN === NaN is false. NaN is also not less than NaN or greater than NaN. But if you get typeof NaN it is Number.

Number function converts string into a number. Produces NaN if it has a problem. Similar to + operator.

parseInt function parses integers from a string. Generally you should include radix argument to specify the base we are parsing.

Like Java, JavaScript has a separate Math object. This is an unfortunate idea. The Math object contains a number of useful functions e.g. abs, floor, log, random, round, sqrt.

JavaScript has Strings. They are a sequence of 0 or more 16-bit characters encoded in UCS-2 (not UTF-16 as it has no awareness of pairs for extended character range). The extra characters of UTF-16 are very rarely used, but it is something to be aware of.

No character type in JavaScript. A “character” is a string of one character.

Strings are immutable.

Similar strings are equal with

String literals can be written with single or double quotes, they are the same.

All Strings have a "length" method that returns the number of 16-bit "characters" in the string. This won't necessarily be the number of unicode characters in the string.

String function converts a number into a string.

Strings are objects and so have methods, e.g.:
  • charAt
  • concat
  • indexOf
  • lastIndexOf
  • match
  • replace
  • search
  • slice
  • split
  • substring
  • toLowerCase
  • toUpperCase

Two boolean values in JavaScript: true and false.

Boolean function will take a value and returns its boolean value.

Null is the value that isn't anything.

Undefined is the value that isn't even null. It is the default value for variables. This is confusing because if you define a variable without initializing it it will have the value "undefined".

Undefined is the value you get when you extract a member from an object that doesn't have that member. It is the "missing value" value.

Values that evaluate as false, everything else evaluates as true:
  • false
  • null
  • undefined
  • empty string
  • 0
  • NaN

After these simple values, everything else in JavaScript is Objects. JavaScript is actually an object-oriented language.

Dynamic Objects
  • unification of Object and Hashtable
  • new Object() produces an empty container of name/value pairs
  • Name can be any string, value can be any value except undefined.
  • Object members can be access with dot notation or subscript notation: myObj.key or myObj['key']
  • Although they are hases, no hash nature is visible: no hash methods

JavaScript is loosely typed, which means that any variable is capable of receiving any value. The values do have a type (so the language isn't "untyped"), but variables aren't bound to them.

JavaScript is syntactically a C family language. Differs from C with its type system which allows functions (which are objects) to be values.

Identifiers follow same rules as most C languages.

  Variable names match: /^[A-Za-z_$]*$/

By convention everything is lowercase except for constructors.

Using the initial _ should be reserved for implementations, but are very popular. Initial $ should be reserved for machines: macro processors, etc. Ajax programmers use $ variables a lot for some reason.

JavaScript has a LOT of reserved words, but only a few are used.

Two comment styles: // or /*

Common operators that you’d expect for a C language.

  +"42" = 42, just like Number("42")

Division of two integers can produce a non integer result

and != do type coercion, so always use === and !== which confirm variables are same value and type.

&& is the guard operator or “logical and”. If first operand is truthy then the result is the second operard, otherwise result is first operand. Can be used to avoid null references.


  return a && a.member;

  does the same as:

  if (a) {
    return a.member;
  } else {
    return a;
  }

|| is default or “logical or” operator. If first operand is truthy then result is first operand, otherwise second operand. Can be used to fill in default values.

  // if formField is true, then userInput = formFieldValue
  var userInput = formFieldValue || default_value;

! is “logical not” operator. If operand is truthy the result is false and vice versa. From this if you do !!value you will get its boolean representation.

JavsScript has Bitwise Operators

  & | ^ >> >>> <<

The bitwise operators convert the operand into a 32-bit signed integer, do the operation, then convert the result back into a 64-bit floating point. Unlike normal bitwise operation, this isn’t faster. So unless you really need a bitwise operation don’t use them.

donald said

May 27, 2008 @ 12:22 AM

Math object was a bad idea? Nonsense. Segmenting the global functionspace into discrete domains is a great idea. Would’ve been nice if there were some mechanism to import a domain’s namespace explicitly, if you were trying to do a whole buncha math operations at once, but, you know, whatever. Save me from the polluted PHP functionspace, please.

Wise of you to focus on Javascript intensively. Too many web developers don’t understand it and slag off on it without thinking.

Stephen said

May 27, 2008 @ 04:12 PM

Hey, I’m just writing what Crockford was saying. I think that having separate objects for like functions is great. I really dig Python’s take on this, i.e. being able to pull in specific subsets of libraries with “from foo import bar”. I don’t really see anything in the Math object that you’d need every time either. If a short script doesn’t need it, then it doesn’t need it!

Thanks for the JavaScript props. I’m tired of treating JavaScript like a black box where I just follow existing examples without really fully understanding them. Programming by coincidence is never a good idea.

RSS feed for comments on this post

Leave a Comment