Basic JavaScript for the impatient programmer

Update: This blog post has become a chapter in my book “Speaking JavaScript”. You can read it here: “Basic JavaScript”.



Kind words by Cody Lindley (author of “jQuery Enlightenment”, “JavaScript Enlightenment” and “DOM Enlightenment”):

“Basic JavaScript” … [is the] most complete and concise write up I am aware of.




This blog post enables you to get started with JavaScript as quickly as possible – if you already know how to program.
It describes the smallest subset of the language that allows you to be productive. I call that subset “Basic JavaScript” and recommend to program in it for a while, before moving on to more details and advanced topics. Learning everything at once is too confusing. The post concludes with tips for what to learn next.



Warning: Below, I’m describing rules of thumbs and best practices. I favor clarity over precision (e.g., whenever you see the word “roughly”). The rules are safe, but – unavoidably – a matter of taste.

Table of contents
























1.Conventions used in this blog post
2.The nature of the language
3.Syntax
4.Variables and assignment
5.Values
6.Booleans
7.Numbers
8.Strings
9.Statements
10.Functions
11.Exception handling
12.Strict mode
13.Variable scoping and closures
14.Objects and inheritance
15.Arrays
16.Regular expressions
17.Math
18.Other functionality of the standard library
19.What to learn next?




Conventions used in this blog post



Command line interaction



Whenever I introduce a new concept, I also try to illustrate it via an interaction in a JavaScript command line. This looks as follows:

> 3 + 4
7

The text after the greater-than character is the input, typed by a human. Everything else is output by the JavaScript engine. Additionally, the method console.log() is sometimes used to print data to the console (this method works in most JavaScript environments, including Node.js).

Finding documentation



Sometimes, you see functions and methods in action, which should make it clear how they work. If not, there are links to pages of the Mozilla Developer Network (MDN), where you can look up the details. Additionally, you can use Google to find documentation on MDN. For example, the following Google search finds the documentation for the push() method of arrays:

mdn array push



The nature of the language



This section gives a little background on the nature of JavaScript, to help you understand why it is the way it is.

JavaScript versus ECMAScript



The programming language is called JavaScript, the language standard is called ECMAScript. They have different names, because there is a trademark on “Java” (held by Oracle). At the moment, only Mozilla is allowed to officially use the name “JavaScript”, because they received a license long ago.
Therefore, an open language standard had to have a different name. The current version of JavaScript is ECMAScript 5, ECMAScript 6 is currently being developed.

Influences



JavaScript’s creator, Brendan Eich, had no choice but to create the language very quickly (or other, worse, technologies would have been used at Netscape). He borrowed from several programming languages:

  • Java is the cause of JavaScript’s syntax and of how it partitions values into primitives and objects.

  • Scheme and AWK inspired JavaScript’s handling of functions – they are first-class and used frequently in the language. Closures make them a powerful tool.

  • Self is responsible for JavaScript’s unique style of object-oriented programming (OOP). Its core (which we can’t go into here) is elegant, some of the things built on top of that core are less so. But a simple pattern (shown later) takes care of most use cases. A killer feature of JavaScript OOP is that you can create objects, directly. There is no need to create a class or something similar first.

  • Perl and Python influenced JavaScript’s handling of strings, arrays and regular expressions.


JavaScript did not have exception handling until ECMAScript 3, which explains why the language so often automatically converts values and so often fails silently: it initially couldn’t throw exceptions.


On one hand, JavaScript has quirks and is missing quite a bit of functionality (block-scoped variables, modules, support for subtyping, etc.). On the other hand, it has several powerful features that allow you to work around these problems. In other languages, you learn language features. In JavaScript, you often learn patterns, instead.

Further reading






Syntax



This section explains a few basic syntactic principles of JavaScript.

Statements versus expressions



To understand JavaScript’s syntax, it helps to know that (simplifyingly), it has two major syntactic categories: statements and expressions.

  • Statements “do things”. A program is a sequence of statements. Example of a statement, which declares (creates) a variable foo:

    var foo;


  • Expressions produce values. They are the right-hand side of an assignment, function arguments, etc. Example of an expression:

    3 * 7



The distinction between statements and expressions is best illustrated by the fact that JavaScript (like Java) has two different ways to do if-then-else. Either as a statement:

var x;
if (y >= 0) {
x = y;
} else {
x = -y;
}

Or as an expression:

var x = y >= 0 ? y : -y;

You can use the latter as a function argument (but not the former):

myFunction(y >= 0 ? y : -y)

Lastly, wherever JavaScript expects a statement, you can also use an expression. For example:

foo(bar(7, 1));

foo(...); is a statement (a so-called expression statement), bar(7, 1) is an expression. Both are function calls.

Control flow statements and blocks



For control flow statements, the body can be a single statement. Two examples:

if (obj !== null) obj.foo();

while (x > 0) x--;

However, any statement can always be replaced by a block, curly braces containing zero or more statements. Thus, you could also write:

if (obj !== null) {
obj.foo();
}

while (x > 0) {
x--;
}

In this blog post, we only use the latter form of control flow statements.

Semicolons



Semicolons are optional in JavaScript. But omitting them can lead to surprises, which is why I recommend against doing it.


As you can see above, semicolons terminate statements, but not blocks. There is one case where you will see a semicolon after a block: A function expression is an expression that ends with a block. If such an expression comes last in a statement, it is followed by a semicolon:


var x = 3 * 7;
var f = function () { };


Comments



JavaScript has two kinds of comments: single-line comments and multi-line comments. Single-line comments start with // and are terminated by the end of the line:

x++; // single-line comment

Multi-line comments are delimited by /* and */

/* This is
a multi-line
comment.
*/


Further reading





Variables and assignment



Variables in JavaScript must be declared, before they can be used:

var foo; // declare variable `foo`


Assignment



You can declare a variable and assign a value at the same time:

var foo = 6;

You can also assign a value to an existing variable:

foo = 4; // change variable `foo`


Compound assignment operators



There are compound assignment operators such as +=. The following two assignments are equivalent:

x += 1;
x = x + 1;


Identifiers and variable names



Identifiers are names for things, they play various syntactic roles in JavaScript. For example, the name of a variable is an identifier.


Roughly, the first character of an identifier can be any Unicode letter, a dollar sign ($) or an underscore (_). Later characters can additionally be any Unicode digit. Thus, the following are all legal identifiers:


arg0
_tmp
$elem
π

Several identifiers are “reserved words” – they are part of the syntax and can’t be used as variable names:

arguments break case catch class const continue debugger default delete do else enum eval export extends false finally for function if implements import in instanceof interface let new null package private protected public return static super switch this throw true try typeof var void while with yield

Technically, the following three identifiers are not reserved words, but shouldn’t be used as variable names, either:


Infinity NaN undefined


Further reading





Values



JavaScript has all the values that we have come to expect from programming languages: booleans, numbers, strings, arrays, etc. All values in JavaScript have properties. Each property has a key (or name) and a value. Think fields of a record.
You use the dot (.) operator to read a property:

value.propKey

An example: The string 'abc' has the property length.

> var str = 'abc';
> str.length
3

The above can also be written as:

> 'abc'.length
3

The dot operator is also used to assign a value to a property:

> var obj = {}; // empty object
> obj.foo = 123; // create property `foo`, set it to 123
123
> obj.foo
123

And you can invoke methods via it:

> 'hello'.toUpperCase()
'HELLO'

Above, we have invoked the method toUpperCase() on the value 'hello'.

Primitive values versus objects



JavaScript makes a somewhat arbitrary distinction between values:

  • The primitive values are: booleans, numbers, strings, null, undefined.

  • All other values are objects. That is actually how objects are defined – all values that are not primitive.


The main difference between the two is how they are compared: each object has a unique identity and is only equal to itself:

> var obj1 = {}; // an empty object
> var obj2 = {}; // another empty object
> obj1 === obj2
false
> obj1 === obj1
true

In contrast, all primitive values encoding the same value are considered the same:

> var prim1 = 123;
> var prim2 = 123;
> prim1 === prim2
true

The following two sections explain primitive values and objects in more detail.

Primitive values



The following are all of the primitive values (short: primitives):

Characteristics of primitives:

  • Compared by value: the “content” is compared.

    > 3 === 3
    true
    > 'abc' === 'abc'
    true


  • Always immutable: the values of properties can’t be changed, no properties can be added or removed.

    > var str = 'abc';
    > str.foo = 3; // try to create property `foo` ⇒ no effect
    > str.foo // unknown property
    undefined

    (Reading an unknown property always returns undefined.)

  • A fixed set of values: you can’t define your own primitives.



Objects



All non-primitive values are objects. The most common kinds of objects are:

  • Plain objects (type Object) can be created by object literals:

    {
    firstName: 'Jane',
    lastName: 'Doe'
    }

    The above object has two properties: The value of property firstName is 'Jane', the value of property lastName is 'Doe'.

  • Arrays (type Array) can be created by array literals:

    [ 'apple', 'banana', 'cherry' ]

    The above array has three elements that can be accessed via numeric indices. For example, the index of 'apple' is 0.

  • Regular expressions (type RegExp) can be created by regular expression literals:

    /^a+b+$/



Characteristics of objects:

  • Compared by reference: identities are compared, every value has its own identity.

    > {} === {} // two different empty objects
    false

    > var obj1 = {};
    > var obj2 = obj1;
    > obj1 === obj2
    true


  • Mutable by default.

    > var obj = {};
    > obj.foo = 123;
    > obj.foo
    123


  • User-extensible: you can define new object types, via constructors.


All data structures (such as arrays) are objects, but not all objects are data structures. For example: regular expressions are objects, but not a data structure.

undefined and null



Somewhat unnecessarily, JavaScript has two “non-values”: undefined and null.

  • undefined means “no value”. Uninitialized variables are undefined:

    > var foo;
    > foo
    undefined

    If you read a non-existant property, you also get undefined:

    > var obj = {}; // empty object
    > obj.foo
    undefined

    Missing parameters are undefined, too:

    > function f(x) { return x }
    > f()
    undefined


  • null means “no object”. It is used as a non-value when an object is expected (parameters, last in a chain of objects, etc.).


Normally, you should treat undefined and null equivalently, as if they were the same non-value. One way of checking for them is via an explicit comparison:

if (x === undefined || x === null) {
...
}

Another way is to exploit the fact that both undefined and null are considered false:

if (!x) {
...
}

Warning: false, 0, NaN and '' are also considered false.

Wrapper types



The instances of object types Foo (including built-in types such as Array and any custom types) get their methods from the object Foo.prototype. You can verify this by reading a method without invoking it:

> [].push === Array.prototype.push
true

In contrast, primitives have no types you can access in the language, so each primitive type has an associated type, a so-called wrapper type:

  • The wrapper type of booleans is Boolean. Booleans get their methods from Boolean.prototype:

    > true.toString === Boolean.prototype.toString
    true

    Note that the name of the wrapper type starts with a capital B. If the type of booleans was accessible in JavaScript, it would likely be called boolean.

  • The wrapper type of numbers is Number.

  • The wrapper type of strings is String.


Wrapper types also have instances (their instances are objects), but those are practically never used. Instead, wrapper types serve another purpose: If you call them as functions, they convert values to primitive types.

> Number('123')
123
> String(true)
'true'


Categorizing values via typeof and instanceof



There are two operators for categorizing values: typeof is mainly used for primitive values, instanceof is used for objects.


typeof looks like this:


typeof «value»

It returns a string describing the “type” of value. Examples:

> typeof true
'boolean'
> typeof 'abc'
'string'
> typeof {} // empty object literal
'object'
> typeof [] // empty array literal
'object'


The following table lists all results of typeof.













OperandResult
undefined'undefined'
null'object'
Boolean value'boolean'
Number value'number'
String value'string'
Function'function'
All other values'object'


Two things contradict what we have said about primitives versus objects:


  • The type of a function is 'function' and not 'object'. Given that Function (the type of functions) is a subtype of Object (the type of objects), this isn’t wrong.

  • The type of null is 'object'. This is a bug, but one that can’t be fixed, because it would break existing code.



instanceof looks like this:


«value» instanceof «Constr»

It returns true if value is an object that has been created by the constructor Constr (think: class). Examples:

> var b = new Bar(); // object created by constructor Bar
> b instanceof Bar
true
> {} instanceof Object
true
> [] instanceof Array
true
> [] instanceof Object // Array is a subtype of Object
true


Further reading






Booleans



The primitive boolean type comprises the values true and false. The following operators produce booleans:

  • Binary logical operators: && (And), || (Or).

  • Prefix logical operator: ! (Not)

  • Equality operators: === !== == !=

  • Ordering operators (for strings and numbers): > >= < <=



Truthy and falsy



Whenever JavaScript expects a boolean value (e.g. for the condition of an if statement), any value can be used. It will be interpreted as either true or false. The following values are interpreted as false:

  • undefined, null

  • Boolean: false

  • Number: -0, NaN

  • String: ''


All other values are considered true. Values interpreted as false are called falsy, values interpreted as true are called truthy. Use Boolean as a function to test how a value is interpreted.

> Boolean(undefined)
false
> Boolean(0)
false
> Boolean(3)
true


Binary logical operators



Binary logical operators in JavaScript are short-circuiting – if the first operand suffices for determining the result, the second operand is not evaluated. For example, in the following code, the function foo() is never called.

false && foo()
true || foo()

Furthermore, binary logical operators return either one of their operands – which may or may not be a boolean. A check for truthiness is used to determine which one:

  • And: If the first operand is falsy, return it. Otherwise, return the second operand.

    > NaN && 'abc'
    NaN
    > 123 && 'abc'
    'abc'


  • Or: If the first operand is truthy, return it. Otherwise, return the second operand.

    > 'abc' || 123
    'abc'
    > '' || 123
    123




Equality operators



To check equality in JavaScript, you can either use strict equality (===) and strict inequality (!==). Or you can use lenient equality (==) and lenient inequality (!=). Rule of thumb: Always use the strict operators, pretend that the lenient operators don’t exist. Strict (in)equality is much safer.

Further reading






Numbers



All numbers in JavaScript are floating point (although most JavaScript engines internally also use integers):

> 1 === 1.0
true

Special numbers:

  • NaN (“not a number”): an error value.

    > Number('xyz') // 'xyz' can’t be converted to a number
    NaN


  • Infinity: also mostly an error value.

    > 3 / 0
    Infinity
    > Math.pow(2, 1024) // number too large
    Infinity

    Infinity is sometimes useful, because it is larger than any other number. Similarly, -Infinity is smaller than any other number.

  • -0: JavaScript has two zeros, +0 and -0. It normally does not let you see that and displays both as simply 0:

    > +0
    0
    > -0
    0

    Therefore, it is best to pretend that there is only a single zero (as we have done when we looked at falsy values: both -0 and +0 are falsy).



Operators



JavaScript has the following arithmetic operators:

  • Addition: number1 + number2

  • Subtraction: number1 - number2

  • Multiplication: number1 * number2

  • Division: number1 / number2

  • Remainder: number1 % number2

  • Increment: ++variable, variable++

  • Decrement: --variable, variable--

  • Negate: -value

  • Convert to number: +value


The global object Math provides more arithmetic operations, via functions.


JavaScript also has operators for bitwise operations (e.g. bitwise And).

Further reading



There is a series of 2ality blog posts on numbers, covering topics such as:



Strings



Strings can be created directly via string literals. Those literals are delimited by single or double quotes. The backslash (\) escapes characters and produces a few control characters. Examples:

'abc'
"abc"

'Did she say "Hello"?'
"Did she say \"Hello\"?"

'That\'s nice!'
"That's nice!"

'Line 1\nLine 2' // newline
'Backlash: \\'

Single characters are accessed via square brackets:

> var str = 'abc';
> str[1]
'b'

The property length counts the number of characters in the string:

> 'abc'.length
3

Reminder: strings are immutable, you need to create a new string if you want to change an existing one.

String operators



Strings are concatenated via the plus (+) operator, which converts the other operand to string if one of the operands is a string.

> var messageCount = 3;
> 'You have '+messageCount+' messages'
'You have 3 messages'

To concatenate strings in multiple steps, use the += operator:

> var str = '';
> str += 'Multiple ';
> str += 'pieces ';
> str += 'are concatenated.';
> str
'Multiple pieces are concatenated.'


String methods



Strings have many useful methods. Examples:



> 'abc'.slice(1) // copy a substring
'bc'
> 'abc'.slice(1, 2)
'b'

> '\t xyz '.trim() // trim whitespace
'xyz'

> 'mjölnir'.toUpperCase()
'MJÖLNIR'

> 'abc'.indexOf('b') // find a string
1
> 'abc'.indexOf('x')
-1


Further reading






Statements



Conditionals



The if statement lets a boolean condition decide between a then clause and an (optional) else clause:

if (myvar === 0) {
// then
}

if (myvar === 0) {
// then
} else {
// else
}

if (myvar === 0) {
// then
} else if (myvar === 1) {
// else-if
} else if (myvar === 2) {
// else-if
} else {
// else
}

In the following switch statement, the value of fruit decides which case is executed.

switch (fruit) {
case 'banana':
// ...
break;
case 'apple':
// ...
break;
default: // all other cases
// ...
}


Loops



The for loop has the format

for(initialization; loop while this condition holds; next step)

Example:

for (var i=0; i < arr.length; i++) {
console.log(arr[i]);
}

The while loop continues looping over its body while its condition holds.

// Same as for loop above:
var i = 0;
while (i < arr.length) {
console.log(arr[i]);
i++;
}

The do-while loop continues looping over its body while its condition holds. As the condition follows the body, the body is always executed at least once.

do {
// ...
} while(condition);

In all loops:

  • break leaves the loop.

  • continue starts a new loop iteration.




Functions



One way of defining a function is via a function declaration:

function add(param1, param2) {
return param1 + param2;
}

The above code defines a function add that has two parameters param1 and param2 and returns the sum of both parameters. This is how you call that function:

> add(6, 1)
7
> add('a', 'b')
'ab'


Another way of defining add() is via a function expression:


var add = function (param1, param2) {
return param1 + param2;
};

A function expression produces a value and can thus be used to directly pass functions as arguments to other functions:

someOtherFunction(function (p1, p2) { ... });


Function declarations are hoisted



Function declarations are hoisted, moved in their entirety to the beginning of the current scope. That allows you to refer to functions that are declared later:

function foo() {
bar(); // OK, bar is hoisted
function bar() {
...
}
}

Note that while var declarations are also hoisted, assignments performed by them are not:

function foo() {
bar(); // Not OK, bar is still undefined
var bar = function () {
// ...
};
}


The special variable arguments



You can call any function in JavaScript with an arbitrary amount of arguments – the language will never complain. It will, however, make all parameters available via the special variable arguments. arguments looks like an array, but has none of the array methods.

> function f() { return arguments }
> var args = f('a', 'b', 'c');
> args.length
3
> args[0] // read element at index 0
'a'


Too many or too few arguments



Let’s use the following function to explore how too many or too few parameters are handled in JavaScript (function toArray() is shown later).

function f(x, y) {
console.log(x, y);
console.log(toArray(arguments));
}


Additional parameters will be ignored (except by arguments):

> f('a', 'b', 'c')
a b
[ 'a', 'b', 'c' ]

Missing parameters will get the value undefined:

> f('a')
a undefined
[ 'a' ]
> f()
undefined undefined
[]


Optional parameters



The following is a common pattern for assigning default values to parameters:

function pair(x, y) {
x = x || 0; // (*)
y = y || 0;
return [ x, y ];
}

In line (*), the || operator returns x if it is truthy (not: null, undefined, etc.). Otherwise, it returns the second operand.

> pair()
[ 0, 0 ]
> pair(3)
[ 3, 0 ]
> pair(3, 5)
[ 3, 5 ]


Enforcing an arity



If you want to enforce an arity, you can check arguments.length:

function pair(x, y) {
if (arguments.length !== 2) {
throw new Error('Need exactly 2 arguments');
}
...
}


Converting arguments to an array



arguments is not an array, it is only array-like: It has a property length and you can access its elements via indices in square brackets. You cannot, however remove elements or invoke any of the array methods on it. Thus, you sometimes need to convert it to an array. Which is what the following function does.

function toArray(arrayLikeObject) {
return [].slice.call(arrayLikeObject);
}


Further reading





Exception handling



The most common way of exception handling is shown below.

function throwException() {
throw new Error('Problem!');
}

try {
throwException();
} catch (e) {
console.log(e); // Error: Problem!
console.log(e.stack); // non-standard, but often supported
}

The try clause surrounds critical code, the catch clause is executed if an exception is thrown inside the try clause.

Further reading





Strict mode



Strict mode enables checks and a few other measures that make JavaScript a slightly cleaner language. It is recommended to use it. To do so, make this the first line of a JavaScript file or a script tag:

'use strict';

You can also switch on strict mode per function, by putting the above code at the beginning of a function:

function functionInStrictMode() {
'use strict';
}

The following two sub-sections look at the three great benefits of strict mode.

Explicit errors



Let’s look at an example where strict mode gives us an explicit error, where JavaScript otherwise fails silently: The following function f() does something illegal, it tries to change the read-only property length that all strings have:

function f() {
'abc'.length = 5;
}

When you call that function, it fails silently, the assignment is simply ignored. Let’s change f() so that it runs in strict mode:

function f_strict() {
'use strict';
'abc'.length = 5;
}

Now we get an error:

> f_strict()
TypeError: Cannot assign to read only property 'length' of abc


this in non-method functions



In strict mode, the value of this in non-method function is undefined:

function f_strict() {
'use strict';
return this;
}
console.log(f_strict() === undefined); // true

In non-strict mode, the value of this is the so-called global object (window in browsers):

function f() {
return this;
}
console.log(f() === window); // true


No auto-created global variables



In non-strict mode, JavaScript automatically creates a global variable if you assign to a non-existing variable:

> function f() { foo = 5 }
> f() // no error
> foo
5

In strict mode, you get an error:

> function f_strict() { 'use strict'; foo2 = 4; }
> f_strict()
ReferenceError: foo2 is not defined


Further reading





Variable scoping and closures



In JavaScript, you must declare variables via var before you can use them:

> var x;
> x = 3;
> y = 4;
ReferenceError: y is not defined

You can declare and initialize several variables with a single var statement:

var x = 1, y = 2, z = 3;

But I recommend to use one statement per variable. Thus, I would rewrite the previous statement to:

var x = 1;
var y = 2;
var z = 3;

Due to hoisting (see below), it is usually best to declare variables at the beginning of a function.

Variables are function-scoped



The scope of a variable is always the complete function (as opposed to the current block). For example:

function foo() {
var x = -3;
if (x < 0) { // (*)
var tmp = -x;
...
}
console.log(tmp); // 3
}

We can see that the variable tmp is not restricted to the block starting in line (*), it exists until the end of the function.

Variables are hoisted



Variable declarations are hoisted: The declaration is moved to the beginning of the function, but assignments that it makes stay put. As an example, take the variable declaration in line (*) in the following function.

function foo() {
console.log(tmp); // undefined
if (false) {
var tmp = 3; // (*)
}
}

Internally, this above function is executed like this:

function foo() {
var tmp; // declaration is hoisted
console.log(tmp);
if (false) {
tmp = 3; // assignment stays put
}
}


Closures



Each function stays connected to the variables of the functions that surround it, even after it leaves the scope it was created in. For example:

function createIncrementor(start) {
return function () { // (*)
return start++;
}
}

The function starting in line (*) leaves the context it was created in, but stays connected to a live version of start:

> var inc = createIncrementor(5);
> inc()
5
> inc()
6
> inc()
7

A closure is a function plus the connection to the variables of its surrounding scopes. What createIncrementor() returns is thus a closure.

IIFE: Simulating block scoping



Sometimes you want to simulate a block, for example to keep a variable from becoming global. The pattern for doing so is called IIFE (Immediately Invoked Function Expression):

(function () { // open block
var tmp = ...; // not a global variable
}()); // close block

Above, you can see a function expression that is called right away. The parentheses prevent that it is interpreted as a function declaration; only function expressions can be immediately invoked.
The function body introduces a new scope and prevents tmp from becoming global.

Inadvertent sharing via closures



The following is a niche problem, but it can bite you hard if you are not aware of it. Thus, feel free to skim, it is enough to get a rough impression of what is going on.


Closures keep their connections to outer variables, which is sometimes not what you want:


var result = [];
for (var i=0; i < 5; i++) {
result.push(function () { return i }); // (*)
}
console.log(result[1]()); // 5 (not 1)
console.log(result[3]()); // 5 (not 3)

The value returned in line (*) is always the current value of i, not the value it had when the function was created. After the loop is finished, i has the value 5, which is why all functions in the array return that value.
If you want a snapshot of the current value, you can use an IIFE:

for (var i=0; i < 5; i++) {
(function (i2) {
result.push(function () { return i2 });
}(i)); // copy current i
}


Further reading






Objects and inheritance



Like all values, objects have properties. You could, in fact, consider an object to be a set of properties, where each property is a (key, value) pair. The key is a string, the value is any JavaScript value. So far, we have only seen properties whose keys were identifiers, because those are the only keys that the dot operator can handle. In this section, you’ll learn an additional way of accessing properties that can handle arbitrary strings as keys.

Single objects



In JavaScript, you can directly create objects, via object literals:

var jane = {
name: 'Jane',

describe: function () {
'use strict';
return 'Person named '+this.name;
}
};

The above object has the properties name and describe. You can read (“get”) and write (“set”) properties:

> jane.name // get
'Jane'
> jane.name = 'John'; // set
> jane.newProperty = 'abc'; // created automatically

Function-valued properties such as describe are called methods. They use this to refer to the object that was used to call them.

> jane.describe() // call method
'Person named John'
> jane.name = 'Jane';
> jane.describe()
'Person named Jane'

The in operator checks whether a property exists:

> 'newProperty' in jane
true
> 'foo' in jane
false

If you read a property that does not exist, you get the value undefined. Hence, the previous two checks could also be performed like this:

> jane.newProperty !== undefined
true
> jane.foo !== undefined
false

The delete operator removes a property:

> delete jane.newProperty
true
> 'newProperty' in jane
false


Arbitrary property keys



A property key can be any string.
So far, we have seen property keys in object literals and after the dot operator. (Roughly) you can only use them that way if they are identifiers. If you want to use other strings as keys, you have to quote them in an object literal and use square brackets to get and set the property:

> var obj = { 'not an identifier': 123 };
> obj['not an identifier']
123
> obj['not an identifier'] = 456;

Square brackets also allow you to compute the key of a property:

> var x = 'name';
> jane[x]
'Jane'
> jane['na'+'me']
'Jane'


Extracting methods



If you extract a method, it loses its connection with the object. On its own, the function is not a method, any more and this has the value undefined (in strict mode).

> var func = jane.describe;
> func()
TypeError: Cannot read property 'name' of undefined

The solution is to use the method bind() that all functions have. It creates a new function whose this always has the given value.

> var func2 = jane.describe.bind(jane);
> func2()
'Person named Jane'


Functions inside a method



Every function has the special variable this. This is inconvenient if you nest a function inside a method, because you can’t access the method’s this from the function.
The following is an example where we call forEach with a function to iterate over an array:

var jane = {
name: 'Jane',
friends: [ 'Tarzan', 'Cheeta' ],
logHiToFriends: function () {
'use strict';
this.friends.forEach(function (friend) {
// `this` is undefined here
console.log(this.name+' says hi to '+friend);
});
}
}

Calling logHiToFriends produces an error:

> jane.logHiToFriends()
TypeError: Cannot read property 'name' of undefined

Let’s look at two ways of fixing this. Fix #1: store this in a different variable.

logHiToFriends: function () {
'use strict';
var that = this;
this.friends.forEach(function (friend) {
console.log(that.name+' says hi to '+friend);
});
}

Fix #2: forEach has a second parameter that allows you to provide a value for this.

logHiToFriends: function () {
'use strict';
this.friends.forEach(function (friend) {
console.log(this.name+' says hi to '+friend);
}, this);
}

Function expressions are often used as arguments in function calls in JavaScript. Always be careful when you refer to this from one of those function expressions.

Constructors: factories for objects



Until now, you may think that JavaScript objects are only maps from strings to values, a notion suggested by JavaScript’s object literals, which look like the map/dictionary literals of other languages. However, JavaScript objects also support a feature that is truly object-oriented: inheritance. This section does not fully explain how JavaScript inheritance works, but gives you a simple pattern to get you started. Consult the blog post “JavaScript inheritance by example”, if you want to know more.


In addition to being “real” functions and methods, functions play a third role in JavaScript: They become constructors, factories for objects, if invoked via the new operator. Constructors are thus a rough analog to classes in other languages.
By convention, the names of constructors start with capital letters.
Example:


// Set up instance data
function Point(x, y) {
this.x = x;
this.y = y;
}
// Methods
Point.prototype.dist = function () {
return Math.sqrt(this.x*this.x + this.y*this.y);
};

We can see that a constructor has two parts: First, the function Point sets up the instance data. Second, the property Point.prototype contains an object with the methods. The former data is specific to each instance, the latter data is shared between all instances.


To use Point, we invoke it via the new operator:


> var p = new Point(3, 5);
> p.x
3
> p.dist()
5.830951894845301

p is an instance of Point:

> p instanceof Point
true
> typeof p
'object'


Further reading






Arrays



Arrays are sequences of array elements that can be accessed via integer indices starting at zero.

Array literals



Array literals are handy for creating arrays:

> var arr = [ 'a', 'b', 'c' ];

The above array has three elements: the strings 'a', 'b' and 'c'.
You can access them via integer indices:

> arr[0]
'a'
> arr[0] = 'x';
> arr
[ 'x', 'b', 'c' ]


Property length always indicates how many elements an array has.


> arr.length
3

But it can also be used to remove trailing elements from the array:

> arr.length = 2;
> arr
[ 'x', 'b' ]

The in operator works for arrays, too.

> 1 in arr // does arr have an element at index 1?
true
> 5 in arr // does arr have an element at index 5?
false

Note that arrays are objects and can thus have object properties:

> arr.foo = 123;
> arr.foo
123


Array methods



Arrays have many methods. A few examples:

> var arr = [ 'a', 'b', 'c' ];

> arr.slice(1, 2) // copy elements
[ 'b' ]
> arr.slice(1)
[ 'b', 'c' ]

> arr.push('x') // append an element
4
> arr
[ 'a', 'b', 'c', 'x' ]

> arr.pop() // remove last element
'x'
> arr
[ 'a', 'b', 'c' ]

> arr.shift() // remove first element
'a'
> arr
[ 'b', 'c' ]

> arr.unshift('x') // prepend an element
3
> arr
[ 'x', 'b', 'c' ]

> arr.indexOf('b') // find the index of an element
1
> arr.indexOf('y')
-1

> arr.join('-') // all elements in a single string
'x-b-c'
> arr.join('')
'xbc'
> arr.join()
'x,b,c'


Iterating over arrays



There are several array methods for iterating over elements. The two most important ones are forEach and map.


forEach iterates over an array and hands the current element and its index to a function:


[ 'a', 'b', 'c' ].forEach(
function (elem, index) { // (*)
console.log(index + '. ' + elem);
});

The above code produces the output

0. a
1. b
2. c

Note that the function in line (*) is free to ignore arguments. It could, for example, only have the parameter elem.


map creates a new array, by applying a function to each element of an existing array.


> [1,2,3].map(function (x) { return x*x })
[ 1, 4, 9 ]


Further reading






Regular expressions



JavaScript has built-in support for regular expressions. They are delimited by slashes:

/^abc$/
/[A-Za-z0-9]+/


Method test(): is there a match?




> /^a+b+$/.test('aaab')
true
> /^a+b+$/.test('aaa')
false


Method exec(): match and capture groups




> /a(b+)a/.exec('_abbba_aba_')
[ 'abbba', 'bbb' ]

The returned array contains the complete match at index 0, the capture of the first group at index 1, etc. There is a way to invoke this method repeatedly to get all matches.

Method replace(): search and replace




> '<a> <bbb>'.replace(/<(.*?)>/g, '[$1]')
'[a] [bbb]'

The first parameter of replace must be a regular expression with a /g flag, otherwise only the first occurrence is replaced. There is also a way to use a function to compute the replacement.

Further reading






Math



Math is an object with arithmetic functions. Examples:

> Math.abs(-2)
2

> Math.pow(3, 2) // 32
9

> Math.max(2, -1, 5)
5

> Math.round(1.9)
2

> Math.cos(Math.PI) // pre-defined constant for π
-1



Other functionality of the standard library



JavaScript’s standard library is relatively Spartan, but there are more things you can use:

  • Date:
    a constructor for dates whose main functionality is parsing and creating date strings and accessing the components of a date (year, hour, etc.).

  • JSON:
    an object with functions for parsing and generating JSON data.

  • console.* methods: these browser-specific methods are not part of the language proper, but some of them also work on Node.js.




What to learn next?



After you have learned the basics taught in this blog post, you can move on to the advanced material mentioned at the ends of most sections. Additionally, I recommend the following resources:



Feedback welcome



I tried to find an optimal subset of JavaScript that allows one to be productive. Did I succeed? Are there things that should be added or things that should be removed? I’d especially like to hear from you if you are new to JavaScript: was everything easy to understand or did you get stuck somewhere while reading this post?

Comments

Popular posts from this blog

Steve Lopez and the Importance of Newspapers

Ideas for fixing unconnected computing

Omar to kill me