Categories
JavaScript Answers

How to get time in milliseconds in JavaScript?

Sometimes, we want to get time in milliseconds in JavaScript.

In this article, we’ll look at how to get time in milliseconds in JavaScript.

How to get time in milliseconds in JavaScript?

To get time in milliseconds in JavaScript, we can use the + operator with new Date().

For instance, we write

const currentTime = +new Date();

to create a new Date object with the current date and time with new Date().

Then we convert the current date and time to the timestamp in milliseconds by putting + before it.

Conclusion

To get time in milliseconds in JavaScript, we can use the + operator with new Date().

Categories
JavaScript Answers

How to create JSON object dynamically via JavaScript?

Sometimes, we want to create JSON object dynamically via JavaScript.

In this article, we’ll look at how to create JSON object dynamically via JavaScript.

How to create JSON object dynamically via JavaScript?

To create JSON object dynamically via JavaScript, we can create the object we want.

Then we call JSON.stringify to convert the object into a JSON string.

For instance, we write

let sitePersonnel = {};
let employees = [];
sitePersonnel.employees = employees;
console.log(sitePersonnel);

let firstName = "John";
let lastName = "Smith";
let employee = {
  firstName,
  lastName,
};
sitePersonnel.employees.push(employee);

console.log(JSON.stringify(sitePersonnel));

to create the sitePersonnel object that has various properties.

Then we call JSON.stringify to convert the sitePersonnel object into a JSON string.

Conclusion

To create JSON object dynamically via JavaScript, we can create the object we want.

Then we call JSON.stringify to convert the object into a JSON string.

Categories
JavaScript Answers

How to unset an element in an array in JavaScript?

Sometimes, we want to unset an element in an array in JavaScript.

In this article, we’ll look at how to unset an element in an array in JavaScript.

How to unset an element in an array in JavaScript?

To unset an element in an array in JavaScript, we call the array splice method.

For instance, we write

myArray.splice(key, 1);

to call splice with index key and 1 to remove the item in myArray at the key index.

Conclusion

To unset an element in an array in JavaScript, we call the array splice method.

Categories
JavaScript Answers

How to find the size of localStorage with JavaScript?

Sometimes, we want to find the size of localStorage with JavaScript.

In this article, we’ll look at how to find the size of localStorage with JavaScript.

How to find the size of localStorage with JavaScript?

To find the size of localStorage with JavaScript, we can convert it to a blob and get its size.

For instance, we write

const size = new Blob(Object.values(localStorage)).size;

to convert the localStorage object to a blob with

Blob(Object.values(localStorage))

Then we get the size of the blob with the size property.

Conclusion

To find the size of localStorage with JavaScript, we can convert it to a blob and get its size.

Categories
JavaScript Answers

How to find if variable is divisible by 2 with JavaScript?

Sometimes, we want to find if variable is divisible by 2 with JavaScript.

In this article, we’ll look at how to find if variable is divisible by 2 with JavaScript.

How to find if variable is divisible by 2 with JavaScript?

To find if variable is divisible by 2 with JavaScript, we can use the modulo operator.

For instance, we write

const divisibleBy2 = variable % 2 === 0;

to check if variable is divisible by 2 with

variable % 2 === 0

If variable is divisible by 2, then we get 0 as the remainder if we divide variable by 2.

% returns the remainder when we divide the left operand by the right one.

Conclusion

To find if variable is divisible by 2 with JavaScript, we can use the modulo operator.