Categories
JavaScript Answers

How to check with JavaScript if connection is local host?

Sometimes, we want to check with JavaScript if connection is local host.

In this article, we’ll look at how to check with JavaScript if connection is local host.

How to check with JavaScript if connection is local host?

To check with JavaScript if connection is local host, we can check the value of location.hostname.

For instance, we write

if (location.hostname === "localhost" || location.hostname === "127.0.0.1") {
  alert("It's a local server!");
}

to check if location.hostname is 'localhost' or '127.0.0.1'.

If either of these are true, then we’re running our app on localhost.

Conclusion

To check with JavaScript if connection is local host, we can check the value of location.hostname.

Categories
JavaScript Answers

How to make a div visible and invisible with JavaScript?

Sometimes, we want to make a div visible and invisible with JavaScript.

In this article, we’ll look at how to make a div visible and invisible with JavaScript.

How to make a div visible and invisible with JavaScript?

To make a div visible and invisible with JavaScript, we set the display or visibility properties.

For instance, we write

elem.style.display = 'block';
elem.style.display = 'inline';

elem.style.visibility = 'visible';

to set the display property to 'block' or 'inline' to make the elem element display as a block or inline element.

Or we set visbility to 'visible' to show the element.

To hide the element, we write

elem.style.display = 'none'; 

elem.style.visibility = 'hidden';

to hide the elem element by setting display to 'none' without keeping the space for the element.

Or we hide the element and keep the space for the element by setting visibility to 'hidden‘.

Conclusion

To make a div visible and invisible with JavaScript, we set the display or visibility properties.

Categories
JavaScript Answers

How to create an abstract base class in JavaScript?

Sometimes, we want to create an abstract base class in JavaScript.

In this article, we’ll look at how to create an abstract base class in JavaScript.

How to create an abstract base class in JavaScript?

To create an abstract base class in JavaScript, we can create a class that other classes can inherit from.

For instance, we write

class Animal {
  constructor() {
    if (this.constructor === Animal) {
      throw new Error("Abstract classes can't be instantiated.");
    }
  }

  say() {
    throw new Error("Method 'say()' must be implemented.");
  }

  eat() {
    console.log("eating");
  }
}

class Dog extends Animal {
  say() {
    console.log("bark");
  }
}

to create the Animal class which throws errors for methods that should be implemented by child classes.

In Animal‘s constructor, we throw an error if the current class is Animal so that we can’t instantiate Animal directly.

Then we create the Dog class, which is a child class of Animal, that implements the methods by overriding the ones in Animal.

Conclusion

To create an abstract base class in JavaScript, we can create a class that other classes can inherit from.

Categories
JavaScript Answers

How to create array with function similar to Python range() with JavaScript?

Sometimes, we want to create array with function similar to Python range() with JavaScript.

In this article, we’ll look at how to create array with function similar to Python range() with JavaScript.

How to create array with function similar to Python range() with JavaScript?

To create array with function similar to Python range() with JavaScript, we can use the Array constructor and the spread operator.

For instance, we write

const range = (n) => [...Array(n).keys()];

to create an array with length n with Array(n).

Then we call keys to return an array of indexes of the array.

And then we spread the indexes of the array into a new array.

Conclusion

To create array with function similar to Python range() with JavaScript, we can use the Array constructor and the spread operator.

Categories
JavaScript Answers

How to add or update an attribute to an HTML element using JavaScript?

Sometimes, we want to add or update an attribute to an HTML element using JavaScript.

In this article, we’ll look at how to add or update an attribute to an HTML element using JavaScript.

How to add or update an attribute to an HTML element using JavaScript?

To add or update an attribute to an HTML element using JavaScript, we can set the property in the attributes property.

For instance, we write

const e = document.createElement("div");
e.attributes["id"] = "div1";

to create a div with createElement.

Then we set the id attribute of the div with

e.attributes["id"] = "div1";

Conclusion

To add or update an attribute to an HTML element using JavaScript, we can set the property in the attributes property.