Categories
JavaScript Answers

How to sort an ES6 Set with JavaScript?

Sometimes, we want to sort an ES6 Set with JavaScript.

In this article, we’ll look at how to sort an ES6 Set with JavaScript.

How to sort an ES6 Set with JavaScript?

To sort an ES6 Set with JavaScript, we can convert it to an array and then sort it.

For instance, we write

const sorted = Array.from(new Set(["b", "a", "c"])).sort();

to convert the set to an array with the Array.from method.

Then we call the sort method on it to return an array with the sorted entries.

Conclusion

To sort an ES6 Set with JavaScript, we can convert it to an array and then sort it.

Categories
JavaScript Answers

How to add class to html element with JavaScript?

Sometimes, we want to add class to html element with JavaScript.

In this article, we’ll look at how to add class to html element with JavaScript.

How to add class to html element with JavaScript?

To add class to html element with JavaScript, we can use the classList.add method.

For instance, we write

const [root] = document.getElementsByTagName("html");
root.classList.add("myCssClass");

to select all html elements with getElementsByTagName, get the first one, and assign it to root.

Then we call root.classList.add to 'myCssClass' to add the myCssClass class to the html element.

Conclusion

To add class to html element with JavaScript, we can use the classList.add method.

Categories
JavaScript Answers

How to get city name from a latitude and longitude point with JavaScript?

Sometimes, we want to get city name from a latitude and longitude point with JavaScript.

In this article, we’ll look at how to get city name from a latitude and longitude point with JavaScript.

How to get city name from a latitude and longitude point with JavaScript?

To get city name from a latitude and longitude point with JavaScript, we can use the node-geocode library.

To install it, we run

npm i node-geocoder

Then we write

const NodeGeocoder = require("node-geocoder");

const options = {
  provider: "google",
  httpAdapter: "https",
  apiKey: "...",
  formatter: "json",
};

const geocoder = NodeGeocoder(options);

geocoder.reverse({ lat: 42.5967439, lon: 77.3285038 }, (err, res) => {
  console.log(res);
});

to create the NodeGeocode object with the options object.

apiKey is the API key for for Mapquest, OpenCage, or Google Premier.

formatter returns the result in 'json' format.

We can also return it as a 'gpx' or 'string'.

Then we call reverse with the lat latitude and lon longitude.

And we get the res from the callback which has the city included.

Conclusion

To get city name from a latitude and longitude point with JavaScript, we can use the node-geocode library.

Categories
JavaScript Answers

How to reset the setInterval timer with JavaScript?

Sometimes, we want to reset the setInterval timer with JavaScript.

In this article, we’ll look at how to reset the setInterval timer with JavaScript.

How to reset the setInterval timer with JavaScript?

To reset the setInterval timer with JavaScript, we can use the clearInterval function.

For instance, we write

const myFn = () => {
  console.log("idle");
};

let myTimer = setInterval(myFn, 4000);

//...
clearInterval(myTimer);
myTimer = setInterval(myFn, 4000);

to call setInterval with myFn to run the function every 4 seconds.

We assign the return timer ID number to myTimer.

Then we call clearInterval with myTimer to stop the timer.

Next, we call setInterval with the same arguments and then we assign the returned timer number to myTimer again.

Conclusion

To reset the setInterval timer with JavaScript, we can use the clearInterval function.

Categories
JavaScript Answers

How to do exponentiation in JavaScript?

Sometimes, we want to do exponentiation in JavaScript.

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

How to do exponentiation in JavaScript?

To do exponentiation in JavaScript, we can use the Math.pow method or the ** operator.

For instance, we write

const a = Math.pow(12, 2);

or

const a = 12 ** 2;

to call Math.pow to raise 12 to the power of 2.

We can also do the same thing with the ** operator.

Therefore, a is 144.

Conclusion

To do exponentiation in JavaScript, we can use the Math.pow method or the ** operator.