Howardism Musings from my Awakening Dementia
My collected thoughts flamed by hubris
Home PageSend Comment

JavaScript Interview Questions

As I mentioned once upon a time, I find it useful to list out those annoying interview questions that we ask and are asked. The answers may not be helpful, but discussing those edge-cases can be.

JavaScript is a language that everyone uses, but few people actually learn. So here are a few questions I've ran across.

With Considered Harmful

What is the result of the following script, if this is the complete code?

var walla_walla_wicka_onions = {};

with ( walla_walla_wicka_onions ) {
    bing = true;
    bang = true;

document.writeln ( );

It won't display true, but instead displays undefined. The reason, as given by Douglas Crockford is that if the bing property isn't already defined in the object (which it isn't), assigns the bing as a global variable:

document.writeln ( bing );

This is a good reason why you should never use a with statement.

Number Coercions

As you know, JavaScript is typeless (which can be both a "good thing" and a "bad thing"). However, JavaScript does have types (you just can't specify a type when creating a variable container).

What does the following result in:

"1" + 2 + 3     // 123
1 + 2 + "3"     // 33

Why? The + operator is overloaded to be both addition and concatenation. Which does the JavaScript engine use depends on the arguments. The first + has a String on the left side, so it assumes concatenation, and produces 12 (another String) when it then concatenates again with the 3.

In the lower line, 1 and 2 are Numbers, so it adds them to get 3, but the concatenation operator is chosen before addition, so the result is 33.

Yes, a constant source of hard to discover bugs.

Single Number Type

What is the result of the following JavaScript code?

document.writeln ( 0.1 + 0.2 === 0.3 );

In JavaScript, there is only a single number type, IEEE 754 Double Precision floating point. While this is good that there is only one type, it isn't very precise.

(Compare this to Python's accuracy. For instance, type print 2**2000 and watch the pretty numbers go by.) Adding a better number type into JavaScript has been considered, it won't be coming any time soon.

Braces on Left or Right?

The following code executes differently in JavaScript:

return              return {
{                       ok: false
    ok: false           };

Because semicolons are optional, one gets inserted with each carriage return … like right after the return statement.

The left-side code actually produces this code:

    ok: false;

Where the ok: is a label, and the false is simply a statement.

Note: JavaScript was invented by Brendan Eich (in 1995) who intended on making a Java-looking Scheme language for the web. Originally called LiveScript, but Marc andreessen thought that Sun would hate it less if it was called JavaScript. (sigh)

Tell others about this article:
Click here to submit this page to Stumble It