Categories
JavaScript Answers

How to hash a password using bcrypt inside a JavaScript async function?

Sometimes, we want to hash a password using bcrypt inside a JavaScript async function.

In this article, we’ll look at how to hash a password using bcrypt inside a JavaScript async function.

How to hash a password using bcrypt inside a JavaScript async function?

To hash a password using bcrypt inside a JavaScript async function, we can use the bcrypt.hash method.

For instance, we write:

const bcrypt = require('bcrypt');

(async () => {
  const password = 'password'
  const saltRounds = 2
  const hashedPassword = await bcrypt.hash(password, saltRounds)
  console.log(hashedPassword)
})()

We call bcrypt.hash with the password string that we want to hash and the number of saltRounds.

It returns a promise so we can use await to get the resolved value, which is the hashed password.

Conclusion

To hash a password using bcrypt inside a JavaScript async function, we can use the bcrypt.hash method.

Categories
JavaScript Answers

How to force input to only allow alphabet letters with JavaScript?

Sometimes, we want to force input to only allow alphabet letters with JavaScript.

In this article, we’ll look at how to force input to only allow alphabet letters with JavaScript.

How to force input to only allow alphabet letters with JavaScript?

To force input to only allow alphabet letters with JavaScript, we can set the keypress event handler of the input to a function that returns whether the key that’s pressed is a alphabet key.

For instance, we write:

<input>

to add an input.

Then we write:

const input = document.querySelector('input')
input.onkeypress = () => {
  return /[a-z]/i.test(event.key)
}

We get the input with document.querySelector.

Then we set the input.onkeypress property to a function that returns /[a-z]/i.test(event.key).

event.key has the character of the key that’s pressed.

So we’re check whether the key pressed is an alphabet key with test.

Conclusion

To force input to only allow alphabet letters with JavaScript, we can set the keypress event handler of the input to a function that returns whether the key that’s pressed is a alphabet key.

Categories
JavaScript Answers

How to copy the current URL to the clipboard with JavaScript?

To copy the current URL to the clipboard with JavaScript, we can call navigator.clipboard.writeText with window.location.href.

For instance, we write:

<button>
  copy
</button>

to add a button.

Then we write:

const button = document.querySelector('button')
button.onclick = () => {
  navigator.clipboard.writeText(window.location.href);
}

We get the button with document.querySelector.

Then we set the button.onclick property to a function that calls navigator.clipboard.writeText with window.location.href.

window.location.href has the URL of the current page, so the URL will be copied to the clipboard with the copy button is clicked.

Categories
JavaScript Answers

How to remove elements in an array using Lodash?

Sometimes, we want to remove elements in an array using Lodash.

In this article, we’ll look at how to remove elements in an array using Lodash.

How to remove elements in an array using Lodash?

To remove elements in an array using Lodash, we can use remove method.

For instance, we write:

const fruits = ['Apple', 'Banana', 'Orange', 'Grape']
_.remove(fruits, (fruit) => {
  return _.indexOf(['Apple', 'Banana', 'Orange'], fruit) !== -1
});
console.log(fruits)

We call remove with fruits and a callback that returns the condition for the items that should be removed.

We returned _.indexOf(['Apple', 'Banana', 'Orange'], fruit) !== -1 so 'Apple', 'Banana' and 'Orange' are removed from fruits.

The removed items are returned.

As a result, fruits is ['Grape'] after remove is called.

Conclusion

To remove elements in an array using Lodash, we can use remove method.

Categories
JavaScript Answers

How to get month name from two digit month number with JavaScript?

Sometimes, we want to get month name from two digit month number with JavaScript.

In this article, we’ll look at how to get month name from two digit month number with JavaScript.

How to get month name from two digit month number with JavaScript?

To get month name from two digit month number with JavaScript, we can use the moment.js’ format method.

For instance, we write:

const formattedMonth = moment().month(9).format('MMMM');
console.log(formattedMonth)

We call format with 'MMMM' to get the full month name.

As a result, formattedMonth is 'October' since we called month with 9 before calling format.

Conclusion

To get month name from two digit month number with JavaScript, we can use the moment.js’ format method.