Categories
JavaScript Answers

How to use switch on ranges of integers in JavaScript?

Sometimes, we want to use switch on ranges of integers in JavaScript.

In this article, we’ll look at how to use switch on ranges of integers in JavaScript.

How to use switch on ranges of integers in JavaScript?

To use switch on ranges of integers in JavaScript, we use true as the value of switch and add the booleans for the range check in case.

For instance, we write

switch (true) {
  case x < 5:
    alert("less than five");
    break;
  case x < 9:
    alert("between 5 and 8");
    break;
  case x < 12:
    alert("between 9 and 11");
    break;
  default:
    alert("none");
    break;
}

to use switch with true.

And then we add the boolean expressions we want to check for case to do the range checks.

Conclusion

To use switch on ranges of integers in JavaScript, we use true as the value of switch and add the booleans for the range check in case.

Categories
JavaScript Answers

How to retrieve the text of the selected option in the select element with JavaScript?

Sometimes, we want to retrieve the text of the selected option in the select element with JavaScript.

In this article, we’ll look at how to retrieve the text of the selected option in the select element with JavaScript.

How to retrieve the text of the selected option in the select element with JavaScript?

To retrieve the text of the selected option in the select element with JavaScript, we use the selectedOptions property.

For instance, we write

const selected = document.getElementById("test").selectedOptions[0].text;

to get the select element with getElementById.

Then we get the selected options from it with selectedOptions.

We get the first selected option with [0].

And we get the text of the selected option with text.

Conclusion

To retrieve the text of the selected option in the select element with JavaScript, we use the selectedOptions property.

Categories
JavaScript Answers

How to convert JavaScript date time to MySQL datetime?

Sometimes, we want to convert JavaScript date time to MySQL datetime.

In this article, we’ll look at how to convert JavaScript date time to MySQL datetime.

How to convert JavaScript date time to MySQL datetime?

To convert JavaScript date time to MySQL datetime, we can call the date’s toISOString method.

For instance, we write

const date = new Date().toISOString().slice(0, 19).replace("T", " ");

to call toISOString to return a date string of the date in ISO8601 format.

Then we call slice with 0 and 19 to get the datetime part of the string.

And we replace 'T' with a space with replace.

Conclusion

To convert JavaScript date time to MySQL datetime, we can call the date’s toISOString method.

Categories
JavaScript Answers

How to get GPS location from the web browser with JavaScript?

Sometimes, we want to get GPS location from the web browser with JavaScript.

In this article, we’ll look at how to get GPS location from the web browser with JavaScript.

How to get GPS location from the web browser with JavaScript?

To get GPS location from the web browser with JavaScript, we can use the navigator.geolocation.getCurrentPosition method.

For instance, we write

navigator.geolocation.getCurrentPosition((location) => {
  console.log(location.coords.latitude);
  console.log(location.coords.longitude);
  console.log(location.coords.accuracy);
});

to call getCurrentLocation with a callback that has the location object parameter.

We get the latitude, longitude, and accuracy from the location.coords property.

Conclusion

To get GPS location from the web browser with JavaScript, we can use the navigator.geolocation.getCurrentPosition method.

Categories
React Answers

How to fix this.setState isn’t merging states as expected with React?

Sometimes, we want to fix this.setState isn’t merging states as expected with React.

In this article, we’ll look at how to fix this.setState isn’t merging states as expected with React.

How to fix this.setState isn’t merging states as expected with React?

To fix this.setState isn’t merging states as expected with React, we make a copy of the original state object, modify it, and then call setState with the modified copied object.

For instance, we write

const { selected: _selected } = this.state;
const selected = { ..._selected, name: "Barfoo" };
this.setState({ selected });

to copy the _selected state with ....

Then we add the name property to it.

Then we assign that to selected and call setState with { selected } to update it.

Conclusion

To fix this.setState isn’t merging states as expected with React, we make a copy of the original state object, modify it, and then call setState with the modified copied object.