JavaScript Basics — Catching Exceptions, Assertions, and Regexes
Photo by Leonardo Wong on Unsplash

JavaScript is one of the most popular programming languages in the world. To use it effectively, we’ve to know about the basics of it.

In this article, we’ll look at how to catch exceptions, use assertions, and regexes.

Selective Catching

We can catch exceptions selectively.

To do that, we can check which kind of subclass of Error is being thrown.

For instance, we can create our own subclass of error by writing:

class InputError extends Error {} try {

//...

throw new InputError('error');

} catch (ex) {

if (ex instanceof InputError) {

console.log(ex);

}

}

We create our own InputError class by using the extends keyword.

Then in the catch block, we use the instanceof operator to check the ex variable to see if it’s an instance of our InputError class.

We didn’t define its own constructor, which means that it’ll use Error ‘s constructor.

So we can pass in a string.

Assertions

Assertions are checks inside a program that check if something is the way it is supposed to be.

For instance,e we can create an assertion by writing some checks and throwing an exception if it’s not met.

We write:

const firstElementExists = (arr) => {

if (!Array.isArray(arr) || arr.length === 0) {

throw new Error('no first element found');

}

return arr[0];

}

Now instead of our program failing, we can use this function to check if the first element of an array exists.

Regular Expressions

Regular expressions are used for finding things in strings.

It’s a language to describe patterns in strings.

For instance, we can create one by writing:

const re1 = new RegExp("foo");

or:

const re2 = /foo/;

in Javascript.

Both regular expressions represent the same pattern. We have f followed by 2 o ‘s.

Backslashes are for delimiting the regular expressions in the 2nd example.

Testing for Matches

We can test for matches by using the test method that’s part of a RegExp instance.

For instance, we can write:

/foo/.test("foobar")

which returns true since ‘foo’ is in foobar .

On the other hand, if we have:

/foo/.test("xbar")

Then test returns false since ‘foo’ isn’t in 'xbar' .

Sets of Characters

Regular expressions can include more complex patterns.

We can use indexOf to check if a string is in a substring like we did above.

For instance, we can check if a string has a number by writing:

/[0-9]/.test("abc 123")

Then we get true since 'abc 123' has digits 0 to 9 in it.

The - is between 2 characters is used to indicate a range of characters.

Common character groups have their own built-in shortcuts.

The following are shorthands we can use in our regex patterns:

\d — any digit character

— any digit character \w — any alphanumeric character

— any alphanumeric character \s — any whitespace character

— any whitespace character \D — any character that’s not a digit

— any character that’s not a digit \W — nonalphanumeric character

— nonalphanumeric character \S — non-whitespace character

— non-whitespace character . — any character except for the newline

We can use the ^ character to match something that’s the opposite of the pattern.

For instance, we can write:

/[^\d]/.test("abc")

Then we match anything that’s not a digit.

That returns true since 'abc' has no digits.

Repeating Parts of a Pattern

We can put a + sign to repeat a pattern.

For instance, we can write:

/\d+/.test("123")

Then we match 1 or more digit characters.

The star * has a similar meaning but also allows the pattern to match 0 times.

A question mark lets us make a pattern optional.

For instance, we can write:

/colou?r/.test("colour")

Then we get true since u is in the string 'colour' .

Ranges

We can check with regex to see if some pattern appeared a given number of times.

The number of times that something appears is indicated with curly braces and some numbers inside it.

For example, we can write:

/\d{1,2}/.test('12')

to check if a digit appears 1 to 2 times. The numbers are separated by a comma.

We can also write:

/\d{1}/.test('12')

to check if a digit appears only once.

To check if something appears x or more times, we can write:

/\d{1,}/.test('12')

So we can if a string has 1 or more digits together.

Conclusion

We can create our own Error subclass and check for them in the catch block with instanceof .

Assertions are checks that must be met or an exception is thrown.

Regexes are useful for checking for patterns in strings.
['John Au-Yeung']
['Technology', 'Programming', 'JavaScript', 'Software Development', 'Web Development']