Categories
JavaScript Answers

How to make a checkbox without a form with HTML and JavaScript?

Sometimes, we want to make a checkbox without a form with HTML and JavaScript.

In this article, we’ll look at how to make a checkbox without a form with HTML and JavaScript.

How to make a checkbox without a form with HTML and JavaScript?

To make a checkbox without a form with HTML and JavaScript, we can add an input element with type checkbox.

Then we can get its value when we check or uncheck it by attaching a change event handler to it.

For instance, we write:

<input type="checkbox" />

to add an input with the type attribute set to checkbox to add a checkbox.

Then we write:

const input = document.querySelector('input')
input.onchange = (e) => {
  console.log(e.target.checked)
}

to select the input with querySelector.

Then we set the input.onchange property to a function that gets the checked value of the checkbox from e.target.checked.

Conclusion

To make a checkbox without a form with HTML and JavaScript, we can add an input element with type checkbox.

Then we can get its value when we check or uncheck it by attaching a change event handler to it.

Categories
JavaScript Answers

How to convert local time to UTC time with moment.js and JavaScript?

Sometimes, we want to convert local time to UTC time with moment.js and JavaScript.

In this article, we’ll look at how to convert local time to UTC time with moment.js and JavaScript.

How to convert local time to UTC time with moment.js and JavaScript?

To convert local time to UTC time with moment.js and JavaScript, we can use the moment utc and format method.

For instance, we write:

const d = new Date(2022, 1, 1)
const s = moment.utc(d).format("YYYY-MM-DD HH:mm:ss Z")
console.log(s)

to create the date d in local time.

Then we create a UTC moment object from d with moment.utc.

Next, we call format to with a format string that returns the date and time with the time zone.

As a result, s is '2022-02-01 08:00:00 +00:00'.

Conclusion

To convert local time to UTC time with moment.js and JavaScript, we can use the moment utc and format method.

Categories
JavaScript Answers

How to create a UTC JavaScript date?

Sometimes, we want to create a UTC JavaScript date.

In this article, we’ll look at how to create a UTC JavaScript date.

How to create a UTC JavaScript date?

To create a UTC JavaScript date, we can use the Date.UTC method.

For instance, we write:

const d = new Date(Date.UTC(2022, 10, 5));
console.log(d.toUTCString());

to call Date.UTC to create a timestamp set to Nov 5, 2022 in UTC.

Then we convert that to a JavaScript date object with the Date constructor.

Therefore, d.toUTCString returns 'Sat, 05 Nov 2022 00:00:00 GMT'.

Conclusion

To create a UTC JavaScript date, we can use the Date.UTC method.

Categories
JavaScript Answers

How to convert a JavaScript object to a query string?

Sometimes, we want to convert a JavaScript object to a query string.

In this article, we’ll look at how to convert a JavaScript object to a query string.

How to convert a JavaScript object to a query string?

To convert a JavaScript object to a query string, we can use the Object.entries method and some array methods.

For instance, we write:

const obj = {
  foo: "22",
  bar: "23434"
};

const queryString = Object.entries(obj)
  .map(([key, value]) => {
    return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
  })
  .join('&')
console.log(queryString)

to call Object.entries with obj to convert obj to an array of key-value pair arrays.

Then we call map with a callback to combine the key and value into a query parameter string.

Next, we call join with '&' to join the query parameters together.

As a result, queryString is 'foo=22&bar=23434'.

Conclusion

To convert a JavaScript object to a query string, we can use the Object.entries method and some array methods.

Categories
JavaScript Answers

How to write the equivalent of Python dictionary’s get method in JavaScript?

Sometimes, we want to write the equivalent of Python dictionary’s get method in JavaScript.

In this article, we’ll look at how to write the equivalent of Python dictionary’s get method in JavaScript.

How to write the equivalent of Python dictionary’s get method in JavaScript?

To write the equivalent of Python dictionary’s get method in JavaScript, we can use hasOwnProperty.

For instance, we write:

const obj = {
  foo: 1
}
const x = obj.hasOwnProperty("foo") ? obj.foo : "default";
const y = obj.hasOwnProperty("bar") ? obj.bar : "default";
console.log(x, y)

to call obj.hasOwnProperty with the keys we’re looking for in obj.

If hasOwnProperty returns true, that means the key is in obj.

And so we return the property’s value.

Otherwise, we return 'default'.

Therefore, x is 1 and y is default.

Conclusion

To write the equivalent of Python dictionary’s get method in JavaScript, we can use hasOwnProperty.