Categories
JavaScript Answers

How to get the second digit from a number with JavaScript?

Sometimes, we want to get the second digit from a number with JavaScript.

In this article, we’ll look at how to get the second digit from a number with JavaScript.

How to get the second digit from a number with JavaScript?

To get the second digit from a number with JavaScript, we convert it to a string and destructure it.

For instance, we write

const [, digit] = myVar.toString();

to call myVar.toString to return a string version of the myVar number.

Then we get the 2nd digit with `destructuring.

Conclusion

To get the second digit from a number with JavaScript, we convert it to a string and destructure it.

Categories
JavaScript Answers

How to change onclick attribute with JavaScript?

To change onclick attribute with JavaScript, we set the onclick property.

For instance, we write

document.getElementById(id).onclick = () => {
  //...
};

to get the element with getElementById.

Then we set its onclick property to the click handler to set the onclick attribute value.

Categories
JavaScript Answers

How to add commas or spaces to group every three digits with JavaScript?

Sometimes, we want to add commas or spaces to group every three digits with JavaScript.

In this article, we’ll look at how to add commas or spaces to group every three digits with JavaScript.

How to add commas or spaces to group every three digits with JavaScript?

To add commas or spaces to group every three digits with JavaScript, we use the toLocaleString method.

For instance, we write

console.log(Number(1234567890.12).toLocaleString());

to call toLocaleString on the number to return a string with the thousands separators included.

Conclusion

To add commas or spaces to group every three digits with JavaScript, we use the toLocaleString method.

Categories
JavaScript Answers

How to sort an array of objects with JavaScript?

Sometimes, we want to sort an array of objects with JavaScript.

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

How to sort an array of objects with JavaScript?

To sort an array of objects with JavaScript, we call the array sort method.

For instance, we write

const library = [
  { name: "Steve", course: "WAP", courseID: "cs452" },
  { name: "Rakesh", course: "WAA", courseID: "cs545" },
  { name: "Asad", course: "SWE", courseID: "cs542" },
];

const sortedByNname = library.sort((a, b) => a.name > b.name);

to call library.sort with a function that compares the name values of each entry alphabetically.

It returns an array sorted by the name property value of each object.

Conclusion

To sort an array of objects with JavaScript, we call the array sort method.

Categories
JavaScript Answers

How to write regex for allowing alphanumeric, – , _ and space with JavaScript?

Sometimes, we want to write regex for allowing alphanumeric, – , _ and space with JavaScript.

In this article, we’ll look at how to write regex for allowing alphanumeric,- , _ and space with JavaScript.

How to write regex for allowing alphanumeric, – , _ and space with JavaScript?

To write regex for allowing alphanumeric, – , _ and space with JavaScript, we use various patterns.

For instance, we write

const re = /^[\w\-\s]+$/;

to define the regex to match alphanumeric, – , _ and space.

\w matches alphanumeric characters and _.

\- matches -,

\s matches spaces.

^ indicates the start of a word.

$ indicates the end of a word.

Conclusion

To write regex for allowing alphanumeric, – , _ and space with JavaScript, we use various patterns.