Categories
JavaScript Answers

How to assign value from successful promise resolve to external variable with JavaScript?

Sometimes, we want to assign value from successful promise resolve to external variable with JavaScript.

In this article, we’ll look at how to assign value from successful promise resolve to external variable with JavaScript.

How to assign value from successful promise resolve to external variable with JavaScript?

To assign value from successful promise resolve to external variable with JavaScript, we use async and await.

For instance, we write

const myFunction = async () => {
  vm.feed = await getFeed();
  // ...
};

to get the resolve value of the promise returned by getFeed with await.

Then we assign the returned value to vm.feed.

We have to use await inside async functions.

Conclusion

To assign value from successful promise resolve to external variable with JavaScript, we use async and await.

Categories
JavaScript Answers

How to prevent HTML and script injections in JavaScript?

Sometimes, we want to prevent HTML and script injections in JavaScript.

In this article, we’ll look at how to prevent HTML and script injections in JavaScript.

How to prevent HTML and script injections in JavaScript?

To prevent HTML and script injections in JavaScript, we escape the string with the JavaScript code.

For instance, we write

const escapedHtml = html.replace(/</g, "&lt;").replace(/>/g, "&gt;");

to call replace to replace the opening and closing brackets with "&lt;" and "&gt;" respectively with replace.

The g flag will make replace replace all matches.

The escaped string is returned and the string won’t have valid code so it can’t be run.

Conclusion

To prevent HTML and script injections in JavaScript, we escape the string with the JavaScript code.

Categories
JavaScript Answers

How to create a ul and fill it based on a passed array with JavaScript?

Sometimes, we want to create a ul and fill it based on a passed array with JavaScript.

In this article, we’ll look at how to create a ul and fill it based on a passed array with JavaScript.

How to create a ul and fill it based on a passed array with JavaScript?

To create a ul and fill it based on a passed array with JavaScript, we can loop through the array and then add the li elements with the array entry content into the ul element.

For instance, we write

const options = {
  set0: ["Option 1", "Option 2"],
  set1: ["First Option", "Second Option", "Third Option"],
};

const list = "<li>" + options.set0.join("</li><li>") + "</li>";
document.getElementById("list").innerHTML = list;

to get the options.set0 array and then join the entries by calling join with "</li><li>".

And then we wrap the returned string with opening and closing li tags.

Next, we select the ul with getElementById.

And then we set its innerHTML property to list to render the li’s in the ul.

Conclusion

To create a ul and fill it based on a passed array with JavaScript, we can loop through the array and then add the li elements with the array entry content into the ul element.

Categories
JavaScript Answers

How to define a JavaScript long integer?

Sometimes, we want to define a JavaScript long integer.

In this article, we’ll look at how to define a JavaScript long integer.

How to define a JavaScript long integer?

To define a JavaScript long integer, we can define a bigint.

For instance, we write

const rem = bigInt("1738141852226360940").mod("32").valueOf();
console.log(rem);

to call the bigInt function with a string with the integer we want to define.

Then we call mod to return the modulo of the bigint when divided by 32.

And then we call valueOf to convert the returned value to an integer.

Conclusion

To define a JavaScript long integer, we can define a bigint.

Categories
JavaScript Answers

How to encode a query string so that it is the value of another query string in JavaScript?

Sometimes, we want to encode a query string so that it is the value of another query string in JavaScript.

In this article, we’ll look at how to encode a query string so that it is the value of another query string in JavaScript.

How to encode a query string so that it is the value of another query string in JavaScript?

To encode a query string so that it is the value of another query string in JavaScript, we use the encodeURIComponent function.

For instance, we write

const c = "d e";
const query = "?a=b&c=" + encodeURIComponent(c);
const uri = "http://www.example.com/script?query=" + encodeURIComponent(query);

to call encodeURIComponent with c to return a URL encoded version of string c.

Then we append that to the "http://www.example.com/script?query=".

Conclusion

To encode a query string so that it is the value of another query string in JavaScript, we use the encodeURIComponent function.