Categories
JavaScript

The Good Parts of JavaScript

Spread the love

JavaScript is one of the most popular programming languages in the world. It can do a lot and have some features that are ahead of many other languages.

In this article, we’ll look at some good parts of JavaScript.

Misconceptions About JavaScript

We can’t assume that JavaScript is like any other language. To take advantage of the good parts, we’ve to learn it as a new language.

It’s possible to create things with JavaScript without knowing too much about it.

But without knowing the good parts, we won’t be writing code as cleaning as we could.

Dynamic Types

We can use dynamic types to our advantage. With or without strong typing, we got to test our code thoroughly.

Otherwise, we’re going to run into issues regardless of whether the language has strong typing or not.

Type errors are only one class of errors and we can eliminate that with tests that we have to type anyways.

The dynamically typed natures of JavaScript with the ability to create one-off objects with object literals are a great feature that we can’t ignore.

We can create objects without creating classes.

Also, it inspired the JavaScript Object Notation (JSON) for transmitting data,

It’s very similar to JavaScript object literals and we can convert them easily.

Simple Testing Ground

Both Node.js and browsers have a REPL where we can test out our code.

There’re also many online JavaScript editors for running Node.js and browser code.

Many of those editors even have templates for creating projects with various frameworks out of the box.

We can also include libraries on the fly.

With REPLs and online editors, we can create tests and prototype code without much effort.

In all the editors and REPLs, we can see the results right away.

Whitespace

In JavaScript, whitespace are usually insignificant. This lets us have some flexibility in formatting our code.

However, there are some whitespace that are required to separate some tokens like keywords and variables.

For instance, if we have:

const foo = this;

Then the space after the const is required, but the rest are not.

Comments

Comments are good to add to let us clarify our code.

We can write comments with blocks surrounded with /* */ or start with // ..

2 forward slashes are also used by regex literals, so we probably don’t want to use them.

Instead, we can create comment blocks with /* */ .

Names

Names in JavaScript are letters that are optionally followed by one or more characters, digits, or underscores.

They also can’t be reserved words that are used by the JavaScript language itself.

We can’t use reserved words as function names, class names, or variable names.

Numbers

There’s a single number type in JavaScript.

Floating point numbers and interest are all one type.

This is more convenient than having separate types for different kinds of numbers.

Having one type of number eliminates the type of errors that are caused by different types of numbers.

If a number has an exponential part, then the value of the literal is computed by multiplying the part before the e by 10 raised to the power after the e .

For instance, the number 1000 and 1e3 are the same number.

Negative numbers are formed by the - prefix operator.

NaN is a number value that’s the result of an operation that can’t produce a number.

NaN isn’t equal to any value, including itself.

For instance, if we divide a number by 0, then we get NaN returned.

We can check if something produces NaN by using the Number.isNaN method, which checks the value as-is, or the isNaN function, which does the conversion to number before checking the returned value.

Numbers have various methods that we can use to manipulate them. Also, we can do Math with the Math object.

Conclusion

JavaScript has a few useful features.

We can use dynamic typing to our advantage. The ability to create and use object literals is a convenience feature that many programming languages don’t provide.

It also has comments we can use to clarify code.

There’s only one number type in JavaScript, which eliminates lots of issues with multiple types of numbers.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *