Categories
JavaScript Answers

How to convert byte array to string in JavaScript?

Sometimes, we want to convert byte array to string in JavaScript.

In this article, we’ll look at how to convert byte array to string in JavaScript.

How to convert byte array to string in JavaScript?

To convert byte array to string in JavaScript, we can use the String.fromCharCode method.

For instance, we write

const s = String.fromCharCode(...[102, 111, 111]);

to call String.fromCharCode with a number array with the character codes spread as arguments of it.

It returns string s with the characters that correspond to the character codes.

Conclusion

To convert byte array to string in JavaScript, we can use the String.fromCharCode method.

Categories
JavaScript Answers

How to use objects in for of loops with JavaScript?

Sometimes, we want to use objects in for of loops with JavaScript.

In this article, we’ll look at how to use objects in for of loops with JavaScript.

How to use objects in for of loops with JavaScript?

To use objects in for of loops with JavaScript, we can use the Object.entries method.

For instance, we write

const myObject = {
  first: "one",
  second: "two",
};

for (const [key, value] of Object.entries(myObject)) {
  console.log(key, value);
}

to call Object.entries with myObject to return an array of myObject key-value pair arrays.

Then we use the for-of loop and the destructuring syntax to get the key and value from each entry.

In it, we log the key and value of each pair.

Conclusion

To use objects in for of loops with JavaScript, we can use the Object.entries method.

Categories
JavaScript Answers

How to calculate x% of a number with JavaScript?

Sometimes, we want to calculate x% of a number with JavaScript.

In this article, we’ll look at how to calculate x% of a number with JavaScript.

How to calculate x% of a number with JavaScript?

To calculate x% of a number with JavaScript, we can use some arithmetic operators.

For instance, we write

const result = (35.8 / 100) * 10000;

to get 35.8 percent of 10000.

We use (35.8 / 100) to get the 35.8 percent part.

And then we multiply that by 10000 to get 35.8 percent of 10000.

Conclusion

To calculate x% of a number with JavaScript, we can use some arithmetic operators.

Categories
React Answers

How to check if the React component is unmounted?

Sometimes, we want to check if the React component is unmounted.

In this article, we’ll look at how to check if the React component is unmounted.

How to check if the React component is unmounted?

To check if the React component is unmounted, we can use a ref to keep track of the mounted value.

For instance, we write

function MyComponent(props) {
  const isMounted = useRef(false);

  useEffect(() => {
    isMounted.current = true;
    return () => {
      isMounted.current = false;
    };
  }, []);

  return <>...</>;
}

export default MyComponent;

to define the isMounted ref with

const isMounted = useRef(false);

Then we add

isMounted.current = true;

in the useEffect to set isMounted.current to true when it’s mounted.

We call useEffect with an empty array so the callback only runs when it’s mounted.

And we return a function that’s called when it’s unmounted in the useEffect callback.

So we set isMounted.current to false there.

Conclusion

To check if the React component is unmounted, we can use a ref to keep track of the mounted value.

Categories
JavaScript Answers

How to get first letter of each word in a string in JavaScript?

Sometimes, we want to get first letter of each word in a string in JavaScript.

In this article, we’ll look at how to get first letter of each word in a string in JavaScript.

How to get first letter of each word in a string in JavaScript?

To get first letter of each word in a string in JavaScript, we can use the string match method.

For instance, we write

const str = "Java Script Object Notation";
const matches = str.match(/\b(\w)/g);
const acronym = matches.join("");

console.log(acronym);

to call str.match with a regex to match the first letter after a word boundary.

We use the g flag to get all matches.

Then we call matches.join to combine the returned array of characters into a string.

Conclusion

To get first letter of each word in a string in JavaScript, we can use the string match method.