<img height="1" width="1" style="display:none;" alt="" src="https://px.ads.linkedin.com/collect/?pid=299788&amp;fmt=gif">

Why JavaScript Isn't as Bad as Some People Say

Software Development

JavaScript is the second most popular programming language of 2019, according to StackOverFlow. 95% of all websites use it. And if a website doesn’t use it, it’s generally going to be a boring website. Despite its widespread use and applicability, it’s also regarded by many as one of the worst programming languages to use. Let’s talk about why, and whether the complaints are valid.

Why is it hated by so many?

There are several reasons to hate any given language, but one of the most common reasons for hating JavaScript is the strange, seemingly counter intuitive behavior. So first, let's look at some code examples that make JavaScript appear ridiculous. What happens when you add two objects together?

1: {} + {} == "[object Object][object Object]"

Adding an object to an array?

2: {} + [] == 0

Adding an array to an array?

3: [] + [] == ""

And a couple more examples.

4: [] + "" == ""
5: [] + {} == "[object Object]"
6: var arr = [];
arr[3] = "";
arr.length == 4
7: (NaN == NaN) == false

These examples are all pretty clearly painting the same picture: JavaScript makes zero sense. But the truth is, it's different. There are two bottom lines here, and I'm putting them pretty far from the actual bottom line.

1. Every single example makes sense when you think about it.

2. No experienced developer is even going to run into nearly any of these problems more than 5 times per year, if that.

"How do these examples make sense?" You might ask. Let's take a look.

1: You can't add objects together, so the computer has two options: convert to string, or convert to number. Most of the time, JavaScript and many other languages will opt to convert to string. What is the string version of an object? "[object Object]". We're adding two objects together in this example, so we have two of the same string concatenated.

2: In this case, JavaScript sees the {} as an empty code block and ignores it. This leaves us with +[]. This changes the operator function, so instead of adding or concatenating, it converts [] into a number.

3: As mentioned before, JavaScript opts to convert things into strings when the + operator is used, if it can. An empty array, when converted to a string, becomes an empty string. Add two empty strings, and you're left with an empty string.

4: Same as #3, but the second array is already a string and doesn't need to be converted.

5: Same principles as #1 and #3 and 4. [] becomes an empty string, {} becomes the string "[object Object]". Add them together and you've got it.

6: This one is a little weird. Other languages force you to specify the length of an array when it's created. So the length is static. In the case of JavaScript, the length is dynamic. We set the fourth value in the array to 3. JavaScript filled in indexes 0-2 (three values, arrays start at zero). So the array is [ , , , 3]. That's a length of four.

7: This one is definitely messed up, but it's not unique to JavaScript and is therefore a stupid complaint against JavaScript. There's a long explanation, but you're better off just using a workaround to test this stuff.

And there you have it!

There's more to complain about, for sure, just as there is with any programming language. But these complaints are pretty ignorant. If you need to test things strictly, without using implicit type conversion like JavaScript tries to do for you, use === instead of ==. If you're adding arrays to objects or something else similar to the examples shown above (except #7 which is truly strange and will definitely come up), you need to reevaluate your career, because nobody does that.

Managing JIRA at Scale White Paper

TAGS: Software Development

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Subscribe to Our Newsletter

Recent Blog Posts