Categories
JavaScript Answers

How to get current time in milliseconds with moment.js and JavaScript?

Sometimes, we want to get current time in milliseconds with moment.js and JavaScript.

In this article, we’ll look at how to get current time in milliseconds with moment.js and JavaScript.

How to get current time in milliseconds with moment.js and JavaScript?

To get current time in milliseconds with moment.js and JavaScript, we use the valueOf method.

For instance, we write

const timeInMilliseconds = moment().valueOf();

to call moment to return an moment object with the current datetime.

And then we call valueOf on it to return the timestamp of the current datetime in milliseconds.

Conclusion

To get current time in milliseconds with moment.js and JavaScript, we use the valueOf method.

Categories
JavaScript Answers

How to check element CSS display with JavaScript?

Sometimes, we want to check element CSS display with JavaScript.

In this article, we’ll look at how to check element CSS display with JavaScript.

How to check element CSS display with JavaScript?

To check element CSS display with JavaScript, we use the window.getComputedStyle method.

For instance, we write

const { display } = window.getComputedStyle(element, null);

to call window.getComputedStyle with the element that we want to get the styles for.

Then we get the value of the display CSS property computed by the browser with the display property from the returned object.

Conclusion

To check element CSS display with JavaScript, we use the window.getComputedStyle method.

Categories
JavaScript Answers

How to remove first and last element in array with JavaScript?

Sometimes, we want to remove first and last element in array with JavaScript.

In this article, we’ll look at how to remove first and last element in array with JavaScript.

How to remove first and last element in array with JavaScript?

To remove first and last element in array with JavaScript, we can use the array slice method.

For instance, we write

const newFruits = fruits.slice(1, -1);

to call fruits.slice with 1 and -1 to return a new array without the first and last entries of fruits.

Then we assign the returned array to newFruits.

Conclusion

To remove first and last element in array with JavaScript, we can use the array slice method.

Categories
JavaScript Answers

How to get the number of bytes in a JavaScript string?

Sometimes, we want to get the number of bytes in a JavaScript string.

In this article, we’ll look at how to get the number of bytes in a JavaScript string.

How to get the number of bytes in a JavaScript string?

To get the number of bytes in a JavaScript string, we can convert it to a blob.

For instance, we write

const size = new Blob(["I'm a string"]).size;

to create a blob from the "I'm a string" string with the Blob constructor.

Then we get the number of bytes in the string with the size property.

Conclusion

To get the number of bytes in a JavaScript string, we can convert it to a blob.

Categories
JavaScript Answers

How to reorder arrays with JavaScript?

Sometimes, we want to reorder arrays with JavaScript.

In this article, we’ll look at how to reorder arrays with JavaScript.

How to reorder arrays with JavaScript?

To reorder arrays with JavaScript, we can use array destructuring syntax.

For instance, we write

const swapPositions = (array, a, b) => {
  [array[a], array[b]] = [array[b], array[a]];
};

const array = [1, 2, 3, 4, 5];
swapPositions(array, 0, 1);

to create the swapPositions function that takes the array and the indexes a and b that we want to swap.

In it, we put array[b] and array[a] into an array.

And then we swap them by destructuring them on the left side.

Then we call swapPositions with the array and the indexes to swap.

array is [2, 1, 3, 4, 5] after we call swapPositions.

Conclusion

To reorder arrays with JavaScript, we can use array destructuring syntax.