Categories
JavaScript Tips

Useful JavaScript Tips — Sleep and Math

Spread the love

Like any kind of apps, JavaScript apps also have to be written well.

Otherwise, we run into all kinds of issues later on.

In this article, we’ll look at some tips we should follow to write JavaScript code faster and better.

Pause Execution of a Function

We can pause execution of a function by creating our own sleep function.

For instance, we can write:

const sleep = (ms) => {
  return new Promise(resolve => setTimeout(resolve, ms))
}

We return a promise with the setTimeour function. The callback is the resolve function.

ms is the amount of pause execution in ms .

Then we can use it by writing:

const foo = async () => {
  await sleep(2000);
  // do more stuff
}

We called sleep with 2000 so the function will pause for 2 seconds before running the code below the sleep call.

It can be used with a loop:

const loop = async () => {
  for (const item of arr) {
    await sleep(2000);
    console.log(item);
  }
}

for-of loops can be used with promises.

So sleep will pause the loop body for 2 seconds.

Generate Random and Unique Strings

We can use the randomstring package to generate random strings.

For instance,e we can wrote:

const randomstring = require('randomstring');
`randomstring.generate(20);`

to generate a string of length 20.

To generate multiple unique strings, we can put all the items in a set.

For instance, we can write:

`const randomstring = require('randomstring');
`const strings = new Set()

while (strings.size < 50) {
  strings.add(randomstring.generate(20));
}

Then we generate unique strings until the set has 50 strings.

Then we can retrieve the strings with the values method.

For instance, we can write:

for (const value of strings.values()) {
  console.log(value);
}

JavaScript Math Object

JavaScript Math object has many properties and methods to make doing math easier.

Math.abs()

The Math.abs method returns the absolute value of a number.

We can write:

Math.abs(-10)

and get 10.

Math.acos()

Returns the arccosine of the argument.

For instance, we can write:

Math.acos(0.3)

and we get 1.2661036727794992.

Math.asin()

Returns the arcsine of the argument.

For instance, we can write:

Math.asin(0.5)

and we get 0.5235987755982989.

Math.atan()

Returns the rectangle of the argument.

For example, we can write:

Math.atan(20)

and we get 1.5208379310729538.

Math.atan2()

Returns the arctangent of the quotient of its arguments.

For instance, we can write:

Math.atan2(3, 2)

and get 0.982793723247329.

Math.ceil()

Rounds a number up to the nearest integer.

For instance, we can write:

Math.ceil(1.99999)

and get 2.

Math.cos()

Returns the cosine of the angle expressed in radians.

For example, we can write:

Math.cos(0)

and get 1.

Math.exp()

Returns the value of e multiplied per exponent that’s passed as the argument.

For example, we can write:

Math.exp(2)

and get 7.38905609893065.

Math.floor()

Rounds the number down to the nearest integer.

For instance, we can write:

Math.floor(1.99999)

and get 1.

Math.log()

Gets the natural logarithm of a number.

For instance, we can write:

Math.log(Math.E)

and get 1.

Math.max()

Gets the maximum number in the set of numbers passed as arguments.

For instance, we can write:

Math.max(1, 2, 3)

and get 3.

Math.min()

Gets the minimum number in the set of numbers passed as arguments.

For example, we can write:

Math.max(1, 2, 3)

and get 1.

Math.pow()

Raised the first argument to the second argument.

For instance, we can write:

Math.pow(2, 3)

and get 8.

Math.random()

Returns a pseudorandom number between 0 and 1.

For instance, we can write:

Math.random()

and get 0.08500663407619236.

Math.round()

Returns the nearest integer.

For instance, we can write:

Math.round(1.3)

and get 1.

Math.sin()

Gets the sine of the angle expressed in radians.

For example, we can write:

Math.sin(0)

and get 0.

Math.sqrt()

Gets the square root of the argument.

For instance, we can write:

Math.sqrt(9)

and get 3.

Math.tan()

Gets the angle of an angle in radians.

For instance, we can write:

Math.tan(Math.PI * 2)

and we get -2.4492935982947064e-16.

Arithmetic Operators

JavaScript has the usual arithmetic operators.

They’re addition, subtraction, multiplication, division, remainder, and exponentiation.

Then we can write:

const three = 1 + 2;

for addition.

But we’ve to make sure that both operands are numbers. Otherwise, it’s used as concatenation.

Subtraction is written as:

const three = 5 - 2;

Division can be written as:

const result = 20 / 10;

If we divide by zero, we get Infinity or -Infinity .

Multiplication can be done by writing:

1 * 2

We can exponentiate by writing:

2 ** 3

and get 8.

We can get the remainder of division by writing:

const result = 20 % 6

and get 2.

Conclusion

There are many operations and math methods we can use.

Also, we can pause execution of functions by using setTimeout .

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *