JavaScript Problems — Strings, Arrays and More
By: John Au-Yeung
Tags: JavaScript, Web Development, Software Development, Technology, Programming

Photo by Susan Mohr on Unsplash

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.

Count String Occurrences in a String

We can use regex to find all the occurrences of a given string with the match method.

For instance, we can write:

const str = "foo bar baz";

const count = (str.match(/bar/g) || []).length;

We search for all occurrences of the 'bar' string in our str string.

Repeat Character N Times

Strings have the repeat method to repeat the string multiple times.

For instance, we can write:

"foo".repeat(10)

to repeat 'foo' 10 times.

We can also write:

Array(11).join("foo")

We need 100 since join puts the 'foo' in between the entries.

Therefore repeat probably makes more sense,

Simplest Code for Array Intersection

We can get the intersection of 2 arrays by using filter to return an array with the entries that are included in the other array.

For instance, we can write:

array1.filter(value => array2.includes(value))

If we have arrays array1 and array2 , then we use filter to return array entries of array1 that is also in array2 by using the includes method with array2 inside the callback.

This would return a new array with arrays that are in both entries.

Remove CSS Class from Element with JavaScript

To remove a class from an element by using the classList property's remove method.

For instance, we can write:

element.classList.remove("foo");

Given that the element element has the foo class.

We can call classList.remove with the argument 'foo' to remove it.

Subtract Days from a Date

We can subtract days from a Date instance with JavaScript.

To do that, we can use the getDate method to get the day of the month.

Then subtract that by whatever number of days we want.

For instance, we can write:

const d = new Date();

d.setDate(d.getDate() - 5);

We call setDate to change the date by changing the day of the month with the number we want.

It doesn't have to be within the month range because it’ll automatically wrap.

Check if an Object is a jQuery Object

We can use the instanceof operator to check if an object is an instance of the jQuery constructor.

For example, we can write:

obj instanceof jQuery

Now we can call jQuery methods in it.

Create an Array Filled with Zeroes

To create an array filled with zeroes, we can use the Array constructor with the fill method.

For instance, we can write:

new Array(len).fill(0);

where len is the length of the array.

fill fills the slots with 0.

Check if a JavaScript Object is a Date

We can check if a JavaScript object is a Date instance with the instanceof operator.

For example, we can write:

date instanceof Date

This works if the object isn’t passed across frame boundaries.

If a Date instance is passed across frame boundaries, we can write:

Object.prototype.toString.call(date) === '[object Date]'

We get the string representation of the Date instance.

Force Refresh of a Page

We can set the Cache-Control header of our code.

For example, we can write:

Cache-Control: max-age=86400, must-revalidate

to set the cache to last 1 day, which is 86400 seconds.

We can disable caching by using the:

Cache-Control: no-cache, must-revalidate

header.

Check if an Array has a Specific String

We can use the indexOf method to check if an array has a specific string.

For instance, we can write:

const arr = ["foo", "bar", "baz"];

const hasFoo = arr.indexOf("foo") > -1;

to check if arr has the string 'foo' . -1 means that the string isn’t in arr ,

Otherwise, the index of the string in the array is returned.

There’s also the includes method if we don’t care about the index.

We can write:

const arr = ["foo", "bar", "baz"];

const hasFoo = arr.includes("foo");

Then it returns true if the string is in arr and false otherwise.

Photo by Terricks Noah on Unsplash

Conclusion

We can set the cache header in our web host to set when the cache expires.

Also, we can check Date instances with built-in operators.

We can get array intersections with array methods.

JavaScript In Plain English

Did you know that we have four publications and a YouTube channel? Find them all at plainenglish.io and subscribe to our YouTube channel!