Categories
JavaScript Answers

How to join two strings with a comma and space between them with JavaScript?

Sometimes, we want to join two strings with a comma and space between them with JavaScript.

In this article, we’ll look at how to join two strings with a comma and space between them with JavaScript.

How to join two strings with a comma and space between them with JavaScript?

To join two strings with a comma and space between them with JavaScript, we can use the array’s join method.

For instance, we write:

const strings = ["a", "b", "c"];
console.log(strings.join(", "));

to join the strings in strings with a comma and a space.

Therefore, the console log logs 'a, b, c'.

Conclusion

To join two strings with a comma and space between them with JavaScript, we can use the array’s join method.

Categories
JavaScript Answers React Answers

How to add a background image into a React Component with JavaScript?

Sometimes, we want to add a background image into a React Component with JavaScript.

In this article, we’ll look at how to add a background image into a React Component with JavaScript.

How to add a background image into a React Component with JavaScript?

To add a background image into a React Component with JavaScript, we can set the backgroundImage CSS property.

For instance, we write:

import React from "react";

export default function App() {
  return (
    <div
      style={{
        width: 300,
        height: 300,
        backgroundImage: `url(https://picsum.photos/300/300)`
      }}
    ></div>
  );
}

We set backgroundImage to the URL of the image we want as the background image.

Therefore, we see the background image displayed.

Conclusion

To add a background image into a React Component with JavaScript, we can set the backgroundImage CSS property.

Categories
JavaScript Answers

How to convert hours and minutes to minutes with moment.js?

Sometimes, we want to convert hours and minutes to minutes with moment.js.

In this article, we’ll look at how to convert hours and minutes to minutes with moment.js.

How to convert hours and minutes to minutes with moment.js?

To convert hours and minutes to minutes with moment.js, we can use the duration and asMinutes methods.

For instance, we write:

const m = moment.duration(moment('10:10:10', 'HH:mm:ss').format("HH:mm")).asMinutes()
console.log(m)

We call moment.duration with a moment object formatted in HH:mm format to get the duration object.

Then we convert the duration to an object with the asMinutes methods.

Therefore, we get 610 as a the value of m.

Conclusion

To convert hours and minutes to minutes with moment.js, we can use the duration and asMinutes methods.

Categories
JavaScript Answers

How to find DOM elements with CSS selectors with JavaScript?

Sometimes, we want to find DOM elements with CSS selectors with JavaScript.

In this article, we’ll look at how to find DOM elements with CSS selectors with JavaScript.

How to find DOM elements with CSS selectors with JavaScript?

To find DOM elements with CSS selectors with JavaScript, we can use the querySelectorAll method.

For instance, we write:

<table>
  <tbody>
    <tr>
      <td></td>
      <td></td>
    </tr>
  </tbody>
</table>

to add the table with some td elements.

Then we can select the 2nd td by writing:

const cells = document.querySelectorAll("table > tbody > tr > td:nth-of-type(2)");
console.log(cells)

with querySelectorAll.

Therefore, cells is a node list object with the 2nd td inside.

Conclusion

To find DOM elements with CSS selectors with JavaScript, we can use the querySelectorAll method.

Categories
JavaScript Answers

How to join relative URLs in JavaScript?

Sometimes, we want to join relative URLs in JavaScript.

In this article, we’ll look at how to join relative URLs in JavaScript.

How to join relative URLs in JavaScript?

To join relative URLs in JavaScript, we can use the URL constructor.

For instance, we write:

const {
  href
} = new URL('../../address', 'http://www.example.com/more/evenmore/')
console.log(href)

We use the URL constructor to combine the relative URL with the base URL.

We then get the combined URL from the href property.

Therefore, href is 'http://www.example.com/address'.

Conclusion

To join relative URLs in JavaScript, we can use the URL constructor.