Categories
JavaScript Answers

How to remove double spaces inside a string with JavaScript?

Sometimes, we want to remove double spaces inside a string with JavaScript.

In this article, we’ll look at how to remove double spaces inside a string with JavaScript.

How to remove double spaces inside a string with JavaScript?

To remove double spaces inside a string with JavaScript, we can call the string’s replace method with a regex.

For instance, we write:

const s = "hello         world"
const newS = s.replace(/\s{2,}/g, ' ');
console.log(newS)

We call replace with a regex that matches 2 or more spaces in s and replace them with single spaces.

Therefore, newS is 'hello world'.

Conclusion

To remove double spaces inside a string with JavaScript, we can call the string’s replace method with a regex.

Categories
JavaScript Answers

How to check for real touch support on a browser with JavaScript?

Sometimes, we want to check for real touch support on a browser with JavaScript.

In this article, we’ll look at how to check for real touch support on a browser with JavaScript.

How to check for real touch support on a browser with JavaScript?

To check for real touch support on a browser with JavaScript, we can use the matchMedia global function.

For instance, we write:

const isTouch = window.matchMedia("(any-hover: none)").matches
console.log(isTouch)

to call window.matchMedia with "(any-hover: none)" to check if the device the code is running on is a touch device.

If matches is true, then the code is running on a touch device.

Conclusion

To check for real touch support on a browser with JavaScript, we can use the matchMedia global function.

Categories
JavaScript Answers

How to pass parameter or argument to Axios interceptor with JavaScript?

Sometimes, we want to pass parameter or argument to Axios interceptor with JavaScript.

In this article, we’ll look at how to pass parameter or argument to Axios interceptor with JavaScript.

How to pass parameter or argument to Axios interceptor with JavaScript?

To pass parameter or argument to Axios interceptor with JavaScript, we can add it to the config.params object.

For instance, we write:

axios.interceptors.request.use((config) => {
  config.params = {
    ...config.params,
    myVar: 'value'
  }
  return config
})

We add the myVar property to config.params by spreading its existing properties into a new object and then add myVar after it.

Conclusion

To pass parameter or argument to Axios interceptor with JavaScript, we can add it to the config.params object.

Categories
JavaScript Answers

How to parse and convert exponential values to decimal in JavaScript?

Sometimes, we want to parse and convert exponential values to decimal in JavaScript.

In this article, we’ll look at how to parse and convert exponential values to decimal in JavaScript.

How to parse and convert exponential values to decimal in JavaScript?

To parse and convert exponential values to decimal in JavaScript, we can use the toFixed method.

For instance, we write:

const n = (4.656657507739E-10).toFixed(20)
console.log(n)

to call toFixed on 4.656657507739E-10 and return a number with 20 decimal places.

Therefore, n is 0.00000000046566575077.

toFixed can parse numbers that has up to 20 decimal places.

Conclusion

To parse and convert exponential values to decimal in JavaScript, we can use the toFixed method.

Categories
JavaScript Answers

How to detect browser time zone on client side with JavaScript?

Sometimes, we want to detect browser time zone on client side with JavaScript.

In this article, we’ll look at how to detect browser time zone on client side with JavaScript.

How to detect browser time zone on client side with JavaScript?

To detect browser time zone on client side with JavaScript, we can use the Internationalization API.

For instance, we write:

console.log(Intl.DateTimeFormat().resolvedOptions().timeZone)

to use Intl.DateTimeFormat().resolvedOptions().timeZone to get the time zone from the browser the device is running on.

Therefore, if we’re in the Pacific time zone, we see 'America/Los_Angeles' logged.

Conclusion

To detect browser time zone on client side with JavaScript, we can use the Internationalization API.