Categories
JavaScript Answers

How to force download of a base64 image?

Sometimes, we want to force download of a base64 image with JavaScript.

in this article, we’ll look at how to force download of a base64 image with JavaScript.

How to force download of a base64 image?

To force download of a base64 image with JavaScript, we add the download attribute to the anchor element.

For instance, we write

<a href="data:image/jpeg;base64,/9j/4AAQSkZ..." download="filename.jpg">
  download
</a>

to add the anchor element with the href attribute set the base64 image URL.

And we add the download attribute and set it to the file name of the downloaded image to let users download the image when the link is clicked.

Conclusion

To force download of a base64 image with JavaScript, we add the download attribute to the anchor element.

Categories
JavaScript Answers

How to convert special characters to HTML in JavaScript?

Sometimes, we want to convert special characters to HTML in JavaScript.

In this article, we’ll look at how to convert special characters to HTML in JavaScript.

How to convert special characters to HTML in JavaScript?

To convert special characters to HTML in JavaScript, we can us the String.fromCharcode method.

For instance, we write

const decodeHtmlCharCodes = (str) =>
  str.replace(/(&#(\d+);)/g, (match, capture, charCode) =>
    String.fromCharCode(charCode)
  );

console.log(decodeHtmlCharCodes("&#8217;"));

to create the decodeHtmlCharCodes function that calls str.replace with a regex to match all HTML entities with /(&#(\d+);)/g.

And we replace them all with the character that’s returned from String.fromCharCode(charCode).

The charCode in the callback is the character code of the whole HTML entity.

Conclusion

To convert special characters to HTML in JavaScript, we can us the String.fromCharcode method.

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 use a for loop.

For instance, we write

let row = 20;
let column = 10;
let f = new Array();

for (i = 0; i < row; i++) {
  f[i] = new Array();
  for (j = 0; j < column; j++) {
    f[i][j] = 0;
  }
}

to create a nested for loop that adds the nested arrays to the f array with the outer loop.

In the inner loop, we fill the nested arrays with 0’s with

for (j = 0; j < column; j++) {
  f[i][j] = 0;
}

Conclusion

To create multidimensional array with JavaScript, we can use a for loop.

Categories
JavaScript Answers

How to execute shell command in JavaScript?

Sometimes, we want to execute shell command in JavaScript.

In this article, we’ll look at how to execute shell command in JavaScript.

How to execute shell command in JavaScript?

To execute shell command in JavaScript, we can use the exec function.

For instance, we write

const { exec } = require("child_process");

exec("cat *.js bad_file | wc -l", (error, stdout, stderr) => {
  console.log("stdout: " + stdout);
  console.log("stderr: " + stderr);
  if (error !== null) {
    console.log("exec error: " + error);
  }
});

to call the child_process module’s exec function.

We call it with the command we want to run in a string.

The 2nd argument is a callback that gets the output from the command.

The stdout variable has the standard output output.

And stderr has the standard error output.

error has the errors is there’s any.

Conclusion

To execute shell command in JavaScript, we can use the exec function.

Categories
JavaScript Answers

How to round to two decimal places with JavaScript?

Sometimes, we want to round to two decimal places with JavaScript.

In this article, we’ll look at how to round to two decimal places with JavaScript.

How to round to two decimal places with JavaScript?

To round to two decimal places with JavaScript, we can use toFixed and the + operator.

For instance, we write

const r = +discount.toFixed(2)

to round the discount number to 2 decimal places by first calling toFixed with 2 to convert it to a string that has discount rounded to 2 decimal places.

And then we add + in front of it to convert the rounded number string back to a number.

Conclusion

To round to two decimal places with JavaScript, we can use toFixed and the + operator.