Truthy and Falsy values in javascript

Md Shamim
3 min readMay 10, 2021

Ans:- Truthy and Falsy values in JavaScript are terms we use in JavaScript for those values that are evaluated to be true or false, but in reality, these values are not true or false. So this kind of values play an important role while doing comparisons in JavaScript, and taking care of those logics is really important while writing the program. For example a value 5 == ‘5’ return true but when we write it triple equal sign like 5 === ‘5’ we get false in output. This is because double equal sign performs some conversion before comparing value while (===) sign just compares the value with its type. So all those values that are not True or False but evaluated as a true or false in a Boolean context, those value considered “truthy” or “falsy”.

2. How to find the Max id in an array of objects in javascript
Ans:-
I had this array of objects and in each object I had an id. I wanted a quick and efficient way to look at all of the ids and find the max id.

In this article I will walk you through a 4 different solutions.

  • Array.forEach
  • Array.map
  • Array.reduce
  • Math.max

ARRAY.FOREACH

Your first thought might be to iterate over the array using some type of loop and why not, its what you have been taught to do since you started writing code. You can start off by declaring a max of zero, iterate over each character and if its id is larger than the max, update the max.

let max = 0;
characters.forEach(character => {
if (character.id > max) {
max = character.id;
}
});
assert(max === 444);

My main objective every time I write code is to get something to work first and then improve upon it later. If you were to run this code it certainly works but it just doesn’t seem right to me. What if you have one thousand or a million objects in the array? Any time that I start iterating over an array using some type of loop to perform som calculation that is a huge red flag for me.

ARRAY.MAP

Whenever that red flag of iterating over an array comes up I immediatly ask myself is this something that map/filter/reduce can solve. So if you were going to take that approach here you can start with the map method. The map() method creates a new array with the results of calling a provided function on every element in the calling array. You will use the map method to create a new array that contains just the id's so you're no longer working with objects. At this point you can just use the normal sort method on the array, grab the last element in the list and that is your max id.

const ids = characters.map(user => user.id);
const sorted = ids.sort((a, b) => a - b);
assert(sorted[sorted.length - 1] === 444);

If you wanted to get really fancy you can do all of that in a single statement.

assert(
characters.map(user => user.id).sort((a, b) => a - b)[
characters.length - 1
] === 444
);

ARRAY.REDUCE

The reduce() method executes a reducer function (that you provide) on each member of the array resulting in a single output value. Here you are going to call reduce on the characters array. You will define a accumulator which will accumulate the callback's return value. Every time the callback function is called it will return a value and store it in max. The callback method is looking at the character id and if it's greater than max it returns that, if not it returns max. Finally it needs to set a default value for max and that is what characters[0].id is doing.

--

--