Categories
JavaScript Answers

How to concatenate two numbers in JavaScript?

Sometimes, we want to concatenate two numbers in JavaScript.

In this article, we’ll look at how to concatenate two numbers in JavaScript.

How to concatenate two numbers in JavaScript?

To concatenate two numbers in JavaScript, we can use template literals.

For instance, we write

const a = 5;
const b = 6;
const numbersAsString = `${a}${b}`;

console.log(numbersAsString);

to interpolate the values a and b into the numersAsString template literal.

Then numbersAsString is '56'.

Conclusion

To concatenate two numbers in JavaScript, we can use template literals.

Categories
HTML

How to submit an HTML form without redirection?

Sometimes, we want to submit an HTML form without redirection.

In this article, we’ll look at how to submit an HTML form without redirection.

How to submit an HTML form without redirection?

To submit an HTML form without redirection, we can redirect to an invisible iframe.

For instance, we write

<iframe name="dummyframe" id="dummyframe" style="display: none"></iframe>

<form action="submitscript.php" target="dummyframe">...</form>

to add a hidden iframe with

<iframe name="dummyframe" id="dummyframe" style="display: none"></iframe>

And then we set the form’s target attribute to the ID of the hidden iframe to redirect to it after form submission is done.

Conclusion

To submit an HTML form without redirection, we can redirect to an invisible iframe.

Categories
JavaScript Answers

How to add keys for grouped output with lodash .groupBy and JavaScript?

Sometimes, we want to add keys for grouped output with lodash .groupBy and JavaScript.

In this article, we’ll look at how to add keys for grouped output with lodash .groupBy and JavaScript.

How to add keys for grouped output with lodash .groupBy and JavaScript?

To add keys for grouped output with lodash .groupBy and JavaScript, we can use the map method to add the keys returned by the groupby method.

For instance, we write

const data = [
  {
    name: "jim",
    color: "blue",
    age: "22",
  },
  {
    name: "non",
    color: "blue",
    age: "33",
  },
  {
    name: "joey",
    color: "green",
    age: "77",
  },
];

console.log(
  _.chain(data)
    .groupBy("color")
    .map((value, key) => ({ color: key, users: value }))
    .value()
);

to call chain with data and groupBy to return an array of data items grouped by the color property value.

And then we call map with a callback that sets the color to the key color value and users is set to value which has the data with the given color key value.

And then we call value to return the values.

Conclusion

To add keys for grouped output with lodash .groupBy and JavaScript, we can use the map method to add the keys returned by the groupby method.

Categories
JavaScript Answers

How to check if a string is HTML or not with JavaScript?

Sometimes, we want to check if a string is HTML or not with JavaScript.

In this article, we’ll look at how to check if a string is HTML or not with JavaScript.

How to check if a string is HTML or not with JavaScript?

To check if a string is HTML or not with JavaScript, we check if there’re any tags present in the string.

For instance, we write

const isHtml = /<\/?[a-z][\s\S]*>/i.test("<p>fizz buzz</p>");

to call /<\/?[a-z][\s\S]*>/i.test with "<p>fizz buzz</p>" to check if "<p>fizz buzz</p>" is a JavaScript string.

We use <\/?[a-z][\s\S]*> to check for any tags in the string to check if it has any HTML markup.

[a-z] checks for letters and \s\S checks for spaces and non-space characters.

Conclusion

To check if a string is HTML or not with JavaScript, we check if there’re any tags present in the string.

Categories
JavaScript Answers

How to call JavaScript function from URL or address bar?

Sometimes, we want to call JavaScript function from URL or address bar.

In this article, we’ll look at how to call JavaScript function from URL or address bar.

How to call JavaScript function from URL or address bar?

To call JavaScript function from URL or address bar, we can type in a data URL.

For instance, we type in

data:text/html,<script>alert('hi');</script>

into the address bar to run the JavaScript code in the script tag.

Therefore, we see an alert box that shows ‘hi’ displayed when we type this in.

Conclusion

To call JavaScript function from URL or address bar, we can type in a data URL.