Categories
JavaScript Answers

How to check if element has any children in JavaScript?

Sometimes, we want to check if element has any children in JavaScript.

In this article, we’ll look at how to check if element has any children in JavaScript.

How to check if element has any children in JavaScript?

To check if element has any children in JavaScript, we can use various properties of the element.

For instance, we write

if (element.firstChild) {
  // ...
}

if (element.hasChildNodes()) {
  // ...
}

if (element.childNodes.length > 0) {
  // ...
}

to check if the firstChild is available with element.firstChild,.

If it’s set, then element has at least one child.

We can also call hasChildNodes to check if it has any child elements.

Or we can check if element.childNodes.length is bigger than 0 to do the same thing.

Conclusion

To check if element has any children in JavaScript, we can use various properties of the element.

Categories
JavaScript Answers

How to add the JavaScript equivalent of the Python pass statement that does nothing?

Sometimes, we want to add the JavaScript equivalent of the Python pass statement that does nothing.

In this article, we’ll look at how to add the JavaScript equivalent of the Python pass statement that does nothing.

How to add the JavaScript equivalent of the Python pass statement that does nothing?

To add the JavaScript equivalent of the Python pass statement that does nothing, we can add an empty block.

For instance, we write

if (condition) {
}

to add an empty if block which does nothing.

Conclusion

To add the JavaScript equivalent of the Python pass statement that does nothing, we can add an empty block.

Categories
JavaScript Answers

How to extend error in JavaScript with ES6 syntax?

Sometimes, we want to extend error in JavaScript with ES6 syntax.

In this article, we’ll look at how to extend error in JavaScript with ES6 syntax.

How to extend error in JavaScript with ES6 syntax?

To extend error in JavaScript with ES6 syntax, we can use the extends keyword.

For instance, we write

class ExtendableError extends Error {
  constructor(message) {
    super();
    this.message = message;
    this.stack = new Error().stack;
    this.name = this.constructor.name;
  }
}

to create the ExtendableError class which inherits from the Error class.

In the constructor, we call super to initialize the parent constructor.

Then we add the instance properties for the constructor instance with the next 3 lines.

Conclusion

To extend error in JavaScript with ES6 syntax, we can use the extends keyword.

Categories
JavaScript Answers

How to get objects value if its name contains dots with JavaScript?

Sometimes, we want to get objects value if its name contains dots with JavaScript.

In this article, we’ll look at how to get objects value if its name contains dots with JavaScript.

How to get objects value if its name contains dots with JavaScript?

To get objects value if its name contains dots with JavaScript, we can use square brackets.

For instance, we write

const smth = myData.list[0]["points.bean.pointsBase"][0].time;

to put "points.bean.pointsBase" in square brackets to access this property in the myData object.

Conclusion

To get objects value if its name contains dots with JavaScript, we can use square brackets.

Categories
JavaScript Answers

How to removing element from array in component state with React?

Sometimes, we want to removing element from array in component state with React.

In this article, we’ll look at how to removing element from array in component state with React.

How to removing element from array in component state with React?

To removing element from array in component state with React, we use the filter method.

For instance, we write

//...

class Comp extends React.Component {
  //...
  removeItem = (index) => {
    this.setState({
      data: this.state.data.filter((_, i) => i !== index),
    });
  };
  //...
}

to add the removeItem method into the Comp component that sets the data state to an array set as the original value of the data state but without the item at index.

We call filter with (_, i) => i !== index to return an array without the index.

Conclusion

To removing element from array in component state with React, we use the filter method.