Categories
JavaScript Answers

How to convert a camel-case string to dashes in JavaScript?

Sometimes, we want to convert a camel-case string to dashes in JavaScript.

In this article, we’ll look at how to convert a camel-case string to dashes in JavaScript.

How to convert a camel-case string to dashes in JavaScript?

To convert a camel-case string to dashes in JavaScript, we can use the string’s replace method.

For instance, we write:

const camel = 'fooBar'
const dashed = camel.replace(/[A-Z]/g, m => "-" + m.toLowerCase());
console.log(dashed)

to call camel.replace with a regex to match all capital letters.

And then we pass in a callback that replace the matches with a dash and the matched letter converted to lower case concatenated to it.

As a result, dashed is "foo-bar".

Conclusion

To convert a camel-case string to dashes in JavaScript, we can use the string’s replace method.

Categories
JavaScript Answers

How to count and limit words in a text area with JavaScript?

Sometimes, we want to count and limit words in a text area with JavaScript.

In this article, we’ll look at how to count and limit words in a text area with JavaScript.

How to count and limit words in a text area with JavaScript?

To count and limit words in a text area with JavaScript, we can check the length of the text area’s value in the keypress event handler.

For instance, we write:

<textarea></textarea>

to add a text area.

Then we write:

const textarea = document.querySelector('textarea')

textarea.onkeypress = (e) => {
  if (e.target.value.length > 20) {
    e.preventDefault()
  }
};

to select the text area with querySelector.

Next, we set textarea.onkeypress to a function that checks if the text area’s input value is bigger than 20.

If it is, then we call e.preventDefault to stop appending the typed characters to the text area’s input value.

Conclusion

To count and limit words in a text area with JavaScript, we can check the length of the text area’s value in the keypress event handler.

Categories
JavaScript Answers

How to add a button click counter with JavaScript?

Sometimes, we want to add a button click counter with JavaScript.

In this article, we’ll look at how to add a button click counter with JavaScript.

How to add a button click counter with JavaScript?

To add a button click counter with JavaScript, we can add a click handler to a button.

For instance, we write:

<button>Click me</button>
<p>Clicks: <span id="clicks">0</span></p>

to add a button and a clicks display element.

Then we write:

const button = document.querySelector('button')
const span = document.querySelector('span')

let clicks = 0;

button.onclick = () => {
  clicks += 1;
  span.innerHTML = clicks;
};

to select the button and the span with querySelector.

Next, we set button.onclick to a click event handler function.

In it, we increment clicks by 1.

And we set span.innerHTML to clicks.

As a result, when we click the button, we show the number of clicks.

Conclusion

To add a button click counter with JavaScript, we can add a click handler to a button.

Categories
JavaScript Answers

How to sort an array of arrays in JavaScript?

Sometimes, we want to sort an array of arrays in JavaScript.

In this article, we’ll look at how to sort an array of arrays in JavaScript.

How to sort an array of arrays in JavaScript?

To sort an array of arrays in JavaScript, we can use the array sort method.

For instance, we write:

const array = [
  [123, 3],
  [745, 4],
  [643, 5],
  [643, 2]
];

const sortedArray = array.sort(([a], [b]) => {
  return b - a;
});

console.log(sortedArray)

We call array.sort with a callback that destructures the first entry from each nested array.

Then we return the value to determine how it’s sorted.

We rerturned b - a, so the returned sorted array will have the first entry sorted in descending order.

As a result, sortedArray is [[745, 4], [643, 5], [643, 2], [123, 3]] .

Conclusion

To sort an array of arrays in JavaScript, we can use the array sort method.

Categories
JavaScript Answers

How to remove all session storage items with keys that match a certain pattern with JavaScript?

Sometimes, we want to remove all session storage items with keys that match a certain pattern with JavaScript.

In this article, we’ll look at how to remove all session storage items with keys that match a certain pattern with JavaScript.

How to remove all session storage items with keys that match a certain pattern with JavaScript?

To remove all session storage items with keys that match a certain pattern with JavaScript, we can loop through the keys of the items we want to remove and remove the entries with the keys.

For instance, we write:

const keys = Object.keys(sessionStorage)
  .filter((k) => {
    return /foo/.test(k);
  })

for (const k of keys) {
  sessionStorage.removeItem(k);
}

to get the keys of the items we want to remove with:

const keys = Object.keys(sessionStorage)
  .filter((k) => {
    return /foo/.test(k);
  })

Then we loop through the keys with a for-of loop.

In it, we call sessionStorage.removeItem with key k to remove the entry with k as the key.

Conclusion

To remove all session storage items with keys that match a certain pattern with JavaScript, we can loop through the keys of the items we want to remove and remove the entries with the keys.