Categories
JavaScript Tips

JavaScript Tips — Arrays and Numbers

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 when writing JavaScript code.

Verify that a Given Argument is a Number

We can check if a given argument is a number with the isNaN and isFinite functions.

For example, we can write:

function isNumber(n){
  return !isNaN(parseFloat(n)) && isFinite(n);
}

isNaN checks that a number isn’t NaN and isFinite checks that a number is finite.

If isNaN returns false and isFinite returns true , then we know that n is a number.

We use parseFloat to try to transform it to a floating-point number first.

Verify that a Given Argument is an Array

We can check if a variable is an array by using the Array.isArray method.

For example, we can write:

Array.isArray(obj)

This works everywhere including outside frames and other contexts.

If we don’t have to worry about frames, we can also use:

Object.prototype.toString.call(obj) === '[object Array]'

or:

arr instanceof Array

to do the check.

Get the Max or the Min in an Array of Numbers

We can get the max or min of a number with the Math.max or Math.min methods.

And we can use the spread operator to spread the arguments.

For example, we can write:

const numbers = [5, 4, 4, 7, 2, 6, 2];
const maxInNumbers = Math.max(...numbers);
const minInNumbers = Math.min(...numbers);

We spread the numbers array as arguments of the Math.max and Math.min methods to get the max and min number from the array respectively.

Empty an Array

We can empty an array by setting the length property to 0.

For example, we can write:

const myArray = [1, 2, 3];
myArray.length = 0;

Then myArray will be emptied.

Don’t Use delete to Remove an Item from an Array

delete isn’t supposed to be used to remove an item from an array.

So we shouldn’t write code like;

const myArray = [1, 2, 3];
delete myArray[1];

Instead, we use splice :

const myArray = [1, 2, 3];
myArray.splice(1, 1);

to remove the item with index 1.

The first argument is the index to delete.

The 2nd argument is how many items to delete starting from the index.

Truncate an Array Using Length

We can set the length property to a number bigger than 0 to truncate an array.

For example, we can write:

const myArray = [1, 2, 3, 4, 5, 6];
myArray.length = 2;

Now myArray would have length 2.

Use Logical and/or for Conditions

We can use && or || to create conditional expressions.

For example, we can write:

const foo = 10;
(foo === 10) && doSomething();
(foo === 5) || doSomething();

(foo === 10) && doSomething(); is the same as:

if (foo === 10){
  doSomething();
}

and (foo === 5) || doSomething(); is the same as:

if `(foo === 5){
  doSomething();
}`

Use the map() Method to Loop Through an Array’s Items

If we need to map each array entry to a new value, we can use the map method.

For example, we can write:

const squares = [1, 2, 3].map((val) => {
  return val ** 2;
});

We square each number in the array with map .

So we get:

[1, 4, 9]

as the value of squares .

Conclusion

We can do many things with arrays and numbers.

Leave a Reply

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