Every programming language has its oddities and challenges. When it comes to JavaScript, it has probably more of those oddities. It’s not the just language itself but also browser support. Different vendors implemented slightly different JavaScript engines for years. It resulted in a ton of challenges. I dealt with these challenges myself. Thanks to JQuery, we had a fresh air. Anyway, this isn’t the end of the story. Over the years, JavaScript improved a lot, really. In this post, I’ll go over JavaScript weirdnesses first and then try to explain why I like JavaScript.
The Weird Parts
I will directly go to code as it’s better to demonstrate than talk.
//Comparison
x == x // returns true
x !== x // returns false
//Types
typeof NaN // returns "number"
"string" instanceof String // returns false
//Numbers
0.1+0.2==0.3 // returns false
//Parentheses matter
function f1() {
return
{
name: 'my function'
}
}
function f2() {
return {
name: 'my function'
}
}
typeof f1() === typeof f2(); //false
We can probably find more of these peculiarities but I’ll keep it short. Let’s focus on the new syntax.
The Good Parts
ES6 is a significant update to the language. It introduced a powerful syntax. I have been using JavaScript for quite some time but I like it more every time I get to use them. So, I’ll go over my favorite features.
The Default Parameters
I’ve first used default parameters in Python and it was charming. It simplifies the code by removing extra logic to cover null cases.
function increment(i, inc = 1) {
return i + inc;
}
The Arrow Functions
I guess it’s not just me. Everbody loves arrows, it simplifies logic for filtering and mapping a lot. Note that, we have some brand new functionality like some and every. They are wonderful. You simply write less.
[1, 2, 3].map(i => i + 1).reduce((s, i) => s + i)
[1, 2, 3].filter(i => i % 2 == 0).sort()
[1, 2, 3].some(i => i % 2 == 0)
[1, 2, 3].every(i => i % 2 == 0)
The Template Strings
The template string is just better way of constructing strings. It removes the challenge to format strings. It can also evaluate the logic inside although it might not be a very good idea to do so.
let name='Gulnihal', postId=3, commentId=5
console.log(`Hello ${name}`)
let url = `https://www.yusufaytas.com/posts/${postId}/comments/${commentId}`
console.log(url)
Destructuring assignment
This is truly magical. You can destruct objects and arrays. You can just get the property you are looking for easily.
const [a, ...b] = [1, 2, 3]; // a becomes 1 and b becomes [2, 3]
const {id, person} = { id: 3, person: {name: 'Gulnihal'}} // id becomes 3 and person becomes {name: 'Gulnihal'}
const newArray = [1, ...[2, 3]]; //newArray becomes [1,2,3]
//You can also destruct in a for loop
const people = [{ id: 3, person: {name: 'Gulnihal'}}]
for (var {id: id, person: {name: name}} of people) {
console.log(`The person with id=${id} is ${name}.`);
}
I’ve gone through my favorites with the new JavaScript features. There are other great features like Promises, Classes and additional object methods. However, the above features changed my daily coding significantly because I’m writing less code in a much more expressive way. In consequence, JavaScript has been the bad guy for a while but it significantly improved. One can accept weird parts and avoid them.