Categories
JavaScript Answers

How to insert a text where cursor is using JavaScript?

Sometimes, we want to insert a text where cursor is using JavaScript.

In this article, we’ll look at how to insert a text where cursor is using JavaScript.

How to insert a text where cursor is using JavaScript?

To insert a text where cursor is using JavaScript, we can get the caret position with the selectionStart property of the element.

For instance, we write

const insertAtCaret = (el, text) => {
  const caretPos = el.selectionStart;
  const textAreaTxt = el.value;
  el.value =
    textAreaTxt.substring(0, caretPos) + text + textAreaTxt.substring(caretPos);
};

to create the insertAtCaret function.

In it, we get the cursor position with el.selectionStart.

And then we get the input value with el.value.

Then we set new text box value with

textAreaTxt.substring(0, caretPos) + text + textAreaTxt.substring(caretPos)

To put the text where the cursor is.

Conclusion

To insert a text where cursor is using JavaScript, we can get the caret position with the selectionStart property of the element.

Categories
JavaScript Answers

How to remove leading zeros from a number in JavaScript?

Sometimes, we want to remove leading zeros from a number in JavaScript.

In this article, we’ll look at how to remove leading zeros from a number in JavaScript.

How to remove leading zeros from a number in JavaScript?

To remove leading zeros from a number in JavaScript, we can use the Number function.

For instance, we write

const numString = "065";
number = Number(numString);
console.log(number);

to call Number with numString to return the number converted from numString.

The leading zeroes in the string will be dropped.

Conclusion

To remove leading zeros from a number in JavaScript, we can use the Number function.

Categories
JavaScript Answers

How to remove property for all objects in array with JavaScript?

Sometimes, we want to remove property for all objects in array with JavaScript.

In this article, we’ll look at how to remove property for all objects in array with JavaScript.

How to remove property for all objects in array with JavaScript?

To remove property for all objects in array with JavaScript, we can use the rest syntax.

For instance, we write

const newArray = array.map(
  ({ dropAttr1, dropAttr2, ...keepAttrs }) => keepAttrs
);

to call array.map with a callback that get an object with all the properties we want to keep in keepAttrs.

And then we return that as the value.

Conclusion

To remove property for all objects in array with JavaScript, we can use the rest syntax.

Categories
JavaScript Answers

How to convert an ISO date to the date format yyyy-mm-dd with JavaScript?

To convert an ISO date to the date format yyyy-mm-dd with JavaScript, we can call the string substring method.

For instance, we write

const date = new Date("2022-03-10T02:00:00Z");
const isoDate = date.toISOString().substring(0, 10);

to create a new Date instance.

Then we call toISOString to return a string with the ISO date time.

And then we get the date portion of the string with substring.

Conclusion

To convert an ISO date to the date format yyyy-mm-dd with JavaScript, we can call the string substring method.

Categories
JavaScript Answers

How to rethrow an exception in JavaScript, but preserve the stack?

Sometimes, we want to rethrow an exception in JavaScript, but preserve the stack.

In this article, we’ll look at how to rethrow an exception in JavaScript, but preserve the stack.

How to rethrow an exception in JavaScript, but preserve the stack?

To rethrow an exception in JavaScript, but preserve the stack, we just throw the exception.

For instance, we write

throw err;

to throw the err Error instance with throw.

Conclusion

To rethrow an exception in JavaScript, but preserve the stack, we just throw the exception.