Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
May 24, 2021 03:20 pm GMT

JavaScript Interview Question 42: How Math.max works in JS

coderslang javascript interview question #42

How exactly Math.max works in JavaScript? Whats the output?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

In JavaScript, the function Math.max() accepts variable number of arguments and returns the biggest of them.

If you pass a couple of arrays into Math.max they will be first converted to strings and then into numbers:

console.log(Math.max([ 0 ], [ 1 ])); // 1console.log(Math.max("0", "1"));     // 1console.log(Math.max(0, 1));         // 1

Booleans will be also converted to numbers. true becomes one and false becomes zero:

console.log(Math.max(true, false));  // 1console.log(Math.max(0, 1));         // 1

Now the condition inside of an if statement can be simplified and we can make sure were getting into the else branch:

if (1 > 1) { // false  console.log('array won');} else {  console.log('array lost');}

ANSWER: The string array lost will be logged to the console.

Read more JavaScript Tutorials or Learn Full-Stack JavaScript


Original Link: https://dev.to/coderslang/javascript-interview-question-42-how-math-max-works-in-js-13ei

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To