Author: Angus Croll

The Secret Life of JavaScript Primitives

You may not know it but, in JavaScript, whenever you interact with string, number or boolean primitives you enter a hidden world of object shadows and coercion. So dust off your Sherlock Holmes outfit and read on…

Continue reading “The Secret Life of JavaScript Primitives”


Understanding JavaScript’s this keyword

(In Portugese)

The JavaScript this keyword is ubiquitous yet misconceptions abound.

What you need to know
Every execution context has an associated ThisBinding whose lifespan is equal to that of the execution context and whose value is constant. There are three types of execution context: global, function and evaluation. Here’s a tabular summary followed by a little more detail, and some examples:

Execution Context Syntax of function call Value of this
Global n/a global object (e.g. window)
Function Method call:;
Function Baseless function call:
global object (e.g. window)
(undefined in strict mode)
Function Using call:, myArg);
Function Using apply:
foo.apply(context, [myArgs]);
Function Constructor with new:
var newFoo = new Foo();
the new instance
(e.g. newFoo)
Evaluation n/a value of this in parent context

1. Global context
this is bound to the global object (window in a browser)

alert(this); //window

Continue reading “Understanding JavaScript’s this keyword”

A JS1K Byte-Saver Quiz!

Here’s a little javascript quiz to help you limber up for JS1K. Each problem is solvable with one statement. Answer with the shortest possible solution.

This is not about writing the most readable or production-ready code. Its a fun test of versatility and language knowledge.

Answers should work on all major browsers except where denoted with “ECMA 5” (in which case they should work on all browsers except IE<9) . The number in parentheses indicates how many characters were in my solution, including semicolons but omitting returns and extra spaces.

I’ll post my solutions on Monday. Good luck! (Spoiler alert – some solutions in comments)

Continue reading “A JS1K Byte-Saver Quiz!”

Understanding JavaScript’s ‘undefined’

Compared to other languages, JavaScript’s concept of undefined is a little confusing. In particular, trying to understand ReferenceErrors (“x is not defined”) and how best to code against them can be frustrating.

This is my attempt to straighten things out a little. If you’re not already familiar with the difference between variables and properties in JavaScript (including the internal VariableObject) now might be a good time to check out my previous posting.

What is undefined?

In JavaScript there is Undefined (type), undefined (value) and undefined (variable).
Continue reading “Understanding JavaScript’s ‘undefined’”

Variables vs. Properties in JavaScript

( 한국어 )

What is a property? What is a variable? In what ways, if any, do they differ?

Basic questions. Fundamental to understanding the language, but mostly overlooked in the literature of JavaScript. (That said, I know of two excellent articles on the topic. I’ve cited them at the end of this text)

Anyway, here’s my take on it:
Continue reading “Variables vs. Properties in JavaScript”

No ifs…alternatives to statement branching in JavaScript

You could do this..

//Example 1
function getEventTarget(evt) {
    if (!evt) {
    	evt = window.event;
    if (!evt) {
    var target;
    if ( {
        target =;
    } else {
        target = evt.srcElement;
    return target;

or you could do this…

//Example 2
function getEventTarget(evt) {
    evt = evt || window.event;
    return evt && ( || evt.srcElement);

Continue reading “No ifs…alternatives to statement branching in JavaScript”