Categories
JavaScript Answers

How to call preventDefault() on an anchor element with JavaScript?

Sometimes, we want to call preventDefault() on an anchor element with JavaScript.

In this article, we’ll look at how to call preventDefault() on an anchor element with JavaScript.

How to call preventDefault() on an anchor element with JavaScript?

To call preventDefault() on an anchor element with JavaScript, we can call preventDefault in an event handler function.

For instance, we write

<a href="#">Click Me</a>

to add an anchor element.

Then we write

const a = document.querySelector("a");
a.onclick = (event) => {
  event.preventDefault();
};

to select the anchor element with querySelector.

And then we set a.onclick to a function that runs when we click on the anchor element.

In it, we call event.preventDefault to prevent the default behavior of the element.

Conclusion

To call preventDefault() on an anchor element with JavaScript, we can call preventDefault in an event handler function.

Categories
JavaScript Answers

How to add try block without catch block in JavaScript?

Sometimes, we want to add try block without catch block in JavaScript.

In this article, we’ll look at how to add try block without catch block in JavaScript.

How to add try block without catch block in JavaScript?

To add try block without catch block in JavaScript, we can add an empty catch or finally block.

For instance, we write

try {
  throw new Error("This won't show anything");
} catch {}

try {
  throw new Error("This will get logged");
} finally {
  //...
}

to add a catch or finally block after the try block.

Adding an empty catch would swallow the error and adding a finally block wouldn’t swallow the error but let us run code regardless of whether an exception is thrown.

Conclusion

To add try block without catch block in JavaScript, we can add an empty catch or finally block.

Categories
JavaScript Answers

How to do alphanumeric check in JavaScript?

Sometimes, we want to do alphanumeric check in JavaScript.

In this article, we’ll look at how to do alphanumeric check in JavaScript.

How to do way to alphanumeric check in JavaScript?

To do alphanumeric check in JavaScript, we can use a regex.

For instance, we write

const isAlphanumeric = /^[a-z0-9]+$/i.test("abc");

to call ^[a-z0-9]+$/i.test with the string that we want to check if it’s alphanumeric or not.

We use ^[a-z0-9]+$ to check if the whole string only has letters and digits.

And we use i to check that in a case insensitive manner.

Conclusion

To do alphanumeric check in JavaScript, we can use a regex.

Categories
JavaScript Answers

How to remove all files from directory without removing directory in Node.js?

Sometimes, we want to remove all files from directory without removing directory in Node.js.

In this article, we’ll look at how to remove all files from directory without removing directory in Node.js.

How to remove all files from directory without removing directory in Node.js?

To remove all files from directory without removing directory in Node.js, we can use fs.readdir to read the file paths from a directory.

And then we use fs.unlink to delete each file with each path.

For instance, we write

const fs = require("fs");
const path = require("path");

const directory = "test";

fs.readdir(directory, (err, files) => {
  if (err) throw err;

  for (const file of files) {
    fs.unlink(path.join(directory, file), (err) => {
      if (err) throw err;
    });
  }
});

to call readdir with directory to read the file paths in directory.

Then we loop through the files paths in the callback to delete all the listed files.

In the loop, we call fs.unlink with the full path returned by path.join(directory, file) to delete the file.

Conclusion

To remove all files from directory without removing directory in Node.js, we can use fs.readdir to read the file paths from a directory.

And then we use fs.unlink to delete each file with each path.

Categories
JavaScript Answers

How to create multidimensional array with JavaScript?

Sometimes, we want to create multidimensional array with JavaScript.

In this article, we’ll look at how to create multidimensional array with JavaScript.

How to create multidimensional array with JavaScript?

To create multidimensional array with JavaScript, we can assign the values to the position with the given indexes.

For instance, we write

const numeric = [
  ["input1", "input2"],
  ["input3", "input4"],
];
numeric[0][0] == "input1";
numeric[0][1] == "input2";
numeric[1][0] == "input3";
numeric[1][1] == "input4";

by putting arrays into arrays like

const numeric = [
  ["input1", "input2"],
  ["input3", "input4"],
];

We also assign values to the positions we want with

numeric[0][0] == "input1";
numeric[0][1] == "input2";
numeric[1][0] == "input3";
numeric[1][1] == "input4";

where the first index is the position of the outer array and the 2nd is the position of the inner array.

Conclusion

To create multidimensional array with JavaScript, we can assign the values to the position with the given indexes.