JS - Other

Fill an Array with Empty Spaces

Array(num).fill('');

Find Highest Value In A List Of Numbers

Math.max(n1, n2, n3, ..., nX)

Find Index Of Char In String

string.indexOf("char")

Find Lowest Value In A List Of Numbers

Math.min(n1, n2, n3, ..., nX)

Find Related Item In Array

array.find(x => conditions)

Find The Type Of A Variable Or An Expression

typeof "variable or expression"

Flattening Multidimensional Array

var flat_entries = [].concat(...multidimensionalArray);
multidimensionalArray.flat();
arrayInMultidimensionalArray.flat(2);

For Loop Array

By i

for(let i = 0; i < array.length; i++)


By item

for (let arrayItem in array)


By index

for (let index in array)

Fullscreen API

The Fullscreen API allows you to display an element or the entire page in full screen.

async function enterFullscreen() {
  await document.documentElement.requestFullscreen();
}

async function exitFullscreen() {
  await document.exitFullscreen();
}


NOTE: To use the Fullscreen API too, you need an interaction from the user.

Generate OTP

funtion generateOTP() {
  var digits = '0123456789';
  let otp = '';

  for (let i = 0; i < 6; i++) {
    otp += digits[Math.floor(Math.random() * 10)];
  }

  return otp;
}

Generate Unique Key

const generateUniqueId = () => {
  return Math.random().toString(36).substring(2);
}

Get a random Boolean

randomBoolean = Math.random() >= 0.5;