Categories
JavaScript Answers

How to add characters to a string in JavaScript?

Sometimes, we want to add characters to a string in JavaScript.

In this article, we’ll look at how to add characters to a string in JavaScript.

How to add characters to a string in JavaScript?

To add characters to a string in JavaScript, we can use the += operator.

For instance, we write

let text = "";
for (let l of list) {
  text += l;
}

to loop through the characters in the list array and concatenate them to text with += .

Conclusion

To add characters to a string in JavaScript, we can use the += operator.

Categories
JavaScript Answers

How to create a case insensitive regex in JavaScript?

Sometimes, we want to create a case insensitive regex in JavaScript.

In this article, we’ll look at how to create a case insensitive regex in JavaScript.

How to create a case insensitive regex in JavaScript?

To create a case insensitive regex in JavaScript, we can use the i flag.

For instance, we write

const value = "some text with dAtE";
const hasDate = /date/i.test(value);

to create the /date/i regex which matches 'date' with the letters in any case.

And then we call test on it with value to match 'dAtE'.

Conclusion

To create a case insensitive regex in JavaScript, we can use the i flag.

Categories
JavaScript Answers

How to do deep copy in ES6 using the spread syntax?

Sometimes, we want to do deep copy in ES6 using the spread syntax.

In this article, we’ll look at how to do deep copy in ES6 using the spread syntax.

How to do deep copy in ES6 using the spread syntax?

To do deep copy in ES6 using the spread syntax, we use JSON.stringify and JSON.parse.

For instance, we write

const oldObject = {
  name: "A",
  address: {
    street: "Station Road",
    city: "Pune",
  },
};
const newObject = JSON.parse(JSON.stringify(oldObject));

to call JSON.stringify with oldObject to return the JSON string version of oldObject.

Then we call JSON.parse to parse the JSON string back to an object to return a deep clone of oldObject.

Conclusion

To do deep copy in ES6 using the spread syntax, we use JSON.stringify and JSON.parse.

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.