Categories
JavaScript Answers

How to calculate percentage with JavaScript?

Sometimes, we want to calculate percentage with JavaScript.

In this article, we’ll look at how to calculate percentage with JavaScript.

How to calculate percentage with JavaScript?

To calculate percentage with JavaScript, we use arithmetic operators.

For instance, we write

const percentage = (partialValue, totalValue) => {
  return (100 * partialValue) / totalValue;
};

const totalActivities = 10;
const doneActivities = 2;

const result = percentage(doneActivities, totalActivities);

to define the percentage function that multiplies partialValue by 100 and then divide the product by totalValue to get the percentage.

Conclusion

To calculate percentage with JavaScript, we use arithmetic operators.

Categories
JavaScript Answers

How to check for null inline in JavaScript?

Sometimes, we want to check for null inline in JavaScript.

In this article, we’ll look at how to check for null inline in JavaScript.

How to check for null inline in JavaScript?

To check for null inline in JavaScript, we use the optional chaining operator.

For instance, we write

const example = { a: ["first", { b: 3 }, false] };

console.log(example?.a);
console.log(example?.b);

console.log(example?.a?.[0]);
console.log(example?.a?.[1]?.a);
console.log(example?.a?.[1]?.b);

to use the optional chaining ?. operator to return undefined if any property in the chain is null or undefined.

Conclusion

To check for null inline in JavaScript, we use the optional chaining operator.

Categories
JavaScript Answers

How to close Bootstrap dropdown after link click with HTML?

Sometimes, we want to close Bootstrap dropdown after link click with HTML.

In this article, we’ll look at how to close Bootstrap dropdown after link click with HTML.

How to close Bootstrap dropdown after link click with HTML?

To close Bootstrap dropdown after link click with HTML, we add the dropdown-toggle class.

For instance, we write

<a class="dropdown-toggle"></a>

to add the dropdown-toggle class to the link to close Bootstrap dropdown after link click with HTML.

Conclusion

To close Bootstrap dropdown after link click with HTML, we add the dropdown-toggle class.

Categories
JavaScript Answers

How to get width and height of an image with JavaScript FileReader?

Sometimes, we want to get width and height of an image with JavaScript FileReader.

In this article, we’ll look at how to get width and height of an image with JavaScript FileReader.

How to get width and height of an image with JavaScript FileReader?

To get width and height of an image with JavaScript FileReader, we use the Image constructor.

For instance, we write

<input type="file" name="profileImg" accept="image/*" class="input-file" />

to add a file input.

Then we write

const input = document.querySelector("input");
input.onchange = (event) => {
  const reader = new FileReader();

  reader.onload = () => {
    const image = new Image();
    image.src = reader.result;
    image.onload = () => {
      consoe.log(image.width);
    };
  };
  const [file] = event.target.files;
  reader.readAsDataURL(file);
};

to select the input with querySelector.

And then we set its onchange property to a function that creates a FileReader object.

We set its onload property to a function that gets the read file data.

In it, we create an image with the Image constructor.

And we set image.src to the read file result which is a base64 URL string.

Then image.width has the width.

We get the file from the input with event.target.files.

And we call readAsDataURL to read the file into the base64 URL.

Conclusion

To get width and height of an image with JavaScript FileReader, we use the Image constructor.

Categories
TypeScript Answers

How to fix Element implicitly has an ‘any’ type because expression of type ‘string’ can’t be used to index type with TypeScript?

Sometimes, we want to fix Element implicitly has an ‘any’ type because expression of type ‘string’ can’t be used to index type with TypeScript.

In this article, we’ll look at how to fix Element implicitly has an ‘any’ type because expression of type ‘string’ can’t be used to index type with TypeScript.

How to fix Element implicitly has an ‘any’ type because expression of type ‘string’ can’t be used to index type with TypeScript?

To fix Element implicitly has an ‘any’ type because expression of type ‘string’ can’t be used to index type with TypeScript, we use index signatures.

For instance, we write

const color: { [key: string]: any } = {
  red: null,
  green: null,
  blue: null,
};

to add the { [key: string]: any } type to color.

[key: string] is the index signature and it lets us add any string key into the object with the type.

The property value can be anything.

Conclusion

To fix Element implicitly has an ‘any’ type because expression of type ‘string’ can’t be used to index type with TypeScript, we use index signatures.