My most hated JavaScript features

  • Typeof operator
  • Automatic semicolon insertion
  • Integer support
  • ES6 Class binding
  • Pass-by-ref and lack of Immutability

Null data type

“Clean code” mentions

  1. In JSON.stringify Null values are included but undefined values are not.
  2. For libraries such lodash it is a valid value in. i.e. item.get(nullValue, default) will return our constant with Null assigned nullValue. This is because lodash.get only defaults on undefined.
  • 0-> Could also be miscontrued as “not allergic”
  • ""-> Empty string could work but could be confusing
  • Null -> seems to describe it perfectly. “Not yet set”

Typeof operator

The main reason this guy is such an issue is because it is so useful, but returns such inconsistent results. For example:

  • typeof(NaN) (not-a-number) equalsNumber (??). You need to use isNaN(value).
const​ typeCheck =​ function​ (o) {
var​ s =​ Object.prototype.toString.call(o);
return​ s.match(/\[object (.*?)\]/)[1].toLowerCase();
}
typeCheck({}); // "object"
typeCheck([]); // "array"
typeCheck(5); // "number"
typeCheck(null​); // "null"
typeCheck(); // "undefined"
typeCheck(/abcd/); // "regex"
typeCheck(new​ Date()); // "date"

Automatic semicolon insertion

In Douglas Crockford’s booked titled: JavaScript: The Good Parts he goes into detail as to why it is a dangerous feature, I can cover the basics here.

function someFunc() {
return
{
prop: true
};
}
someFunc();
// returns undefined
// WHY -> as ASI has terminated the return statement, meaning the block below it is not returned or run.

Integer support

The short is that JavaScript doesn’t have integers, everything is a 64-bit Floating Point Number with a maximum number of decimals of 17. Unfortunately the prevailing issue is that they have limited precision.

ES6 Class binding

With ES6 Classes were introduced, internally they are syntactic sugar over prototype-based inheritance.

Class Logger {
printName (name = 'there') {
this.print(`Hello ${name}`);
}
print (text) {
console.log(text);
}
}
const logger = new Logger();
const { printName } = logger;
printName();
// error - can't find print
constructor () {
this.printName = this.printName.bind(this);
}
constructor() {
autoBind(this);
}

Pass-by-ref and lack of Immutability

One of the trickiest parts I found when I started with JavaScript was its pass-by-reference approach. For example:

const myObj = { hasGoatee: false };
const myNewObj = myObj;
myNewObj.hasGoatee = true;
console.log(myObj);
// { hasGoatee: true }
(myObj === myNewObj) // comparison of address space in memory
const myObj = { hasGoatee: false };
const myNewObj = Object.assign({}, myObj);
myNewObj.hasGoatee = true;
console.log(myObj);
// { hasGoatee: false }
JSON.stringify(object1) === JSON.stringify(object2);

More where this came from

This story is published in Noteworthy, where thousands come every day to learn about the people & ideas shaping the products we love.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store