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

How exactly Math.max works in JavaScript? What’s 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 arr…


This content originally appeared on DEV Community and was authored by Coderslang: Become a Software Engineer

coderslang javascript interview question #42

How exactly Math.max works in JavaScript? What’s 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 ])); // 1
console.log(Math.max("0", "1"));     // 1
console.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));  // 1
console.log(Math.max(0, 1));         // 1

Now the condition inside of an if statement can be simplified and we can make sure we’re 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


This content originally appeared on DEV Community and was authored by Coderslang: Become a Software Engineer


Print Share Comment Cite Upload Translate Updates
APA

Coderslang: Become a Software Engineer | Sciencx (2021-05-24T15:20:55+00:00) JavaScript Interview Question #42: How Math.max works in JS. Retrieved from https://www.scien.cx/2021/05/24/javascript-interview-question-42-how-math-max-works-in-js/

MLA
" » JavaScript Interview Question #42: How Math.max works in JS." Coderslang: Become a Software Engineer | Sciencx - Monday May 24, 2021, https://www.scien.cx/2021/05/24/javascript-interview-question-42-how-math-max-works-in-js/
HARVARD
Coderslang: Become a Software Engineer | Sciencx Monday May 24, 2021 » JavaScript Interview Question #42: How Math.max works in JS., viewed ,<https://www.scien.cx/2021/05/24/javascript-interview-question-42-how-math-max-works-in-js/>
VANCOUVER
Coderslang: Become a Software Engineer | Sciencx - » JavaScript Interview Question #42: How Math.max works in JS. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/24/javascript-interview-question-42-how-math-max-works-in-js/
CHICAGO
" » JavaScript Interview Question #42: How Math.max works in JS." Coderslang: Become a Software Engineer | Sciencx - Accessed . https://www.scien.cx/2021/05/24/javascript-interview-question-42-how-math-max-works-in-js/
IEEE
" » JavaScript Interview Question #42: How Math.max works in JS." Coderslang: Become a Software Engineer | Sciencx [Online]. Available: https://www.scien.cx/2021/05/24/javascript-interview-question-42-how-math-max-works-in-js/. [Accessed: ]
rf:citation
» JavaScript Interview Question #42: How Math.max works in JS | Coderslang: Become a Software Engineer | Sciencx | https://www.scien.cx/2021/05/24/javascript-interview-question-42-how-math-max-works-in-js/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.