Sucksβ

Nota Bene.

This page is a draft, because it covers very few aspects of JavaScript.

Currently missing parts: correct page structure, perfomance section (node.js), toolchain (node.js, npm, webpack, pm2, etc.), references to related benchmarks, libraries, RFCs, etc., community section. Feel free to add information yourself with Edit on GitHub link.
We don't serve ads
and don't ask you to buy shit, because that sucks. If you want, be our patron ( patreon, paypal, liberapay ), that's it.

Note: some of this is not JavaScript itself, but Web API

Poor Design

XMLHttpRequest
HTMLHRElement
var i = 1;
// some code
i = i + ""; // oops!
// some more code
i + 1;  // evaluates to the String '11'
i - 1;  // evaluates to the Number 0
var j = "1";
j++; // j becomes 2

var k = "1";
k += 1; // k becomes "11"

[1,5,20,10].sort() // [1, 10, 20, 5]

Type System

var args = Array.prototype.slice.call(arguments);
// arguments will be deprecated eventually
0.1 + 0.2 === 0.30000000000000004;

The problem is not the result, which is expected, but the choice of using floating point number to represent numbers, and this is a lazy language designer choice. See http://www.math.umd.edu/~jkolesar/mait613/floating_point_math.pdf.

typeof NaN === "number"

// To make matters worse NaN doesn't equal itself
NaN != NaN
NaN !== NaN

// This checks if x is NaN
x !== x
// This is the proper way to test
isNaN(x)

This is as it should be, as per IEEE754. Again, the problem is the indiscriminate choice of IEEE754 by the language designer or implementer.

Bad Features

Note: You can bypass many of these bad features by using linters

// This as a local object reference in a method
object.property = function foo() {
    return this; // This is the object that the function (method) is being attached to
}

// This as a global object
var functionVariable = function foo() {
    return this; // This is a Window
}

// This as a new object
function ExampleObject() {
    this.someNewProperty = bar; // This points to the new object
    this.confusing = true;
}

// This as a locally changing reference

function doSomething(somethingHandler, args) {
    somethingHandler.apply(this, args); // Here this will be what we 'normally' expect
    this.foo = bar; // This has been changed by the call to 'apply'
    var that = this;

    // But it gets better, because the meaning of this can change three times in a single function
    someVar.onEvent = function () {
        that.confusing = true;
        // Here, this would refer to someVar
    }
}
// This returns undefined
return
{
    a: 5
};
// This returns undefined
return
{
    'a': 5
};
function bar() {
    // Oops I left off the var keyword, now I have a global variable
    foo = 5;
}

This can be fixed by using ‘use strict’ in ES5.

0 == ""
0 == "0"
0 == " \t\r\n "
"0" == false
null == undefined

""    != "0"
false != "false"
false != undefined
false != null
new Function("x", "y", "return x + y");
new Array(1, 2, 3, 4, 5);
new Object({"a": 5});
new Boolean(true);
parseInt("72", 10);

You can use Number('72') to convert to a number.

with (obj) {
    foo = 1;
    bar = 2;
}
for (var name in object) {
    if (object.hasOwnProperty(name)) {
        /* ... */
    }
}
// Or
Object.keys(object).forEach(function() { ... });
var n = 0;
for (var i in [3, 'hello world', false, 1.5]) {
    i = parseInt(i); // output is wrong without this cumbersome line
    alert(i + n);
}
// Or
[3, 'hello world', false, 1.5].map(Number).forEach(function() { alert(i + n) });

Missing Features

// It works okay for numbers and strings
const pi = 3.14159265358;
const msg = "Hello World";

// It doesn't work for objects
const bar = {"a": 5, "b": 6};
const foo = [1, 2, 3, 4, 5];

// You also can't easily make your parameters constant
const func = function() {
    const x = arguments[0], y = arguments[1];

    return x + y;
};
// ES6
x -> x * x
Math.pow(7, 2); // 49

DOM

// This event handler lets the event propagate
function doNothingWithEvent(event) {
    return true;
}

// This event handler cancels propagation
function doNothingWithEvent(event) {
    alert('screwing everything up');
    return true;
}
JavaScript sucks, here is why. We hope, that article will help you to choose the perfect programming language for you. JavaScript sucks, here is why. 2020 Your Language Sucks JavaScript JavaScript
We don't serve ads
and don't ask you to buy shit, because that sucks. If you want, be our patron ( patreon, paypal, liberapay ), that's it.