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.

Categories
JavaScript Answers

How to count words in string with JavaScript?

Sometimes, we want to count words in string with JavaScript.

In this article, we’ll look at how to count words in string with JavaScript.

How to count words in string with JavaScript?

To count words in string with JavaScript, we can use the string split method.

For instance, we write

const numWords = string.split(/\s+/).length;

to call string.split with the /\s+/ regex to split the string string by the spaces.

It returns an array with the split strings.

So we get the number of words in the string with the length property.

Conclusion

To count words in string with JavaScript, we can use the string split method.

Categories
JavaScript Answers

How to show current time in JavaScript in the format HH:MM:SS?

Sometimes, we want to show current time in JavaScript in the format HH:MM:SS.

In this article, we’ll look at how to show current time in JavaScript in the format HH:MM:SS.

How to show current time in JavaScript in the format HH:MM:SS?

To show current time in JavaScript in the format HH:MM:SS, we use the date’s toLocaleTimeString method.

For instance, we write

const d = new Date();
console.log(d.toLocaleTimeString());

to create date d with the current datetime.

And we call toLocaleTimeString to return a string with the human readable locale time string with date and time being the values in d.

Conclusion

To show current time in JavaScript in the format HH:MM:SS, we use the date’s toLocaleTimeString method.