Categories
JavaScript Answers

How to check if a number is negative with JavaScript?

Sometimes, we want to check if a number is negative with JavaScript.

In this article, we’ll look at how to check if a number is negative with JavaScript.

How to check if a number is negative with JavaScript?

To check if a number is negative with JavaScript, we use the Math.sign method.

For instance, we write

const number = 1;

if (Math.sign(number) === 1) {
  console.log("I'm positive");
} else if (Math.sign(number) === -1) {
  console.log("I'm negative");
} else {
  console.log("I'm not a number");
}

to call Math.sign with number.

Since 1 positive, Math.sign returns 1.

If a number is negative, it returns -1.

If the number is positive zero, returns 0.

If the number is negative zero, returns -0.

Otherwise, NaN is returned.

Conclusion

To check if a number is negative with JavaScript, we use the Math.sign method.

Categories
JavaScript Answers

How to get image from canvas element and use it in img src tag with JavaScript?

Sometimes, we want to get image from canvas element and use it in img src tag with JavaScript.

In this article, we’ll look at how to get image from canvas element and use it in img src tag with JavaScript.

How to get image from canvas element and use it in img src tag with JavaScript?

To get image from canvas element and use it in img src tag with JavaScript, we can use the canvas toDataURL method.

For instance, we write

const image = new Image();
image.id = "pic";
image.src = canvas.toDataURL();
document.getElementById("image-for-crop").appendChild(image);

to call canas.toDataURL to return the canvas image a base64 URL string.

Then we set its as the src attribute value of the image img element to show the image in the base64 URL.

Conclusion

To get image from canvas element and use it in img src tag with JavaScript, we can use the canvas toDataURL method.

Categories
JavaScript Answers

How to check if DOM is ready without a framework with JavaScript?

Sometimes, we want to check if DOM is ready without a framework with JavaScript.

In this article, we’ll look at how to check if DOM is ready without a framework with JavaScript.

How to check if DOM is ready without a framework with JavaScript?

To check if DOM is ready without a framework with JavaScript, we can listen for the DOMContentLoaded event.

For instance, we write

document.addEventListener("DOMContentLoaded", (event) => {
  console.log("DOM fully loaded and parsed");
});

to listen for the DOMContentLoaded event with addEventListener.

We call it with a callback that runs when the DOM is fully loaded.

Conclusion

To check if DOM is ready without a framework with JavaScript, we can listen for the DOMContentLoaded event.

Categories
JavaScript Answers

How to dynamically create HTML5 canvas with JavaScript?

Sometimes, we want to dynamically create HTML5 canvas with JavaScript.

In this article, we’ll look at how to dynamically create HTML5 canvas with JavaScript.

How to dynamically create HTML5 canvas with JavaScript?

To dynamically create HTML5 canvas with JavaScript, we can use the createElement method.

For instance, we write

const canvas = document.createElement("canvas");
canvas.id = "canvas";
canvas.width = 1224;
canvas.height = 768;
canvas.style.zIndex = 8;
canvas.style.position = "absolute";
canvas.style.border = "1px solid";

to create the canvas element with createElement.

Then we set its id, width, height, zIndex, position, and border. to set its size, position, border and ID.

Conclusion

To dynamically create HTML5 canvas with JavaScript, we can use the createElement method.

Categories
JavaScript Answers

How to get width in pixels from element with style set with % with JavaScript?

Sometimes, we want to get width in pixels from element with style set with % with JavaScript.

In this article, we’ll look at how to get width in pixels from element with style set with % with JavaScript.

How to get width in pixels from element with style set with % with JavaScript?

To get width in pixels from element with style set with % with JavaScript, we can use the offsetWidth property.

For instance, we write

document.querySelector("div").offsetWidth;

to select the div with querySelector.

Then we get the width of the element with offsetWidth in pixels.

Conclusion

To get width in pixels from element with style set with % with JavaScript, we can use the offsetWidth property.