Categories
JavaScript Answers

How to check for multiple conditions for JavaScript .includes() method?

Sometimes, we want to check for multiple conditions for JavaScript .includes() method.

In this article, we’ll look at how to check for multiple conditions for JavaScript .includes() method.

How to check for multiple conditions for JavaScript .includes() method?

To check for multiple conditions for JavaScript .includes() method, we use the array some method.

For instance, we write

const str1 = "hi hello, how do you do?";
const conditions = ["hello", "hi", "howdy"];
const test1 = conditions.some((el) => str1.includes(el));

to call conditions.some with a callback to see if str1 has any one of 'hello', 'hi' and 'howdy' in the str1 string.

We call str1.includes to see if the el entry in conditions is in the string.

Conclusion

To check for multiple conditions for JavaScript .includes() method, we use the array some method.

Categories
JavaScript Answers

How to check if string contains characters and whitespace with JavaScript?

Sometimes, we want to check if string contains characters and whitespace with JavaScript.

In this article, we’ll look at how to check if string contains characters and whitespace with JavaScript.

How to check if string contains characters and whitespace with JavaScript?

To check if string contains characters and whitespace with JavaScript, we check if there’re non-whitespace characters in the string.

For instance, we write

if (/\S/.test(myString)) {
  // ...
}

to call /\S/.test with myString to check if myString has any non-whitespace characters.

Conclusion

To check if string contains characters and whitespace with JavaScript, we check if there’re non-whitespace characters in the string.

Categories
JavaScript Answers

How to append to URL and refresh page with JavaScript?

Sometimes, we want to append to URL and refresh page with JavaScript.

In this article, we’ll look at how to append to URL and refresh page with JavaScript.

How to append to URL and refresh page with JavaScript?

To append to URL and refresh page with JavaScript, we append to the window.location.href string.

For instance, we write

let url = window.location.href;
if (url.indexOf("?") > -1) {
  url += "&param=1";
} else {
  url += "?param=1";
}
window.location.href = url;

to append a substring to the url according to the value of url.

And then we set the new url string as the value of window.location.href to update the URL and refresh the page.

Conclusion

To append to URL and refresh page with JavaScript, we append to the window.location.href string.

Categories
JavaScript Answers

How to find the index of an object whose attributes match a search in an array of objects with JavaScript?

Sometimes, we want to find the index of an object whose attributes match a search in an array of objects with JavaScript.

In this article, we’ll look at how to find the index of an object whose attributes match a search in an array of objects with JavaScript.

How to find the index of an object whose attributes match a search in an array of objects with JavaScript?

To find the index of an object whose attributes match a search in an array of objects with JavaScript, we can use the findIndex method.

For instance, we write

const index = [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }].findIndex(
  (obj) => obj.id === 3
);

to call findIndex to find the item that has id set to 3 in the

[{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }]

array and return the index of the match.

Conclusion

To find the index of an object whose attributes match a search in an array of objects with JavaScript, we can use the findIndex method.

Categories
JavaScript Answers

How to normalize mouse wheel speed across browsers with JavaScript?

Sometimes, we want to normalize mouse wheel speed across browsers with JavaScript.

In this article, we’ll look at how to normalize mouse wheel speed across browsers with JavaScript.

How to normalize mouse wheel speed across browsers with JavaScript?

To normalize mouse wheel speed across browsers with JavaScript, we just assign -1 or 1 depending on the direction of the wheel scrolling.

For instance, we write

const handleScroll = (evt) => {
  const direction = evt.detail < 0 || evt.wheelDelta > 0 ? 1 : -1;
};

someEl.addEventListener("mousewheel", handleScroll, false);

to add the handleScroll function that checks if evt.detail is less than 0 or evt.wheelDelta is bigger than 0 and return 1.

Otherwise, we return -1.

evt.detail < 0 || evt.wheelDelta > 0 being true means we’re scrolling up.

Otherwise, we’re scrolling down.

Conclusion

To normalize mouse wheel speed across browsers with JavaScript, we just assign -1 or 1 depending on the direction of the wheel scrolling.