Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Why I Like JavaScript

DZone's Guide to

Why I Like JavaScript

Every developer has their preferred language. Read on to see why this dev loves JavaScript, despite some of the strange parts that have turned others off of it.

· Web Dev Zone ·
Free Resource

Deploying code to production can be filled with uncertainty. Reduce the risks, and deploy earlier and more often. Download this free guide to learn more. Brought to you in partnership with Rollbar.

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 breath of 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 go directly to the 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 it. So, I'll go over my favorite features.

The Default Parameters

I've first used default parameters in Python, and it was charming. In JavaScript, however, 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 functionalities, 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 a better way of constructing strings. It removes the challenges inherent in formatting 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 = `http://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.

JavaScript has been the bad guy for a while but it gas significantly improved. One can simply accept the weird parts and avoid them.

Deploying code to production can be filled with uncertainty. Reduce the risks, and deploy earlier and more often. Download this free guide to learn more. Brought to you in partnership with Rollbar.

Topics:
web dev ,javascript ,jquery ,javascript functions ,javascript methods

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}