Categories
JavaScript Answers

How to change the buttons text using JavaScript?

Sometimes, we want to change the buttons text using JavaScript.

In this article, we’ll look at how to change the buttons text using JavaScript.

How to change the buttons text using JavaScript?

To change the buttons text using JavaScript, we set the innerText property.

For instance, we write

document.getElementById('ShowButton').innerText = 'Show filter';

to select the button with getElementById.

Then we set the innerText property of the button to the text we want to show the button with the text we set.

Conclusion

To change the buttons text using JavaScript, we set the innerText property.

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
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.