Categories
JavaScript Answers

How to detect touch screen devices with JavaScript?

Sometimes, we want to detect touch screen devices with JavaScript.

In this article, we’ll look at how to detect touch screen devices with JavaScript.

How to detect touch screen devices with JavaScript?

To detect touch screen devices with JavaScript, we check if the ontouchstart property is in document.documentElement.

For instance, we write

const isTouchDevice = "ontouchstart" in document.documentElement;

to check if the 'ontouchstart' property is in document.documentElement to check if the device supports touch.

Conclusion

To detect touch screen devices with JavaScript, we check if the ontouchstart property is in document.documentElement.

Categories
JavaScript Answers

How to override JavaScript’s toString() function to provide meaningful output for debugging?

Sometimes, we want to override JavaScript’s toString() function to provide meaningful output for debugging.

In this article, we’ll look at how to override JavaScript’s toString() function to provide meaningful output for debugging.

How to override JavaScript’s toString() function to provide meaningful output for debugging?

To override JavaScript’s toString() function to provide meaningful output for debugging, we can add the Symbol.toStringTag method into our class.

For instance, we write

class Person {
  constructor(name) {
    this.name = name;
  }
  get [Symbol.toStringTag]() {
    return "Person";
  }
}

let p = new Person("Dan");
Object.prototype.toString.call(p);

to create the Person class that has the Symbol.toStringTag which returns the string that’s logged when we call toString on a Person instance.

Then we create the p Person instance.

Finally, we call Object.prototype.toString.call with p to call toString using the overridden toString method we created.

Conclusion

To override JavaScript’s toString() function to provide meaningful output for debugging, we can add the Symbol.toStringTag method into our class.

Categories
JavaScript Answers

How to fill the whole canvas with specific color with JavaScript?

Sometimes, we want to fill the whole canvas with specific color with JavaScript.

In this article, we’ll look at how to fill the whole canvas with specific color with JavaScript.

How to fill the whole canvas with specific color with JavaScript?

To fill the whole canvas with specific color with JavaScript, we can use the canvas context fillRect method.

For instance, we write

<canvas width="300" height="150" id="canvas"> </canvas>

to add a canvas.

Then we write

const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
ctx.fillStyle = "blue";
ctx.fillRect(0, 0, canvas.width, canvas.height);

to select the canvas with getElementById.

And the we call getContext to get the context.

Next, we set the background color by setting the fillStyle.

And then we call fillRect with 0, 0, canvas.width, canvas.height to draw rectangle that fills the whole canvas with the fillStyle color.

Conclusion

To fill the whole canvas with specific color with JavaScript, we can use the canvas context fillRect method.

Categories
JavaScript Answers

How to determine if Chrome is in incognito mode via a script with JavaScript?

Sometimes, we want to determine if Chrome is in incognito mode via a script with JavaScript.

In this article, we’ll look at how to determine if Chrome is in incognito mode via a script with JavaScript.

How to determine if Chrome is in incognito mode via a script with JavaScript?

To determine if Chrome is in incognito mode via a script with JavaScript, we check if the file systems API can’t be used.

For instance, we write

const fs = window.RequestFileSystem || window.webkitRequestFileSystem;
if (!fs) {
  console.log("check failed");
} else {
  fs(
    window.TEMPORARY,
    100,
    console.log.bind(console, "not in incognito mode"),
    console.log.bind(console, "incognito mode")
  );
}

to check if fs is undefined.

If it’s undefined, then the script is run in incognito mode.

Otherwise, we call fs with callbacks to check if it’s executed successfully.

The 3rd argument is the success callback and the 4th is the error callback.

If the error callback is run, then the user is in incognito mode.

Conclusion

To determine if Chrome is in incognito mode via a script with JavaScript, we check if the file systems API can’t be used.

Categories
JavaScript Answers

How to generate unique random numbers between 1 and 100 with JavaScript?

Sometimes, we want to generate unique random numbers between 1 and 100 with JavaScript.

In this article, we’ll look at how to generate unique random numbers between 1 and 100 with JavaScript.

How to generate unique random numbers between 1 and 100 with JavaScript?

To generate unique random numbers between 1 and 100 with JavaScript, we can use the Math.random method.

For instance, we write

const arr = [];
while (arr.length < 10) {
  const r = Math.floor(Math.random() * 100) + 1;
  if (arr.indexOf(r) === -1) {
    arr.push(r);
  }
}
console.log(arr);

to use a while loop to put random numbers into the arr array until we have 10 random numbers.

We create the random number between 1 and 100 with

Math.floor(Math.random() * 100) + 1

Then we check if the number is already in arr with

arr.indexOf(r) === -1

If it isn’t, then we call push to push the number into the array.

Conclusion

To generate unique random numbers between 1 and 100 with JavaScript, we can use the Math.random method.