Categories
JavaScript Answers

How to pass a reference of a variable that is immutable as argument in a function with JavaScript?

Sometimes, we want to pass a reference of a variable that is immutable as argument in a function with JavaScript.

In this article, we’ll look at how to pass a reference of a variable that is immutable as argument in a function with JavaScript.

How to pass a reference of a variable that is immutable as argument in a function with JavaScript?

To pass a reference of a variable that is immutable as argument in a function with JavaScript, we can make the function accept an object.

For instance, we write

const x = { value: 0 };

const a = (obj) => {
  obj.value++;
};

a(x);
console.log(x.value);

to define the x object and a function.

In a, we increment the value property by 1.

Then we call a with x.

As a result, x.value is 1 since objects are passed by reference.

Conclusion

To pass a reference of a variable that is immutable as argument in a function with JavaScript, we can make the function accept an object.

Categories
JavaScript Answers

How to detect if browser supports HTML5 Local Storage with JavaScript?

Sometimes, we want to detect if browser supports HTML5 Local Storage with JavaScript.

In this article, we’ll look at how to detect if browser supports HTML5 Local Storage with JavaScript.

How to detect if browser supports HTML5 Local Storage with JavaScript?

To detect if browser supports HTML5 Local Storage with JavaScript, we check if Storage is defined.

For instance, we write

if (typeof Storage !== "undefined") {
  // ...
} else {
  // ...
}

to check if Storage isn’t undefined with typeof Storage !== "undefined".

If it’s true, then local storage is available.

Otherwise, it’s not available.

Conclusion

To detect if browser supports HTML5 Local Storage with JavaScript, we check if Storage is defined.

Categories
JavaScript Answers

How to replace CSS file on the fly and apply the new style to the page with JavaScript?

Sometimes, we want to replace CSS file on the fly and apply the new style to the page with JavaScript.

In this article, we’ll look at how to replace CSS file on the fly and apply the new style to the page with JavaScript.

How to replace CSS file on the fly and apply the new style to the page with JavaScript?

To replace CSS file on the fly and apply the new style to the page with JavaScript, we can set the media attribute of the link element.

For instance, we write

<link rel="stylesheet" href="main.css" />
<link rel="stylesheet" href="light.css" media="none" id="light" />
<link rel="stylesheet" href="dark.css" media="none" id="dark" />

to add a few style sheets.

Then we write

const enableStylesheet = (node) => {
  node.media = "";
};

const disableStylesheet = (node) => {
  node.media = "none";
};

to define the enableStylesheet and disableStylesheet functions.

We set node.media to an empty string in enableStylesheet to enable the stylesheet.

And we set node.media to 'none' in disableStylesheet to disable the stylesheet.

Conclusion

To replace CSS file on the fly and apply the new style to the page with JavaScript, we can set the media attribute of the link element.

Categories
JavaScript Answers

How to offset the center point in Google Maps API V3 and JavaScript?

Sometimes, we want to offset the center point in Google Maps API V3 and JavaScript.

In this article, we’ll look at how to offset the center point in Google Maps API V3 and JavaScript.

How to offset the center point in Google Maps API V3 and JavaScript?

To offset the center point in Google Maps API V3 and JavaScript, we can use the panTo method.

For instance, we write

const offsetX = 0.25;
const offsetY = 0.25;

const span = map.getBounds().toSpan();

const newCenter = {
  lat: center.lat() + span.lat() * offsetY,
  lng: center.lng() + span.lng() * offsetX,
};

map.panTo(newCenter);

to get the latitude and longitude from center with lat and lng respectively.

Then we set the new lat and lng in the newCenter object.

Next, we call panTo with newCenter to pan the map to the new center coordinates.

Conclusion

To offset the center point in Google Maps API V3 and JavaScript, we can use the panTo method.

Categories
JavaScript Answers

How to match an empty dictionary in JavaScript?

Sometimes, we want to match an empty dictionary in JavaScript.

In this article, we’ll look at how to match an empty dictionary in JavaScript.

How to match an empty dictionary in JavaScript?

To match an empty dictionary in JavaScript, we can use the Object.keys method.

For instance, we write

const isEmpty = (obj) => {
  return Object.keys(obj).length === 0;
};

to define the isEmpty function.

In it, we call Object.keys on object obj to return an array of non-inherited string property keys.

Then we get the length of the array and check if it’s 0 to check if we have an empty object.

Conclusion

To match an empty dictionary in JavaScript, we can use the Object.keys method.