Categories
JavaScript Answers

How to capture key press without placing an input element on the page with JavaScript?

Sometimes, we want to capture key press without placing an input element on the page with JavaScript.

In this article, we’ll look at how to capture key press without placing an input element on the page with JavaScript.

How to capture key press without placing an input element on the page with JavaScript?

To capture key press without placing an input element on the page with JavaScript, we can listen for keyboard events in the document.

For instance, we write

document.onkeydown = (evt) => {
  if (evt.ctrlKey && evt.keyCode === 90) {
    console.log("Ctrl-Z");
  }
};

to set the document.onkeydown property to a function that checks if ctrl+z is pressed.

We check for ctrl key press with evt.ctrlKey and we check for the z key press by checking if keyCode is 90.

Conclusion

To capture key press without placing an input element on the page with JavaScript, we can listen for keyboard events in the document.

Categories
JavaScript Answers

How to add method to object with JavaScript?

Sometimes, we want to add method to object with JavaScript.

In this article, we’ll look at how to add method to object with JavaScript.

How to add method to object with JavaScript?

To add method to object with JavaScript, we can add it to the prototype of the constructor.

For instance, we write

function Foo() {}
Foo.prototype.bar = function () {};

const x = new Foo();
x.bar();

to create the Foo constructor with

function Foo() {}

Then we add the bar instance method to its prototype with

Foo.prototype.bar = function () {};

Then we instantiate a Foo object and called the Foo instance’s bar method with

const x = new Foo();
x.bar();

Conclusion

To add method to object with JavaScript, we can add it to the prototype of the constructor.

Categories
JavaScript Answers

How to get am pm from the date time string using moment.js and JavaScript?

Sometimes, we want to get am pm from the date time string using moment.js and JavaScript.

In this article, we’ll look at how to get am pm from the date time string using moment.js and JavaScript.

How to get am pm from the date time string using moment.js and JavaScript?

To get am pm from the date time string using moment.js and JavaScript, we use the 'A' format code.

For instance, we write

console.log(
  moment("Mon 03-Jul-2022, 11:00 AM", "ddd DD-MMM-YYYY, hh:mm A").format(
    "hh:mm A"
  )
);

to call format on a moment object with "hh:mm A" to return a time string with the hours and minutes along with am or pm at the end with 'A'.

Conclusion

To get am pm from the date time string using moment.js and JavaScript, we use the 'A' format code.

Categories
JavaScript Answers

How to detect 64-bit or 32-bit Windows from user agent with JavaScript?

Sometimes, we want to detect 64-bit or 32-bit Windows from user agent with JavaScript.

In this article, we’ll look at how to detect 64-bit or 32-bit Windows from user agent with JavaScript.

How to detect 64-bit or 32-bit Windows from user agent with JavaScript?

To detect 64-bit or 32-bit Windows from user agent with JavaScript, we can use the navigator.userAgent string property.

For instance, we write

if (
  navigator.userAgent.includes("WOW64") ||
  navigator.userAgent.includes("Win64")
) {
  console.log("This is a 64 bit OS");
} else {
  console.log("Not a 64 bit OS");
}

to check if navigator.userAgent includes 'WOW64' or the 'Win64' strings.

If it includes either one, then the user agent is the 64 bit Windows user agent.

Conclusion

To detect 64-bit or 32-bit Windows from user agent with JavaScript, we can use the navigator.userAgent string property.

Categories
JavaScript Answers

How to create multi-dimensional associative arrays in JavaScript?

Sometimes, we want to create multi-dimensional associative arrays in JavaScript.

In this article, we’ll look at how to create multi-dimensional associative arrays in JavaScript.

How to create multi-dimensional associative arrays in JavaScript?

To create multi-dimensional associative arrays in JavaScript, we can create a nested object.

For instance, we write

const myObj = {
  fred: { apples: 2, oranges: 4, bananas: 7, melons: 0 },
  mary: { apples: 0, oranges: 10, bananas: 0, melons: 0 },
  sarah: { apples: 0, oranges: 0, bananas: 0, melons: 5 },
};

console.log(myObj["fred"]["apples"]);

to create the myObj object.

In it, we add the fred, mary and sarah properties.

And we set them to objects with the apples, oranges, bananas and melons properties.

Then we get a nested property with myObj["fred"]["apples"].

Conclusion

To create multi-dimensional associative arrays in JavaScript, we can create a nested object.