Categories
JavaScript Answers

How to pass an array as a URL parameter with JavaScript?

Sometimes, we want to pass an array as a URL parameter with JavaScript.

In this article, we’ll look at how to pass an array as a URL parameter with JavaScript.

How to pass an array as a URL parameter with JavaScript?

To pass an array as a URL parameter with JavaScript, we can create our own query string with the values.

For instance, we write

const myArray = ["aaa", "bbb", "ccc"];

const myArrayQry = myArray
  .map((el, idx) => {
    return `myArray['${idx}']=${el}`;
  })
  .join("&");

to call map with a function that returns the query string part with "myArray['${idx}']" as the key and el as its value.

idx is the index of the item in myArray.

Conclusion

To pass an array as a URL parameter with JavaScript, we can create our own query string with the values.

Categories
JavaScript Answers

How to fix unexpected string concatenation with JavaScript?

Sometimes, we want to fix unexpected string concatenation with JavaScript.

In this article, we’ll look at how to fix unexpected string concatenation with JavaScript.

How to fix unexpected string concatenation with JavaScript?

To fix unexpected string concatenation with JavaScript, we can use template literals.

For instance, we write

const ANR = "Animal Friend,ANR,ANP,$30";
const specialityPlates = [
  {
    cName: "Environmental",
    oSubMenu: [
      {
        cName: `${ANR}`,
        cReturn: `${ANR}|27.00`,
      },
    ],
  },
];

to put the ANR into the template literal to return a string with ANR‘s content in it.

Conclusion

To fix unexpected string concatenation with JavaScript, we can use template literals.

Categories
JavaScript Answers

How to determine image file size and dimensions via JavaScript?

Sometimes, we want to determine image file size and dimensions via JavaScript.

In this article, we’ll look at how to determine image file size and dimensions via JavaScript.

How to determine image file size and dimensions via JavaScript?

To determine image file size and dimensions via JavaScript, we use the clientWidth and clientHeight properties.

For instance, we write

const img = document.getElementById("imageId");

const width = img.clientWidth;
const height = img.clientHeight;

to select the img element with getElementById.

We get the width with the clientWidth property and the height with the clientHeight property.

Conclusion

To determine image file size and dimensions via JavaScript, we use the clientWidth and clientHeight properties.

Categories
JavaScript Answers

How to close client connection with Node.js socket.io and JavaScript?

Sometimes, we want to close client connection with Node.js socket.io and JavaScript.

In this article, we’ll look at how to close client connection with Node.js socket.io and JavaScript.

How to close client connection with Node.js socket.io and JavaScript?

To close client connection with Node.js socket.io and JavaScript, we call the on method.

For instance, we write

socket.on("end", () => {
  socket.disconnect(0);
});

to call on to listen to the 'end' event on the server.

In the event handler, we call disconnect to close the client connection.

Then on client side, we write

const socket = io();
socket.emit("end");

to call socket.emit with 'end' to emit the end event to the server.

Conclusion

To close client connection with Node.js socket.io and JavaScript, we call the on method.

Categories
JavaScript Answers

How to add synchronous delay in code execution with JavaScript?

Sometimes, we want to add synchronous delay in code execution with JavaScript.

In this article, we’ll look at how to add synchronous delay in code execution with JavaScript.

How to add synchronous delay in code execution with JavaScript?

To add synchronous delay in code execution with JavaScript, we use a promise.

For instance, we write

const asyncWait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

(async () => {
  console.log("one");
  await asyncWait(5000);
  console.log("two");
})();

to create the asyncWait function that returns a promise that calls setTiimeout with ms milliseconds.

The promise is settled after resolve is called after ms milliseconds.

Then we call it in the async function with await to wait for the promise to finish before calling the 2nd console.log.

Conclusion

To add synchronous delay in code execution with JavaScript, we use a promise.