Like any kind of apps, there are difficult issues to solve when we write JavaScript apps.

In this article, we’ll look at some solutions to common JavaScript problems.

Replace All Commas in a String

We can replace all commas in a string with the replace method.

For instance, we can write:

const str = 'foo,bar,baz';
const newStr = str.replace(/,/g, '-');

Then newStr is “foo-bar-baz” .

Good Way to Extend Error in JavaScript?

We can extend the Error class with the extends keyword.

For instance, we can write:

class SpecialError extends Error {
  constructor(message) {
    super(message);
    this.name = 'special error';
  }
}

We created a SpecialError class which inherits from the Error class.

We can set our own name to distinguish it from the Error instance.

#javascript

JavaScript Tips — Errors, Auto-Resizing Text Area, and More
1.15 GEEK