Categories
JavaScript Answers

How to get query parameters’ hash fragment with React-Router v4?

Sometimes, we want to get query parameters’ hash fragment with React-Router v4.

In this article, we’ll look at how to get query parameters’ hash fragment with React-Router v4.

How to get query parameters’ hash fragment with React-Router v4?

To get query parameters’ hash fragment with React-Router v4, we can use the query-string library.

To install it, we run

npm install -save query-string

Then we use it by writing

const queryString = require("query-string");
const parsed = queryString.parse(this.props.location.search);
console.log(parsed.param);

in our component.

We call queryString.parse with the query string we get from this.props.location.search.

And then we get the parameters as an object with parsed.param.

Conclusion

To get query parameters’ hash fragment with React-Router v4, we can use the query-string library.

Categories
JavaScript Answers

How to select sibling nodes with JavaScript?

Sometimes, we want to select sibling nodes with JavaScript.

In this article, we’ll look at how to select sibling nodes with JavaScript.

How to select sibling nodes with JavaScript?

To select sibling nodes with JavaScript, we can use various properties of an element.

For instance, we write

const previousSibling = node.previousSibling;
const nextSibling = node.nextSibling;

to use the previousSibling to get the previous sibling node of node.

And we use the nextSibling to get the next sibling node of node.

These properties will return any type of nodes.

To return only element nodes, we write

const previousSibling = node.previousElementSibling;
const nextSibling = node.nextElementSibling;

to use the previousElementSibling to get the previous sibling element of node.

And we use the nextElementSibling to get the next sibling element of node.

Conclusion

To select sibling nodes with JavaScript, we can use various properties of an element.

Categories
JavaScript Answers

How to check if a string is entirely made of the same substring with JavaScript?

Sometimes, we want to check if a string is entirely made of the same substring with JavaScript.

In this article, we’ll look at how to check if a string is entirely made of the same substring with JavaScript.

How to check if a string is entirely made of the same substring with JavaScript?

To check if a string is entirely made of the same substring with JavaScript, we can use the indexOf method with a string that concatenated with itself.

For instance, we write

const check = (str) => {
  return (str + str).indexOf(str, 1) !== str.length;
};

to check if the start index of str in str + str when searching from index 1 isn’t str.length.

If this is true, then str isn’t entirely made of the same substring as itself.

Conclusion

To check if a string is entirely made of the same substring with JavaScript, we can use the indexOf method with a string that concatenated with itself.

Categories
JavaScript Answers

How to do deep clone in JavaScript?

Sometimes, we want to do deep clone in JavaScript.

In this article, we’ll look at how to do deep clone in JavaScript.

How to do deep clone in JavaScript?

To do deep clone in JavaScript, we use the structuredClone function.

For instance, we write

const original = { name: "example" };

const clone = structuredClone(original);

to call structuredClone with the original object to return a deep clone of the original object.

structuredClone is built into modern browsers.

Conclusion

To do deep clone in JavaScript, we use the structuredClone function.

Categories
JavaScript Answers

How to concatenate two numbers in JavaScript?

Sometimes, we want to concatenate two numbers in JavaScript.

In this article, we’ll look at how to concatenate two numbers in JavaScript.

How to concatenate two numbers in JavaScript?

To concatenate two numbers in JavaScript, we can use template literals.

For instance, we write

const a = 5;
const b = 6;
const numbersAsString = `${a}${b}`;

console.log(numbersAsString);

to interpolate the values a and b into the numersAsString template literal.

Then numbersAsString is '56'.

Conclusion

To concatenate two numbers in JavaScript, we can use template literals.