This blog post has become a chapter in my book “
Speaking JavaScript”. You can read it here: “
Basic JavaScript”.
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 Conventions used in this blog post Command line interactionWhenever 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 documentationSometimes, 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 pushThe 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 ECMAScriptThe 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.
InfluencesJavaScript’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:
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 readingThis section explains a few basic syntactic principles of JavaScript.
Statements versus expressionsTo understand JavaScript’s syntax, it helps to know that (simplifyingly), it has two major syntactic categories: statements and expressions.
var foo;
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 blocksFor 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.
SemicolonsSemicolons are
optionalin 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 namesIdentifiers
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 undefinedFurther reading
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 objectsJavaScript makes a somewhat arbitrary distinction between values:
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 valuesThe following are all of the
primitive values(short:
primitives):
Characteristics of primitives:
> 3 === 3 true > 'abc' === 'abc' true
> 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.)
All non-primitive values are
objects. The most common kinds of objects are:
{ firstName: 'Jane', lastName: 'Doe' }The above object has two properties: The value of property firstName is 'Jane', the value of property lastName is 'Doe'.
[ 'apple', 'banana', 'cherry' ]The above array has three elements that can be accessed via numeric indices. For example, the index of 'apple' is 0.
/^a+b+$/
Characteristics of objects:
> {} === {} // two different empty objects false > var obj1 = {}; > var obj2 = obj1; > obj1 === obj2 true
> var obj = {}; > obj.foo = 123; > obj.foo 123
All data structures (such as
arrays) are objects, but not all objects are data structures. For example:
regular expressionsare objects, but not a data structure.
undefined and nullSomewhat unnecessarily, JavaScript has two “non-values”:
undefinedand
null.
> var foo; > foo undefinedIf you read a non-existant property, you also get undefined:
> var obj = {}; // empty object > obj.foo undefinedMissing parameters are undefined, too:
> function f(x) { return x } > f() undefined
Normally, you should treat
undefinedand
nullequivalently, 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
undefinedand
nullare
considered false:
if (!x) { ... }
Warning:
false,
0,
NaNand
''are also considered
false.
Wrapper typesThe instances of object types
Foo(including built-in types such as
Arrayand 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:
> true.toString === Boolean.prototype.toString trueNote 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.
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:
typeofis mainly used for primitive values,
instanceofis 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.
Operand Result 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:
instanceof looks like this:
«value» instanceof «Constr»
It returns
trueif
valueis 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 trueFurther reading Booleans
The primitive boolean type comprises the values
trueand
false. The following operators produce booleans:
Whenever JavaScript expects a boolean value (e.g. for the condition of an
ifstatement), any value can be used. It will be interpreted as either
trueor
false. The following values are interpreted as
false:
All other values are considered
true. Values interpreted as
falseare called
falsy, values interpreted as
trueare called
truthy. Use
Booleanas a function to test how a value is interpreted.
> Boolean(undefined) false > Boolean(0) false > Boolean(3) trueBinary 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:
> NaN && 'abc' NaN > 123 && 'abc' 'abc'
> 'abc' || 123 'abc' > '' || 123 123
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 NumbersAll numbers in JavaScript are floating point (although most JavaScript engines internally also use integers):
> 1 === 1.0 true
Special numbers:
> Number('xyz') // 'xyz' can’t be converted to a number NaN
> 3 / 0 Infinity > Math.pow(2, 1024) // number too large InfinityInfinity is sometimes useful, because it is larger than any other number. Similarly, -Infinity is smaller than any other number.
> +0 0 > -0 0Therefore, 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).
JavaScript has the following
arithmetic operators:
The global object
Mathprovides more arithmetic operations, via functions.
JavaScript also has operators for bitwise operations (e.g. bitwise And).
Further readingThere is a
seriesof 2ality blog posts on numbers, covering topics such as:
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
lengthcounts 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 operatorsStrings 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') -1Further reading Statements Conditionals
The
ifstatement 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
switchstatement, the value of
fruitdecides which
caseis executed.
switch (fruit) { case 'banana': // ... break; case 'apple': // ... break; default: // all other cases // ... }Loops
The
forloop 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
whileloop 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-whileloop 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:
One way of defining a function is via a
function declaration:
function add(param1, param2) { return param1 + param2; }
The above code defines a function
addthat has two parameters
param1and
param2and 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
vardeclarations 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.
argumentslooks 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
xif 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 arrayarguments
is not an array, it is only
array-like: It has a property
lengthand 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 handlingis 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
tryclause surrounds critical code, the
catchclause is executed if an exception is thrown inside the
tryclause.
Further readingenables 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 errorsLet’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
lengththat 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 abcthis in non-method functions
In strict mode, the value of
thisin 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
thisis the so-called
global object(
windowin browsers):
function f() { return this; } console.log(f() === window); // trueNo 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 definedFurther reading Variable scoping and closures
In JavaScript, you must declare variables via
varbefore 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
varstatement:
var x = 1, y = 2, z = 3;
But I
recommendto 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-scopedThe 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
tmpis not restricted to the block starting in line (*), it exists until the end of the function.
Variables are hoistedVariable 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
closureis a function plus the connection to the variables of its surrounding scopes. What
createIncrementor()returns is thus a closure.
IIFE: Simulating block scopingSometimes 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
tmpfrom becoming global.
Inadvertent sharing via closuresThe 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,
ihas 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
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 objectsIn 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
nameand
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
describeare called
methods. They use
thisto 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
inoperator 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
deleteoperator removes a property:
> delete jane.newProperty true > 'newProperty' in jane falseArbitrary 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
thishas 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
thisalways 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
thisfrom the function. The following is an example where we call
forEachwith 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
logHiToFriendsproduces an error:
> jane.logHiToFriends() TypeError: Cannot read property 'name' of undefined
Let’s look at two ways of fixing this. Fix #1: store
thisin 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:
forEachhas 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
thisfrom one of those function expressions.
Constructors: factories for objectsUntil now, you may think that JavaScript objects are
onlymaps 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
Pointsets up the instance data. Second, the property
Point.prototypecontains 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.830951894845301p
is an instance of
Point:
> p instanceof Point true > typeof p 'object'Further reading
Arrays are sequences of array elements that can be accessed via integer indices starting at zero.
Array literalsArray 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
inoperator 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 123Array 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
forEachand
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') falseMethod 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 wayto invoke this method repeatedly to get all matches.
Method replace(): search and replace> '<a> <bbb>'.replace(/<(.*?)>/g, '[$1]') '[a] [bbb]'
The first parameter of
replacemust be a regular expression with a
/gflag, otherwise only the first occurrence is replaced. There is also
a wayto use a function to compute the replacement.
Further readingis 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 π -1Other functionality of the standard library
JavaScript’s standard library is relatively Spartan, but there are more things you can use:
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:
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?
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4