Categories
JavaScript Answers

How to deserialize query string into a JSON object with JavaScript?

Sometimes, we want to deserialize query string into a JSON object with JavaScript

In this article, we’ll look at how to deserialize query string into a JSON object with JavaScript.

How to deserialize query string into a JSON object with JavaScript?

To deserialize query string into a JSON object with JavaScript, we can use the URLSearchParams constructor.

For instance, we write:

const url = new URL('https://example.com?foo=1&bar=2');
const params = new URLSearchParams(url.search);
const obj = Object.fromEntries([...params])
console.log(obj)

to create a new URL instance with the URL string.

Then we create the URLSearchParams instance with the url.search property which has the query string.

Next, we spread params into an array of key-value pair arrays.

Finally, we use Object.fromEntries to convert the array into an object.

Therefore, obj is

{
  bar: "2",
  foo: "1"
}

Conclusion

To deserialize query string into a JSON object with JavaScript, we can use the URLSearchParams constructor.

Categories
JavaScript Answers

How to compare times using moment.js and JavaScript?

Sometimes, we want to compare times using moment.js and JavaScript.

In this article, we’ll look at how to compare times using moment.js and JavaScript.

How to compare times using moment.js and JavaScript?

To compare times using moment.js and JavaScript, we can compare the moment objects.

For instance, we write:

const past = moment().subtract("days", 1)
const now = moment()
console.log(past < now)

to create 2 moment date objects, past and now.

Then we can compare them directly with the usual JavaScript comparison operators.

The objects will automatically be converted to timestamps before comparison.

Therefore, the console log should log true.

Conclusion

To compare times using moment.js and JavaScript, we can compare the moment objects.

Categories
JavaScript Answers

How to change the URL without reloading the page with JavaScript?

Sometimes, we want to change the URL without reloading the page with JavaScript.

In this article, we’ll look at how to change the URL without reloading the page with JavaScript.

How to change the URL without reloading the page with JavaScript?

To change the URL without reloading the page with JavaScript, we can use window.history.pushState.

For instance, we write:

window.history.pushState("state", "title", "/new-url");

to change the URL to /new-url.

The first argument is the state, which can be anything that is serializable.

Then 2nd argument is the title, which is ignored by most browsers.

Conclusion

To change the URL without reloading the page with JavaScript, we can use window.history.pushState.

Categories
JavaScript Answers

How to remove strings in beginning and end with JavaScript?

Sometimes, we want to remove strings in beginning and end with JavaScript.

In this article, we’ll look at how to remove strings in beginning and end with JavaScript.

How to remove strings in beginning and end with JavaScript?

To remove strings in beginning and end with JavaScript, we can use the string trim method.

For instance, we write:

const s = '  foo  '
const trimmed = s.trim()
console.log(trimmed)

to call s.trim to return a string without the whitespaces at the beginning and the end of s.

Therefore, trimmed is 'foo'.

Conclusion

To remove strings in beginning and end with JavaScript, we can use the string trim method.

Categories
JavaScript Answers

How to use string includes() in a JavaScript switch case?

Sometimes, we want to use string includes() in a JavaScript switch case.

In this article, we’ll look at how to use string includes() in a JavaScript switch case.

How to use string includes() in a JavaScript switch case?

To use string includes() in a JavaScript switch case, we should replace switch and case with if statements.

For instance, we write:

const id = "someId";
if (id.includes('product')) {
  //...
} else if (id.includes('user')) {
  //...
}

to call id.includes with the substrings we’re checking and run the code we want according to the checks.

Conclusion

To use string includes() in a JavaScript switch case, we should replace switch and case with if statements.