Categories
JavaScript Answers

How to create empty array of a given size with JavaScript?

Sometimes, we want to create empty array of a given size with JavaScript.

In this article, we’ll look at how to create empty array of a given size with JavaScript.

How to create empty array of a given size with JavaScript?

To create empty array of a given size with JavaScript, we use the Array constructor.

For instance, we write

const arr = Array(5);
console.log(arr.length);

to call Array with 5 to return an array with 5 empty slots.

Conclusion

To create empty array of a given size with JavaScript, we use the Array constructor.

Categories
JavaScript Answers

How to bind function arguments without binding this with JavaScript?

Sometimes, we want to bind function arguments without binding this with JavaScript.

In this article, we’ll look at how to bind function arguments without binding this with JavaScript.

How to bind function arguments without binding this with JavaScript?

To bind function arguments without binding this with JavaScript, we return a function that calls the function that we want to bind the arguments with.

For instance, we write

const bindArgs = (func, ...boundArgs) => {
  return (...args) => {
    return func(...boundArgs, ...args);
  };
};

const deleteGroup = bindArgs(deleteGroup, "groupName1");

to create the bindArgs function that returns a function that calls func with the arguments from the boundArgs array and the args that we pass into the function returned by bindArgs.

Then we call bindArgs with the deleteGroup and args being an array with 'groupName1'.

Conclusion

To bind function arguments without binding this with JavaScript, we return a function that calls the function that we want to bind the arguments with.

Categories
JavaScript Answers

How to return values from async functions using async-await from function with JavaScript?

To return values from async functions using async-await from function with JavaScript, we return the resolve value of the promise.

For instance, we write

const getData = async () => {
  const { data } = await axios.get(
    "https://jsonplaceholder.typicode.com/posts"
  );
  return data;
};

(async () => {
  console.log(await getData());
})();

to create the getData function that gets the resolve value of the promise returned by axios.get with await.

We extract the data property from the object returned by the promise and return it.

We then use await with getData to get the resolve value of the promise that was returned with return in getData.

Categories
JavaScript Answers

How to import module with a variable name in Node.js?

Sometimes, we want to import module with a variable name in Node.js.

In this article, we’ll look at how to import module with a variable name in Node.js.

How to import module with a variable name in Node.js?

To import module with a variable name in Node.js, we can call the import function.

For instance, we write

const importModule = async () => {
  try {
    const module = await import("./path/module.js");
  } catch (error) {
    console.error("import failed");
  }
};

to call import with the path string to the module.

It returns a promise with the module that we imported.

So we get the module with await.

Conclusion

To import module with a variable name in Node.js, we can call the import function.

Categories
JavaScript Answers

How to detect when an image fails to load in JavaScript?

Sometimes, we want to detect when an image fails to load in JavaScript.

In this article, we’ll look at how to detect when an image fails to load in JavaScript.

How to detect when an image fails to load in JavaScript?

To detect when an image fails to load in JavaScript, we can listen for the img element’s error event.

For instance, we write

const imageFound = () => {
  alert("That image is found and loaded");
};

const imageNotFound = () => {
  alert("That image was not found.");
};

const img = new Image();
img.onload = imageFound;
img.onerror = imageNotFound;
img.src = "http://foo.com/bar.jpg";

to create an img element with Image.

Then we set img.onerror to the imageNotFound function.

imageNotFound runs when the img with the img.src URL fails to load.

Conclusion

To detect when an image fails to load in JavaScript, we can listen for the img element’s error event.